Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ee4c8e9
Delete R/create_incidence_table.R
Kwan-Jenny Jan 31, 2026
4207ef5
Delete R/create_noise_df.R
Kwan-Jenny Jan 31, 2026
a05bd51
Delete R/plot_summary_metrics.R
Kwan-Jenny Jan 31, 2026
1b39d19
Delete R/simulate_seroincidence.R
Kwan-Jenny Jan 31, 2026
992fdb0
Delete R/prepare_df_for_serocalculator.R
Kwan-Jenny Jan 31, 2026
94f3bd9
Delete R/postprocess_jags_output.R
Kwan-Jenny Jan 31, 2026
19eb4d2
Delete R/generate_final_table.R
Kwan-Jenny Jan 31, 2026
fb6f507
Delete R/create_xs_data.R
Kwan-Jenny Jan 31, 2026
40ddcd8
Delete R/process_shigella_data.R
Kwan-Jenny Jan 31, 2026
9d1b8ac
Add process_shigella_data function for data reshaping
Kwan-Jenny Jan 31, 2026
58db1c7
Add function to summarize antibody trajectories for newperson
Kwan-Jenny Jan 31, 2026
137a598
Add function to predict posterior at specified times
Kwan-Jenny Feb 1, 2026
f054420
Add compute_residual_metrics function for antibody data
Kwan-Jenny Feb 1, 2026
9efd9b1
Add model comparison function with residual metrics
Kwan-Jenny Feb 1, 2026
4655d41
Add fmt_mci function to format median and intervals
Kwan-Jenny Feb 1, 2026
ae38681
Add function to extract and summarize newperson parameters
Kwan-Jenny Feb 1, 2026
87c45a2
Update R/fig2_overall_newperson.R
Kwan-Jenny Feb 1, 2026
e7648cd
Update R/prep_newperson_params.R
Kwan-Jenny Feb 1, 2026
3702aa1
Update R/model_comparison_table.R
Kwan-Jenny Feb 1, 2026
be6012c
Update R/compute_residual_metrics.R
Kwan-Jenny Feb 1, 2026
cca5392
Update R/process_shigella_data.R
Kwan-Jenny Feb 1, 2026
9b608fe
Fix package dependencies, namespace exports, and package infrastructu…
Copilot Mar 31, 2026
756544d
Fix 5 CI failures: version bump, LICENSE, non-ASCII chars, VignetteBu…
Copilot Mar 31, 2026
e4e6c42
Update man/fmt_mci.Rd to match roxygen docs (devtools::document() sync)
Copilot Mar 31, 2026
190c42f
Fix review feedback: rlang::sym rename, t1=0 edge case, shigella::: i…
Copilot Mar 31, 2026
c101941
Restore ab() and bt() to exact upstream implementations; update t1=0 …
Copilot Apr 1, 2026
913eb3b
Update data-raw/mock_data.R
Kwan-Jenny Apr 2, 2026
1fa288b
Fix roxygen docs: give bt() its own params, move trajectory docs to ab()
Copilot Apr 2, 2026
6b11bbf
Fix Github -> GitHub capitalization in inst/WORDLIST
Copilot Apr 2, 2026
9059965
Fix Github -> GitHub capitalization in NEWS.md
Copilot Apr 2, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
^vignettes/articles/*_files$
^vignettes/articles$
^codecov\.yml$
^data-raw$
^README\.Rmd$
^\.lintr\.R$
^_quarto\.yml$
^.*\.qmd$
^.*\.png$
^Vary.*\.r$
^sim_.*\.r$
^simulation.*\.qmd$
48 changes: 48 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

permissions:
contents: read

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}

env:
GITHUB_PAT: ${{ github.token }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
46 changes: 46 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
on:
push:
branches: [main, master]
release:
types: [published]
workflow_dispatch:

name: pkgdown

permissions:
contents: write
pages: write

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ github.token }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4
with:
clean: false
branch: gh-pages
folder: docs
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ simulation1_edited_files
simulation1_edited.html
*.rmarkdown
**/.quarto/
data/
data/*
!data/.gitignore
!data/mock_posterior_draws.rda
!data/mock_case_data.rda
/.quarto/
*.pdf
README_files
23 changes: 15 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shigella
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9004
Version: 0.0.0.9005
Authors@R: c(
person("Kwan Ho", "Lee", , "ksjlee@ucdavis.edu", role = c("aut", "cre")),
person("Douglas Ezra", "Morrison", , "demorrison@ucdavis.edu", role = c("aut"),
Expand All @@ -9,14 +9,15 @@ Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
Imports:
cli,
dplyr,
future,
future.apply,
ggplot2,
gridExtra,
serodynamics (>= 0.0.0.9011)
rlang,
serocalculator,
tibble,
tidyr
URL: https://ucd-serg.github.io/shigella/
Remotes:
UCD-SERG/serodynamics
Expand All @@ -26,8 +27,11 @@ Suggests:
coda,
forcats,
furrr,
future,
future.apply,
ggeasy,
ggmcmc,
gridExtra,
gtsummary,
haven,
here,
Expand All @@ -42,15 +46,18 @@ Suggests:
purrr,
quarto,
readxl,
rmarkdown,
runjags,
scales,
serodynamics (>= 0.0.0.9011),
spelling,
table1,
tibble,
tidyr,
testthat (>= 3.0.0),
tidyverse
VignetteBuilder: knitr
Depends:
R (>= 3.5)
LazyData: true
Config/testthat/edition: 3
Config/Needs/website: quarto
Language: en-US
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2024
COPYRIGHT HOLDER: Kwan Ho Lee, Douglas Ezra Morrison
10 changes: 9 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Generated by roxygen2: do not edit by hand

export(postprocess_jags_output)
export(compute_residual_metrics)
export(fig2_overall_newperson)
export(fmt_mci)
export(make_model_comparison_table)
export(model_comparison_table)
export(prep_newperson_params)
export(process_shigella_data)
importFrom(dplyr,any_of)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
Expand All @@ -13,3 +19,5 @@ importFrom(ggplot2,geom_point)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggplot2,theme_minimal)
importFrom(rlang,.data)
importFrom(rlang,ensym)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

* added `.lintr` configuration file
* Moved helper functions into `R/` subdirectory (#1)
* simplified spell-check Github Action (#3)
* simplified spell-check GitHub Action (#3)

# shigella 0.0.0

Expand Down
183 changes: 183 additions & 0 deletions R/compute_residual_metrics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#' Residual-based posterior predictive metrics for longitudinal antibody curves
#'
#' Computes residuals between observed antibody measurements and posterior
#' median predictions evaluated at the observed time points. Returns pointwise
#' residuals or aggregated error metrics (MAE, RMSE, SSE) at multiple
#' summary levels.
#'
#' @param model A data frame of posterior draws in long format with columns:
#' \code{Subject}, \code{Iso_type}, \code{Chain}, \code{Iteration},
#' \code{Parameter}, \code{value}.
#' @param dataset A \code{serodynamics} case dataset produced by
#' \code{serodynamics::as_case_data()} (must contain \code{id} and
#' \code{antigen_iso} columns, and time/value attributes).
#' @param ids Character vector of subject IDs to include (matched against
#' \code{dataset$id} and \code{model$Subject}).
#' @param antigen_iso Character scalar specifying the antigen/isotype to analyze
#' (matched against \code{dataset$antigen_iso} and \code{model$Iso_type}).
#' @param scale Scale on which to compute residuals. One of \code{"original"}
#' or \code{"log"}. If \code{"log"}, residuals are computed on the natural
#' log scale and observations/predictions \eqn{\le 0} are removed.
#' @param summary_level Level at which to summarize metrics. One of:
#' \describe{
#' \item{\code{"pointwise"}}{
#' Return pointwise residuals at each observed time point.
#' }
#' \item{\code{"id_antigen"}}{
#' Summarize by \code{id} and \code{antigen_iso}.
#' }
#' \item{\code{"antigen"}}{Summarize by \code{antigen_iso} only.}
#' \item{\code{"overall"}}{Single summary across all included observations.}
#' }
#'
#' @return A tibble. For \code{summary_level = "pointwise"}, returns
#' per-observation residuals. Otherwise returns MAE, RMSE, SSE, and
#' \code{n_obs} at the requested summary level.
#'
#' @details
#' The posterior predictive summary uses the posterior median at each observed
#' time point, with a 95\% credible interval computed from draw-level
#' predictions. Predictions are generated by
#' \code{\link{predict_posterior_at_times}}.
#'
#' @examples
#' \dontrun{
#' # Per-ID error metrics (original scale)
#' m_id <- compute_residual_metrics(
#' model = overall_sf2a,
#' dataset = dL_clean_sf2a,
#' ids = unique(dL_clean_sf2a$id),
#' antigen_iso = "IgG",
#' scale = "original",
#' summary_level = "id_antigen"
#' )
#'
#' # Pointwise residuals on log scale
#' r_pw <- compute_residual_metrics(
#' model = overall_sf2a,
#' dataset = dL_clean_sf2a,
#' ids = "SOSAR-22008",
#' antigen_iso = "IgA",
#' scale = "log",
#' summary_level = "pointwise"
#' )
#' }
#'
#' @export
compute_residual_metrics <- function(model,
dataset,
ids,
antigen_iso,
scale = c("original", "log"),
summary_level = c(
"id_antigen",
"pointwise",
"antigen",
"overall"
)) {

scale <- match.arg(scale)
summary_level <- match.arg(summary_level)

time_var <- get_timeindays_var(dataset)
value_var <- serocalculator::get_values_var(dataset)
Comment thread
Kwan-Jenny marked this conversation as resolved.
Comment thread
Kwan-Jenny marked this conversation as resolved.

observed_data <- dataset |>
dplyr::rename(
t = !!rlang::sym(time_var),
obs = !!rlang::sym(value_var)
) |>
Comment thread
Kwan-Jenny marked this conversation as resolved.
dplyr::select(dplyr::all_of(c("id", "t", "obs", "antigen_iso"))) |>
dplyr::mutate(id = as.character(.data$id)) |>
dplyr::filter(.data$id %in% ids, .data$antigen_iso == antigen_iso)

if (nrow(observed_data) == 0) {
cli::cli_abort(
"No observed data found for the specified IDs and antigen_iso."
)
}

obs_times <- sort(unique(observed_data$t))

predictions_all <- predict_posterior_at_times(
model = model,
ids = ids,
antigen_iso = antigen_iso,
times = obs_times
)

pred_summary <- predictions_all |>
dplyr::summarise(
.by = c("id", "t"),
pred_med = stats::median(.data$res, na.rm = TRUE),
pred_lower = stats::quantile(.data$res, probs = 0.025, na.rm = TRUE),
pred_upper = stats::quantile(.data$res, probs = 0.975, na.rm = TRUE)
) |>
dplyr::mutate(id = as.character(.data$id))

residual_data <- observed_data |>
dplyr::inner_join(pred_summary, by = c("id", "t"))

Comment thread
Kwan-Jenny marked this conversation as resolved.
if (scale == "log") {
residual_data <- residual_data |>
dplyr::filter(.data$obs > 0, .data$pred_med > 0) |>
dplyr::mutate(
residual = log(.data$obs) - log(.data$pred_med),
abs_residual = abs(.data$residual),
sq_residual = .data$residual^2
)
} else {
residual_data <- residual_data |>
dplyr::mutate(
residual = .data$obs - .data$pred_med,
abs_residual = abs(.data$residual),
sq_residual = .data$residual^2
)
}

if (summary_level == "pointwise") {
return(residual_data |>
dplyr::select(
"id",
"antigen_iso",
"t",
"obs",
"pred_med",
"pred_lower",
"pred_upper",
"residual",
"abs_residual",
"sq_residual"
))
}

if (summary_level == "id_antigen") {
return(residual_data |>
dplyr::summarise(
.by = c("id", "antigen_iso"),
MAE = mean(.data$abs_residual, na.rm = TRUE),
RMSE = sqrt(mean(.data$sq_residual, na.rm = TRUE)),
SSE = sum(.data$sq_residual, na.rm = TRUE),
n_obs = dplyr::n()
))
}

if (summary_level == "antigen") {
return(residual_data |>
dplyr::summarise(
.by = "antigen_iso",
MAE = mean(.data$abs_residual, na.rm = TRUE),
RMSE = sqrt(mean(.data$sq_residual, na.rm = TRUE)),
SSE = sum(.data$sq_residual, na.rm = TRUE),
n_obs = dplyr::n()
))
}

residual_data |>
dplyr::summarise(
MAE = mean(.data$abs_residual, na.rm = TRUE),
RMSE = sqrt(mean(.data$sq_residual, na.rm = TRUE)),
SSE = sum(.data$sq_residual, na.rm = TRUE),
n_obs = dplyr::n()
)
}
Loading
Loading