Skip to content

Commit f543317

Browse files
refactor(plot_serocurve): apply review polish
- Add empty-result guard for newperson param_source that errors with an actionable message when no newperson rows match the requested antigen- isotype/stratification, instead of silently producing an empty plot. - Clamp the tx time grid to xlim when supplied, avoiding unnecessary computation outside the visible range. - Lift duplicated antigen_iso_col assignment above the if/else. - Document the antigen_iso/strat default-source asymmetry (defaults come from the subject-level draws while population samples come from the population_params attribute). - Move plot_predicted_curve into the "Visualize model results" pkgdown reference section so plot_serocurve is no longer an orphaned entry. Co-authored-by: Samuel Hisaji Schildhauer <sschildhauer@users.noreply.github.com>
1 parent 54d5fe7 commit f543317

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

R/plot_serocurve.R

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
#'
99
#' @param model An `sr_model` object returned by [run_mod()].
1010
#' @param antigen_iso A [character] vector of antigen-isotype combinations to
11-
#' plot. Defaults to all antigen-isotypes present in `model`.
11+
#' plot. Defaults to all antigen-isotypes present in the subject-level
12+
#' draws of `model` (`model$Iso_type`); in normal usage these match the
13+
#' levels available in `attr(model, "population_params")`.
1214
#' @param strat A [character] vector of stratification levels to include.
13-
#' Defaults to all stratification levels present in `model`.
15+
#' Defaults to all stratification levels present in the subject-level
16+
#' draws of `model` (`model$Stratification`); in normal usage these match
17+
#' the levels available in `attr(model, "population_params")`.
1418
#' @param param_source [character]; which posterior samples to use for the
1519
#' curve. Options:
1620
#' - `"population"` (default): uses population-level `mu.par` samples stored
@@ -54,6 +58,8 @@ plot_serocurve <- function(
5458

5559
param_source <- match.arg(param_source, c("population", "newperson"))
5660

61+
antigen_iso_col <- "Iso_type"
62+
5763
# ---- Retrieve posterior samples of curve parameters --------------------
5864
if (param_source == "population") {
5965
pop_params <- attr(model, "population_params")
@@ -104,16 +110,32 @@ plot_serocurve <- function(
104110
Iso_type = factor(.data$Iso_type),
105111
Stratification = factor(.data$Stratification)
106112
)
107-
antigen_iso_col <- "Iso_type"
108113
} else {
109114
# "newperson": predictive distribution for a new individual drawn from
110115
# the population-level prior
111-
param_samples <- model |>
116+
newperson_rows <- model |>
112117
dplyr::filter(
113118
.data$Subject == "newperson",
114119
.data$Iso_type %in% .env$antigen_iso,
115120
.data$Stratification %in% .env$strat
116-
) |>
121+
)
122+
123+
if (nrow(newperson_rows) == 0) {
124+
cli::cli_abort(
125+
c(
126+
paste0(
127+
"No {.val newperson} subject found in {.arg model} for the ",
128+
"requested {.arg antigen_iso}/{.arg strat}."
129+
),
130+
"i" = paste0(
131+
"Ensure the model was fit with a {.val newperson} subject ",
132+
"included."
133+
)
134+
)
135+
)
136+
}
137+
138+
param_samples <- newperson_rows |>
117139
dplyr::select(
118140
all_of(
119141
c("Chain", "Iteration", "Parameter", "Iso_type", "Stratification",
@@ -128,11 +150,16 @@ plot_serocurve <- function(
128150
Iso_type = factor(.data$Iso_type),
129151
Stratification = factor(.data$Stratification)
130152
)
131-
antigen_iso_col <- "Iso_type"
132153
}
133154

134155
# ---- Compute predicted curves over a grid of time points ---------------
135-
tx <- seq(0, 1200, by = 5)
156+
# Clamp the grid to `xlim` when supplied to avoid unnecessary computation
157+
# outside the visible range.
158+
if (!is.null(xlim)) {
159+
tx <- seq(xlim[1], xlim[2], by = 5)
160+
} else {
161+
tx <- seq(0, 1200, by = 5)
162+
}
136163

137164
serocourse_all <- param_samples |>
138165
dplyr::reframe(

man/plot_serocurve.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgdown/_pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ reference:
3535
- plot_jags_Rhat
3636
- plot_jags_trace
3737
- plot_jags_effect
38-
- plot_predicted_curve
3938
- title: Visualize model results
4039
contents:
40+
- plot_predicted_curve
4141
- plot_serocurve
4242
- title: Postprocess JAGS output
4343
contents:

0 commit comments

Comments
 (0)