Skip to content

Commit c27b070

Browse files
committed
Merge commit 'd7d1eba7c81d4e4a299d6b8642acd8b419f67add'
2 parents 4971cf6 + d7d1eba commit c27b070

86 files changed

Lines changed: 18019 additions & 11324 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/check-spelling.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ jobs:
1414
runs-on: ubuntu-latest
1515
name: Spellcheck
1616
container:
17-
image: rocker/tidyverse:4.1.2
17+
image: rocker/tidyverse:latest
1818
steps:
1919
- name: Checkout repo
2020
uses: actions/checkout@v3
2121

22-
- name: Run Spelling Check test
22+
- uses: r-lib/actions/setup-r@v2
23+
with:
24+
use-public-rspm: true
25+
26+
- uses: r-lib/actions/setup-r-dependencies@v2
27+
with:
28+
packages: any::spelling
29+
30+
- name: Run Spelling Check Test
2331
uses: insightsengineering/r-spellcheck-action@v3.0.2

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Type: Package
22
Package: serocalculator
33
Title: Estimating Infection Rates from Serological Data
4-
Version: 1.3.0.9048
4+
Version: 1.3.0.9055
55
Authors@R: c(
66
person("Peter", "Teunis", , "p.teunis@emory.edu", role = c("aut", "cph"),
77
comment = "Author of the method and original code."),
88
person("Kristina", "Lai", , "kwlai@ucdavis.edu", role = c("aut", "cre")),
99
person("Chris", "Orwa", role = "aut"),
10+
person("Kwan Ho", "Lee", , "ksjlee@ucdavis.edu", role = "ctb"),
1011
person("Kristen", "Aiemjoy", , "kaiemjoy@ucdavis.edu", role = "aut"),
1112
person("Douglas Ezra", "Morrison", , "demorrison@ucdavis.edu", role = "aut")
1213
)

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ S3method(autoplot,curve_params)
55
S3method(autoplot,pop_data)
66
S3method(autoplot,seroincidence)
77
S3method(autoplot,seroincidence.by)
8+
S3method(autoplot,sim_results)
89
S3method(autoplot,summary.seroincidence.by)
910
S3method(print,seroincidence)
1011
S3method(print,seroincidence.by)
@@ -14,6 +15,7 @@ S3method(strata,default)
1415
S3method(summary,pop_data)
1516
S3method(summary,seroincidence)
1617
S3method(summary,seroincidence.by)
18+
export(analyze_sims)
1719
export(as_curve_params)
1820
export(as_noise_params)
1921
export(as_pop_data)
@@ -120,6 +122,7 @@ importFrom(tidyselect,contains)
120122
importFrom(tidyselect,ends_with)
121123
importFrom(utils,capture.output)
122124
importFrom(utils,download.file)
125+
importFrom(utils,head)
123126
importFrom(utils,tail)
124127
importFrom(utils,unzip)
125128
useDynLib(serocalculator, .registration = TRUE)

NEWS.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
## New features
44

5+
* Made `graph.curve.params()` the default sub-method for `autoplot.curve_params()` (#450)
6+
* Added `log_x` and `log_y` options to `graph.curve.params()` sub-method for
7+
`autoplot.curve_params()` (#453)
8+
* Extended `sim_pop_data_multi()` to loop over multiple sample sizes (#444)
9+
* Added new functions `analyze_sims()` and `autoplot.sim_results()` (#444)
510
* Rename `estimate_scr()` to `est_seroincidence_by()` (#439)
611
* Rename `estimate_scr()` to `est_seroincidence()` (#432)
7-
* Rename argument `curve_params` to `sr_params` (#424)
12+
* Rename argument `curve_params` to `sr_params` for estimation functions (#424)
813
* added documentation for `count_strata()` (#431)
914
* Rename `as_curve_params()` to `as_sr_params()` (#421)
1015
* Rename `load_curve_params()` to `load_sr_params()` (#421)

R/ab1.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# uses r > 1 scale for shape
2+
ab1 <- function(t, y0, y1, t1, alpha, shape) {
3+
beta <- bt(y0, y1, t1)
4+
5+
yt <- 0
6+
7+
if (t <= t1) {
8+
yt <- y0 * exp(beta * t)
9+
}
10+
11+
if (t > t1) {
12+
yt <- (y1^(1 - shape) - (1 - shape) * alpha * (t - t1))^(1 / (1 - shape))
13+
}
14+
15+
return(yt)
16+
}

R/analyze_sims.R

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#' Analyze simulation results
2+
#'
3+
#' @param data a [tibble::tbl_df] with columns:
4+
#' * `lambda.sim`,
5+
#' * `incidence.rate`,
6+
#' * `SE`,
7+
#' * `CI.lwr`,
8+
#' * `CI.upr`
9+
#' for example, as produced by [summary.seroincidence.by()] with
10+
#' `lambda.sim` as a stratifying variable
11+
#'
12+
#' @returns a `sim_results` object (extends [tibble::tbl_df])
13+
#' @export
14+
#'
15+
#' @example inst/examples/exm-analyze_sims.R
16+
#'
17+
analyze_sims <- function(
18+
data) {
19+
20+
to_return <-
21+
data |>
22+
split(
23+
f = ~ sample_size + lambda.sim
24+
) |>
25+
lapply(FUN = analyze_sims_one_stratum) |>
26+
bind_rows()
27+
28+
class(to_return) <- union("sim_results", class(to_return))
29+
30+
return(to_return)
31+
}
32+
33+
analyze_sims_one_stratum <- function(
34+
data,
35+
true_lambda = data$lambda.sim,
36+
sample_size = data$sample_size) {
37+
38+
# Filter out rows where CI.lwr or CI.upr is Inf or NaN
39+
data <- data |>
40+
filter(is.finite(.data$CI.lwr) & is.finite(.data$CI.upr))
41+
42+
# Compute Bias
43+
bias <- mean(data$incidence.rate - true_lambda, na.rm = TRUE)
44+
45+
# Standard Error (Mean of reported standard errors)
46+
standard_error <- mean(data$SE, na.rm = TRUE)
47+
48+
# RMSE (Root Mean Square Error)
49+
rmse <- mean((data$incidence.rate - true_lambda)^2, na.rm = TRUE) |> sqrt()
50+
51+
# Confidence Interval Width (Mean of Upper - Lower bounds, without Inf values)
52+
ci_width <- mean(data$CI.upr - data$CI.lwr, na.rm = TRUE)
53+
54+
coverage_prop <-
55+
mean(data$CI.lwr <= true_lambda & data$CI.upr >= true_lambda, na.rm = TRUE)
56+
57+
to_return <- tibble(
58+
lambda.sim = mean(true_lambda),
59+
sample_size = mean(sample_size),
60+
Bias = bias,
61+
Mean_Est_SE = standard_error,
62+
Empirical_SE = stats::sd(data$incidence.rate, na.rm = TRUE),
63+
RMSE = rmse,
64+
Mean_CI_Width = ci_width,
65+
CI_Coverage = coverage_prop
66+
)
67+
# Return computed statistics as a list
68+
return(to_return)
69+
}

R/as_sr_params.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ as_sr_params <- function(data, antigen_isos = NULL) {
6767
))
6868
}
6969

70+
# if `object` lacks an `iter` column, add it:
71+
if (!is.element("iter", names(curve_data))) {
72+
cli::cli_warn(
73+
c(
74+
"`data` is missing `iter` column",
75+
"It will be inferred from row ordering."
76+
)
77+
)
78+
curve_data <-
79+
curve_data |>
80+
mutate(
81+
.by = any_of(c("antigen_iso", "chain")),
82+
iter = row_number()
83+
)
84+
}
85+
7086
# assign antigen attribute
7187
attr(curve_data, "antigen_isos") <- antigen_isos
7288

R/autoplot.curve_params.R

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
#' graph antibody decay curves by antigen isotype
1+
#' Graph antibody decay curves by antigen isotype
2+
#' @param object
3+
#' a `curve_params` object (constructed using [as_sr_params()]), which is
4+
#' a [data.frame()] containing MCMC samples of antibody decay curve parameters
5+
#' @param method a [character] string indicating whether to use
6+
#' - [graph.curve.params()] (default) or
7+
#' - [graph_seroresponse_model_1()] (previous default)
8+
#' as the graphing method.
29
#'
3-
#' @inheritParams graph_seroresponse_model_1
4-
#' @inheritDotParams graph_seroresponse_model_1
5-
#' @param antigen_isos antigen isotypes to analyze
6-
#' (can subset `curve_params`)
10+
#' @param ... additional arguments passed to the sub-function
11+
#' indicated by the `method` argument.
12+
#' @details
13+
#' Currently, the backend for this method is [graph.curve.params()].
14+
#' Previously, the backend for this method was [graph_seroresponse_model_1()].
15+
#' That function is still available if preferred.
716
#' @return a [ggplot2::ggplot()] object
817
#' @export
918
#' @examples
@@ -23,14 +32,12 @@
2332
#' }
2433
autoplot.curve_params <- function(
2534
object,
26-
antigen_isos = unique(object$antigen_iso),
35+
method = c("graph.curve.params", "graph_seroresponse_model_1"),
2736
...) {
2837

2938
# spaghettified in order to swap out implementations with minimal
3039
# disruption to API
31-
object |>
32-
graph_seroresponse_model_1(
33-
antigen_isos = antigen_isos,
34-
...
35-
)
40+
method <- match.arg(method)
41+
cur_function <- match.fun(method)
42+
object |> cur_function(...)
3643
}

R/autoplot.seroincidence.by.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#' est2 <- est_seroincidence_by(
2525
#' strata = c("catchment"),
2626
#' pop_data = xs_data,
27-
#' curve_params = curve,
27+
#' sr_params = curve,
2828
#' curve_strata_varnames= NULL,
2929
#' noise_strata_varnames = NULL,
3030
#' noise_params = noise,

R/autoplot.sim_results.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' Plot simulation results
2+
#' `autoplot()` method for `sim_results` objects
3+
#'
4+
#' @param object a `sim_results` object (from [analyze_sims()])
5+
#' @param statistic which column of `object` should be the y-axis?
6+
#' @param ... unused
7+
#' @returns a [ggplot2::ggplot]
8+
#' @export
9+
#'
10+
#' @example inst/examples/exm-autoplot.sim_results.R
11+
autoplot.sim_results <- function(
12+
object,
13+
statistic = "Empirical_SE",
14+
...) {
15+
object |>
16+
dplyr::mutate(lambda.sim = factor(.data$lambda.sim)) |>
17+
ggplot2::ggplot() +
18+
ggplot2::aes(
19+
x = .data$sample_size,
20+
group = .data$lambda.sim,
21+
col = .data$lambda.sim,
22+
y = .data[[statistic]]
23+
) +
24+
ggplot2::geom_point() +
25+
ggplot2::geom_line() +
26+
ggplot2::theme(legend.position = "bottom")
27+
}

0 commit comments

Comments
 (0)