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

Simulation functions and vignette plus other things #199

Merged
merged 43 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e4d7f01
#169 move probability functionality
ben18785 May 15, 2024
e3391df
#169 corrections to exposure matrix and simulating time models first …
ben18785 May 15, 2024
d33282c
fix(i169): minor corrections to simulation functionalities
ntorresd May 15, 2024
e577a40
#169 chatgpt gave a useful tip about refactorising
ben18785 May 15, 2024
1f14a31
#169 removed redundant functions
ben18785 May 15, 2024
0ad6e1b
#169 added all tests for simulate_serosurvey_time_model
ben18785 May 15, 2024
a480a7c
#169 started to include docstrings
ben18785 May 16, 2024
f1fa164
#169 added further docstrings and more tests incl seroreversion
ben18785 May 16, 2024
2ccf6ee
#169 first draft of time-varying foi model with comprehensive unit tests
ben18785 May 16, 2024
338c6d1
#169 started to add in age-varying simulation but there's an error wi…
ben18785 May 16, 2024
d4a3f37
#169 tests corrected for probability exact calcs
ben18785 May 16, 2024
2c31fb8
#169 first draft of age-varying simulation model with full tests
ben18785 May 16, 2024
b15dae2
#169 added master simulate function. Bug for sample_size_by_individua…
ben18785 May 16, 2024
618ea70
added tests for master simulate function
ben18785 May 16, 2024
30cef67
#169 corrected probability_exact_time_varying function (which now doe…
ben18785 May 16, 2024
bc5c705
#169 removed redundant age-varying probability calculator
ben18785 May 16, 2024
260d6bd
#169 added validation functions for serosurveys
ben18785 May 16, 2024
ae2283a
fix(i169): correct age binning for data simulation function
ntorresd May 16, 2024
fb24513
test(i169): adds test draft for time-age model
ntorresd May 16, 2024
445baa1
#169 corrected mistake which meant (previously) age and time model se…
ben18785 May 16, 2024
2315e0b
Update simulate_serosurvey.R
ben18785 May 16, 2024
3ce4bbf
#169 working draft of age and time model
ben18785 May 16, 2024
29981ec
#169 now passing all tests for simulate_serosurvey
ben18785 May 16, 2024
af6633b
#169 added tidyr calls to expand_grid
ben18785 May 16, 2024
8bb65e5
#169 corrected partial argument match
ben18785 May 16, 2024
3c3991a
i169: remove old data simulation functions
ntorresd May 16, 2024
ef6b796
#169 added validation error for simulation
ben18785 May 16, 2024
c5b9f91
test(i169): remove old data simulation functions tests
ntorresd May 17, 2024
7a154f9
update NAMESPACE
ntorresd May 17, 2024
3622401
fix: minor correction to `simulate_serosurvey` foi specification
ntorresd May 22, 2024
78a3cb8
fix: minor correction to `get_cohort_ages`
ntorresd May 22, 2024
3fbd48c
fix: corrections to `plot_foi` option to plot foi used to simulate
ntorresd May 22, 2024
970d115
test: add value based tests for `fit_seromodel` using simulated data
ntorresd May 22, 2024
903f6ef
first draft of simulation vignette
ben18785 Jul 19, 2024
e0265b3
now runs ok
ben18785 Jul 19, 2024
3a2e833
made vignette nicer
ben18785 Jul 21, 2024
9e700fc
started to add more general model
ben18785 Jul 22, 2024
37ab1ac
added in general simulation functionality
ben18785 Jul 22, 2024
68153b8
doc: make sure old vignettes work as expected
ntorresd Jul 24, 2024
57fa10b
doc: syntax changes to 'simulating_serosurveys.Rmd'
ntorresd Jul 31, 2024
6923235
doc: use different names for foi data frames on each example
ntorresd Jul 31, 2024
617e4ed
doc: add analytical solution line to seroprevalence plots in 'simulat…
ntorresd Jul 31, 2024
b890745
doc: changes to code chunks options
ntorresd Jul 31, 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
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ Imports:
ggplot2,
Hmisc,
loo,
purrr
purrr,
tidyr,
tibble,
Matrix
LinkingTo:
BH (>= 1.66.0),
Rcpp (>= 0.12.0),
Expand Down
11 changes: 7 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

export(extract_seromodel_summary)
export(fit_seromodel)
export(generate_sim_data)
export(get_chunk_structure)
export(get_cohort_ages)
export(get_foi_central_estimates)
export(get_prev_expanded)
export(get_sim_n_seropositive)
export(get_sim_probability)
export(get_table_rhats)
export(group_sim_data)
export(plot_foi)
export(plot_info_table)
export(plot_rhats)
export(plot_seromodel)
export(plot_seroprev)
export(plot_seroprev_fitted)
export(prepare_serodata)
export(probability_seropositive_by_age)
export(probability_seropositive_general_model_by_age)
export(run_seromodel)
export(simulate_serosurvey)
export(simulate_serosurvey_age_and_time_model)
export(simulate_serosurvey_age_model)
export(simulate_serosurvey_general_model)
export(simulate_serosurvey_time_model)
import(Rcpp)
import(dplyr)
import(methods)
Expand Down
20 changes: 10 additions & 10 deletions R/modelling.R
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,13 @@ get_chunk_structure <- function(
#' cohort_ages <- get_cohort_ages(serodata = serodata)
#' @export
get_cohort_ages <- function(serodata) {
birth_year <- (min(serodata$birth_year):serodata$tsur[1])
age <- (seq_along(min(serodata$birth_year):(serodata$tsur[1] - 1)))

birth_year <- min(serodata$birth_year):(serodata$tsur[1] - 1)
age <- rev(seq_along(birth_year))

cohort_ages <- data.frame(
birth_year = birth_year[-length(birth_year)],
age = rev(age)
birth_year = birth_year,
age = age
)
return(cohort_ages)
}
Expand Down Expand Up @@ -507,9 +508,8 @@ get_exposure_matrix <- function(serodata) {
#' Extract central estimates for the fitted forced FoI
#'
#' @param seromodel_object Stanfit object containing the results of fitting a
#' model by means of [run_seromodel].
#' @param cohort_ages A data frame containing the age of each cohort
#' corresponding to each birth year.
#' model by means of [fit_seromodel].
#' @inheritParams fit_seromodel
#' @param lower_quantile Lower quantile used to compute the credible interval of
#' the fitted force-of-infection.
#' @param upper_quantile Lower quantile used to compute the credible interval of
Expand All @@ -525,18 +525,18 @@ get_exposure_matrix <- function(serodata) {
#' cohort_ages <- get_cohort_ages(serodata = serodata)
#' foi_central_estimates <- get_foi_central_estimates(
#' seromodel_object = seromodel_object,
#' cohort_ages = cohort_ages
#' serodata = serodata
#' )
#' @export
get_foi_central_estimates <- function(
seromodel_object,
cohort_ages,
serodata,
lower_quantile = 0.05,
upper_quantile = 0.95
) {
# extracts force-of-infection from stan fit
foi <- rstan::extract(seromodel_object, "foi", inc_warmup = FALSE)[[1]]

cohort_ages <- get_cohort_ages(serodata)
# defines time scale depending on the type of the model
if(
seromodel_object@model_name %in%
Expand Down
Loading
Loading