Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some model components #5

Merged
merged 107 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
6491e64
remove extra files
kaitejohnson Jun 26, 2024
342701a
add a .gitignore with R and data stuff
kaitejohnson Jun 26, 2024
4685a13
add git ignore from cfa-repo-template
kaitejohnson Jun 26, 2024
f66494a
make .github folder mirror cfa-repo-template
kaitejohnson Jun 26, 2024
d19fe05
remove attributes
kaitejohnson Jun 26, 2024
eeb3fe6
add bare bones componets of an R package
kaitejohnson Jun 26, 2024
1338587
update description
kaitejohnson Jun 26, 2024
112ae91
add description of package
kaitejohnson Jun 26, 2024
76d8574
Update README.md
kaitejohnson Jun 26, 2024
fcd6b15
fix pre-commit
kaitejohnson Jun 26, 2024
00fac5b
Merge branch 'format-as-package' of https://github.com/CDCgov/ww-infe…
kaitejohnson Jun 26, 2024
620d604
pre-commit on readme
kaitejohnson Jun 26, 2024
d8167b6
attempt to set up pkgdown
kaitejohnson Jun 26, 2024
178c1cf
correct path to deps
kaitejohnson Jun 26, 2024
9d444e8
remove call to package
kaitejohnson Jun 26, 2024
4f8ff3a
Create CODEOWNERS
kaitejohnson Jun 26, 2024
46f79c4
Create CONTRIBUTING.md
kaitejohnson Jun 26, 2024
45d949f
Create NEWS.md
kaitejohnson Jun 26, 2024
47364bd
Create SUPPORT.md
kaitejohnson Jun 26, 2024
9884e8c
add stan model and start of vignette
kaitejohnson Jun 27, 2024
70c69a5
start of generating simulated data
kaitejohnson Jun 27, 2024
d453696
add parameters, package, more documentation to gen simulated data
kaitejohnson Jun 28, 2024
513587b
add in functions for getting delay distributions
kaitejohnson Jun 28, 2024
fcad57a
add functions needed for generating simulated data
kaitejohnson Jun 28, 2024
e858a03
add simulated data as package data
kaitejohnson Jun 29, 2024
5a91ec8
add some initial pre-processing functions from other packages
kaitejohnson Jun 29, 2024
9afd74d
add functions to preprocess wastewater data
kaitejohnson Jun 30, 2024
f09fee2
add minimal hosp data preprocessing
kaitejohnson Jun 30, 2024
9f8ad5d
add internal package data for covid GI and delay
kaitejohnson Jun 30, 2024
c732701
add functions to create stan data, modify model to be more generally …
kaitejohnson Jun 30, 2024
900a5c7
simplify pre-processing in vignette
kaitejohnson Jul 1, 2024
6ead306
add draft of wrapper function to fit model
kaitejohnson Jul 1, 2024
d04ace0
add initialization functions
kaitejohnson Jul 1, 2024
e222e52
add a wrapper function around the model fitting
kaitejohnson Jul 2, 2024
0736f02
fix bug
kaitejohnson Jul 2, 2024
4f2a2d1
update gitignore to exclude stan binary and .Rproj
kaitejohnson Jul 2, 2024
3961d08
make it s.t. ww data needs a column called exclude, add preprocessing…
kaitejohnson Jul 3, 2024
d9bd1d7
add exclude column in preprocessing, make outlier flag optional step
kaitejohnson Jul 3, 2024
2a94332
add functions that set up model specification and mcmc options
kaitejohnson Jul 3, 2024
2894ae1
add wwinference function
kaitejohnson Jul 3, 2024
e6ef714
add post processing functions, realizing that I have an indexing erro…
kaitejohnson Jul 3, 2024
e8a3dfa
fix the handling of passing in col names, error was breaking LOD calling
kaitejohnson Jul 4, 2024
f5b314f
update documentation
kaitejohnson Jul 4, 2024
f558bd9
fix typo
kaitejohnson Jul 4, 2024
00c0543
adjust postprocess function, add in diagnostics
kaitejohnson Jul 5, 2024
f675dc4
add functions for quickly plotting outputs
kaitejohnson Jul 5, 2024
d272090
revert params back to current modle version
kaitejohnson Jul 5, 2024
728d83d
add plotting and diagnostics to vignette
kaitejohnson Jul 5, 2024
1028c72
fix merge conflicts with main from updating package infreastructure"
kaitejohnson Jul 5, 2024
f663ab6
fix merge conflicts with main from updating package infreastructure"
kaitejohnson Jul 5, 2024
06c8147
try running pre-commit locally
kaitejohnson Jul 5, 2024
64ab128
add dependences to DESCRIPTION
kaitejohnson Jul 5, 2024
fe88a39
add more deps
kaitejohnson Jul 5, 2024
b9c4eb8
try removing print statement
kaitejohnson Jul 5, 2024
56037c4
revert
kaitejohnson Jul 5, 2024
102da37
update vignette data
kaitejohnson Jul 5, 2024
b2d064e
fix typos
kaitejohnson Jul 5, 2024
9b3777f
remove example
kaitejohnson Jul 5, 2024
e770235
try adding bookdown to imports
kaitejohnson Jul 5, 2024
81ef6d8
fix to specify ISO8601 format for dates
kaitejohnson Jul 8, 2024
e7871a0
Update R/wwinference.R
kaitejohnson Jul 9, 2024
448af6f
Update inst/stan/functions/utils.stan
kaitejohnson Jul 9, 2024
4ee29b6
update checkers documentation, remove usesethis from imports
kaitejohnson Jul 9, 2024
10a030d
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 9, 2024
33390d4
remove additional stan model
kaitejohnson Jul 9, 2024
b751780
Exclude data-raw in dependency check
kaitejohnson Jul 9, 2024
933029b
remove print statement
kaitejohnson Jul 9, 2024
60eac88
move bookdown to suggests
kaitejohnson Jul 9, 2024
e59f421
Update R/delay_distribs.R
kaitejohnson Jul 10, 2024
00ce331
remove sgtf call
kaitejohnson Jul 10, 2024
8556a29
use to_simplex on discretized growth adjusted weibull
kaitejohnson Jul 10, 2024
965bc1f
fix description incubation period
kaitejohnson Jul 10, 2024
d28c419
use to simplex again
kaitejohnson Jul 10, 2024
307c966
revise figure description to be more general
kaitejohnson Jul 10, 2024
1265741
Update R/figures.R
kaitejohnson Jul 10, 2024
93c5355
Update R/figures.R
kaitejohnson Jul 10, 2024
64c7ad1
Update R/figures.R
kaitejohnson Jul 10, 2024
5b5d146
fix documentation and set rhat tolerance as fxn arg
kaitejohnson Jul 10, 2024
57399fc
change from p_high rhat to frac high rhat
kaitejohnson Jul 10, 2024
d56203b
rename postprocess to get_draws_df
kaitejohnson Jul 10, 2024
89b6178
Update R/preprocessing.R description
kaitejohnson Jul 10, 2024
6c45004
Update R/preprocessing.R description for hosp data
kaitejohnson Jul 10, 2024
40fa4c9
Update R/utils.R fix typo
kaitejohnson Jul 10, 2024
5919d8c
Update R/utils.R
kaitejohnson Jul 10, 2024
dd881a9
Update R/utils.R
kaitejohnson Jul 10, 2024
9144ec3
Update R/utils.R
kaitejohnson Jul 10, 2024
d2ebb91
Update R/utils.R
kaitejohnson Jul 10, 2024
5323019
Update R/utils.R
kaitejohnson Jul 10, 2024
6bc80d4
Update R/utils.R
kaitejohnson Jul 10, 2024
c408929
make col name for eval data general
kaitejohnson Jul 10, 2024
ba2cec8
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 10, 2024
8668baf
run pre-commit on all files
kaitejohnson Jul 10, 2024
cad3aa4
add col name argument to vignette
kaitejohnson Jul 10, 2024
9f8abb2
adjust R(t) figure title and y-axis label
kaitejohnson Jul 10, 2024
164fe96
fix typos in wwinference.Rmd
kaitejohnson Jul 10, 2024
8942981
Update vignettes/wwinference.Rmd
kaitejohnson Jul 10, 2024
0a61076
rename postprocess to get draws_df
kaitejohnson Jul 10, 2024
99ecb80
remove the functions needed to generate delay distribs from package f…
kaitejohnson Jul 10, 2024
2a4c7ec
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 10, 2024
f9849ba
fix documentation of eval data in figures
kaitejohnson Jul 10, 2024
bf26b3d
add documentation to functions in covid_pmf generation, remove drop f…
kaitejohnson Jul 10, 2024
a2f97a7
remove documentation of drop first and renormalize
kaitejohnson Jul 10, 2024
28f6d2d
Update data-raw/covid_pmfs.R
dylanhmorris Jul 10, 2024
d61400f
fix missed namespace call
kaitejohnson Jul 10, 2024
d0e35ad
add to change log with first PR into main
kaitejohnson Jul 10, 2024
c5b6348
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 10, 2024
1f892d4
run pre-commit
kaitejohnson Jul 10, 2024
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
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ export(get_ww_data_indices)
export(get_ww_data_sizes)
export(get_ww_values)
export(indicate_ww_exclusions)
export(make_hospital_onset_delay_pmf)
export(make_incubation_period_pmf)
export(make_reporting_delay_pmf)
export(preprocess_hosp_data)
export(preprocess_ww_data)
export(simulate_double_censored_pmf)
export(to_simplex)
export(validate_paramlist)
export(wwinference)
Expand Down
185 changes: 0 additions & 185 deletions R/delay_distribs.R
dylanhmorris marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

4 changes: 3 additions & 1 deletion R/figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#' @param draws A dataframe containing the posterior draws with the data joined
#' to it. This is the `draws_df` output of a call to [wwinference()]
#' @param count_data_eval A dataframe containing the count data we will
#' evaluate the forecasts against.
#' evaluate the forecasts against. Must contain the columns `date` and
#' a column indicating the count data to evaluate against, with the name
#' of that column specified as the `count_data_eval_col_name`
#' @param count_data_eval_col_name string indicating the name of the count
#' data to evaluate against the forecasted count data
#' @param forecast_date A string indicating the date we made the forecast, for
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,33 @@ convert_to_logsd <- function(mean, sd) {
to_simplex <- function(vector) {
return(vector / sum(vector))
}

#' Drop the first element of a simplex and re-normalize the result to sum to 1.
#'
#' When this vector corresponds to the generation interval distribution, we
#' want to drop this first bin. The renewal equation assumes that same-day
#' infection and onward transmission does not occur, and we assume
#' everything is 1 indexed not 0 indeced. We need to
#' manually drop the first element from the PMF vector.
#'
#' @param x A numeric vector, sums to 1. Corresponds to a discretized PDF or PMF
#' (usually the GI distribution).
#'
#' @return A numeric vector, sums to 1.
#' @export
#' @examples
#' pmf_orig <- c(0.1, 0.1, 0.1, 0.7)
#' pmf_trunc <- drop_first_and_renormalize(pmf_orig)
drop_first_and_renormalize <- function(x) {
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
# Check input sums to 1
stopifnot(abs(sum(x) - 1) < 1e-8)
# Drop and renormalize
y <- x[2:length(x)] / sum(x[2:length(x)])
vec_outside_tol <- abs(sum(y) - 1L) > 1e-10
# Normalize until within tolerance
while (vec_outside_tol) {
y <- y / sum(y)
vec_outside_tol <- abs(sum(y) - 1L) > 1e-10
}
return(y)
}
107 changes: 107 additions & 0 deletions data-raw/covid_pmfs.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,116 @@
# Load in the parameters
params <- get_params(
system.file("extdata", "example_params.toml",
package = "wwinference"
)
)




# Simulate daily double censored PMF. From {epinowcast}:
# https://package.epinowcast.org/dev/reference/simulate_double_censored_pmf.html #nolint
#
# This function simulates the probability mass function of a daily
# double-censored process. The process involves two distributions: a primary
# distribution which represents the censoring process for the primary event
# and another distribution (which is offset by the primary).
#
# Based off of:
# https://www.medrxiv.org/content/10.1101/2024.01.12.24301247v1
simulate_double_censored_pmf <- function(
max, fun_primary = stats::runif, primary_args = list(),
fun_dist = stats::rlnorm,
dist_args = list(...), n = 1e6, ...) {
primary <- do.call(fun_primary, c(list(n), primary_args))
secondary <- primary + do.call(fun_dist, c(list(n), dist_args))
delay <- floor(secondary) - floor(primary)
if (missing(max)) {
max <- base::max(delay)
}
cdf <- ecdf(delay)(0:max)
pmf <- c(cdf[1], diff(cdf))
vec_outside_tol <- abs(sum(pmf) - 1L) > 1e-10
while (vec_outside_tol) {
pmf <- pmf / sum(pmf)
vec_outside_tol <- abs(sum(pmf) - 1L) > 1e-10
}
return(pmf)
}


# This retruns an incubation period pmf corresponding to the incubation period
# for COVID after Omicron used in
# Park et al 2023. These estimates are from early Omicron.

make_incubation_period_pmf <- function(backward_scale = 3.60,
backward_shape = 1.50,
r = 0.15) {
# From: Park, Sang Woo, et al. "Inferring the differences in incubation-period
# and generation-interval distributions of the Delta and Omicron variants of
# SARS-CoV-2." Proceedings of the National Academy of Sciences 120.22 (2023):
# e2221887120.

# "However, when we account for growth-rate differences and reestimate the
# forward incubation periods, we find that both variants have similar
# incubation-period distributions with a mean of 4.1 d (95% CI: 3.8 to 4.4 d)
# for the Delta variant and 4.2 d (95% CI: 3.6 to 4.9 d) for the Omicron
# variant Fig. 3B)."

# Fits a Weibull to the data

# Relies on fundamental assumption about epidemic growth rate.


discr_gr_adj_weibull <- tibble::tibble(
time = seq(0, 23, by = 1), # 23 seems to get most of the distribution mass
density0 = dweibull(time,
shape = backward_shape,
scale = backward_scale
) * exp(r * time)
)

inc_period_pmf <- to_simplex(discr_gr_adj_weibull$density0)
kaitejohnson marked this conversation as resolved.
Show resolved Hide resolved
return(inc_period_pmf)
}

# Makes the hospital onset delay pmf using the parameter estimates based on
# Danache et al linelist data from symptom onset to hospital
# admission. See below:
# https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0261428

make_hospital_onset_delay_pmf <- function(neg_binom_mu = 6.98665,
neg_binom_size = 2.490848) {
density <- dnbinom(
x = seq(0, 30, 1),
mu = neg_binom_mu, size = neg_binom_size
)
hosp_onset_delay_pmf <- density / sum(density)
kaitejohnson marked this conversation as resolved.
Show resolved Hide resolved

return(hosp_onset_delay_pmf)
}


# Makes the reporting delay pmf by convolving the incubation period pmf with
# the symptom to hospital admission pmf and normalizing

make_reporting_delay_pmf <- function(incubation_period_pmf,
hospital_onset_delay_pmf) {
pmfs <- list(
"incubation_period" = incubation_period_pmf,
"hosp_onset_delay" = hospital_onset_delay_pmf
)

infection_to_hosp_delay_pmf <- add_pmfs(pmfs) |>
to_simplex()
kaitejohnson marked this conversation as resolved.
Show resolved Hide resolved
return(infection_to_hosp_delay_pmf)
}





# Put it all together
generation_interval <- withr::with_seed(42, {
wwinference::simulate_double_censored_pmf(
kaitejohnson marked this conversation as resolved.
Show resolved Hide resolved
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
max = params$gt_max, meanlog = params$mu_gi,
Expand Down
2 changes: 1 addition & 1 deletion man/drop_first_and_renormalize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_draws_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/get_plot_forecasted_counts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading