From c5eb2dca8afd23606684e880124910fc068b7b4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Torres=20Dom=C3=ADnguez?= <45337127+ntorresd@users.noreply.github.com> Date: Tue, 22 Aug 2023 18:09:24 -0500 Subject: [PATCH] Remove `serodata` from pre-loaded datasets of the package (#107) * remove serodata .Rdata and .Rd files * remove R/serodata.R * doc: update functions documentation replacing for in examples * doc: update serofoi logo * doc: update README and vignettes This commit changes the removed preloaded dataset `serodata` for the identical `chagas2012`. * fix: minor correction to test_modelling --- R/model_comparison.R | 18 ++--- R/modelling.R | 46 ++++++------- R/seroprevalence_data.R | 10 +-- R/visualisation.R | 109 +++++++++++++++---------------- man/extract_seromodel_summary.Rd | 12 ++-- man/fit_seromodel.Rd | 8 +-- man/get_exposure_ages.Rd | 25 +++++++ man/get_exposure_matrix.Rd | 24 +++++++ man/get_prev_expanded.Rd | 12 ++-- man/get_table_rhats.Rd | 18 ++--- man/plot_foi.Rd | 23 +++---- man/plot_info_table.Rd | 22 +++---- man/plot_rhats.Rd | 15 ++--- man/plot_seromodel.Rd | 21 +++--- man/plot_seroprev.Rd | 13 +++- man/plot_seroprev_fitted.Rd | 15 ++--- man/prepare_bin_data.Rd | 44 +++++++++++++ man/prepare_serodata.Rd | 2 + man/run_seromodel.Rd | 9 ++- tests/testthat/test_modelling.R | 60 +++++++++++++++++ 20 files changed, 323 insertions(+), 183 deletions(-) create mode 100644 man/get_exposure_ages.Rd create mode 100644 man/get_exposure_matrix.Rd create mode 100644 man/prepare_bin_data.Rd create mode 100644 tests/testthat/test_modelling.R diff --git a/R/model_comparison.R b/R/model_comparison.R index 0d4fc601..fdc152dc 100644 --- a/R/model_comparison.R +++ b/R/model_comparison.R @@ -7,18 +7,14 @@ #' @inheritParams get_foi_central_estimates #' @return rhats table #' @examples +#' \dontrun{ #' data(chagas2012) -#' serodata <- prepare_serodata(serodata = chagas2012) -#' model_constant <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant", -#' iter = 1500 -#' ) -#' cohort_ages <- get_cohort_ages(serodata) -#' get_table_rhats( -#' seromodel_object = model_constant, -#' cohort_ages = cohort_ages -#' ) +#' data_test <- prepare_serodata(serodata = chagas2012) +#' model_constant <- run_seromodel(serodata = data_test, +#' foi_model = "constant", +#' n_iters = 1500) +#' get_table_rhats(model_object = model_constant) +#' } #' @export get_table_rhats <- function(seromodel_object, cohort_ages) { diff --git a/R/modelling.R b/R/modelling.R index db95bcac..0a2aaf07 100644 --- a/R/modelling.R +++ b/R/modelling.R @@ -124,12 +124,11 @@ validate_prepared_serodata <- function(serodata) { #' the implementation of the model. For further details refer to #' [fit_seromodel]. #' @examples -#' data(chagas2012) +#' \dontrun{ #' serodata <- prepare_serodata(chagas2012) -#' run_seromodel( -#' serodata, -#' foi_model = "constant" -#' ) +#' run_seromodel (chagas2012, +#' foi_model = "constant") +#' } #' @export run_seromodel <- function( serodata, @@ -220,12 +219,12 @@ run_seromodel <- function( #' @return `seromodel_object`. `stanfit` object returned by the function #' [sampling][rstan::sampling] #' @examples +#' \dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(chagas2012) -#' seromodel_fit <- fit_seromodel( -#' serodata = serodata, -#' foi_model = "constant" -#' ) +#' seromodel_fit <- fit_seromodel(serodata = serodata, +#' foi_model = "constant") +#' } #' #' @export fit_seromodel <- function( @@ -317,9 +316,11 @@ fit_seromodel <- function( #' @return `cohort_ages`. A data.frame containing the age of each cohort #' corresponding to each birth year #' @examples +#' \dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(serodata = chagas2012, alpha = 0.05) -#' cohort_ages <- get_cohort_ages(serodata = serodata) +#' exposure_ages <- get_exposure_ages(serodata) +#' } #' @export get_cohort_ages <- function(serodata) { birth_year <- (min(serodata$birth_year):serodata$tsur[1]) @@ -341,6 +342,7 @@ get_cohort_ages <- function(serodata) { #' @return `exposure_output`. An atomic matrix containing the expositions for #' each entry of `serodata` by year. #' @examples +#' \dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(serodata = chagas2012) #' exposure_matrix <- get_exposure_matrix(serodata = serodata) @@ -429,15 +431,13 @@ get_foi_central_estimates <- function(seromodel_object, #' \item{`converged`}{convergence} #' } #' @examples +#' \dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(chagas2012) -#' seromodel_object <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant" -#' ) -#' extract_seromodel_summary(seromodel_object, -#' serodata = serodata -#' ) +#' seromodel_object <- run_seromodel(serodata = serodata, +#' foi_model = "constant") +#' extract_seromodel_summary(seromodel_object) +#' } #' @export extract_seromodel_summary <- function(seromodel_object, serodata) { @@ -503,14 +503,14 @@ extract_seromodel_summary <- function(seromodel_object, #' @return `prev_final`. The expanded prevalence data. This is used for plotting #' purposes in the `visualization` module. #' @examples +#' \dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(chagas2012) -#' seromodel_object <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant" -#' ) -#' foi <- rstan::extract(seromodel_object, "foi")[[1]] -#' get_prev_expanded(foi, serodata) +#' seromodel_object <- run_seromodel(serodata = serodata, +#' foi_model = "constant") +#' foi <- rstan::extract(seromodel_object$fit, "foi")[[1]] +#' get_prev_expanded <- function(foi, serodata) +#' } #' @export get_prev_expanded <- function(foi, serodata, diff --git a/R/seroprevalence_data.R b/R/seroprevalence_data.R index e4617aad..4d4bfe12 100644 --- a/R/seroprevalence_data.R +++ b/R/seroprevalence_data.R @@ -39,8 +39,10 @@ #' observed prevalence} #' } #' @examples +#'\dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(chagas2012) +#' } #' @export prepare_serodata <- function(serodata = serodata, alpha = 0.05) { @@ -93,11 +95,11 @@ prepare_serodata <- function(serodata = serodata, #' @inheritParams run_seromodel #' @return data set with the binomial confidence intervals #' @examples +#'\dontrun{ #' data(chagas2012) -#' serodata <- prepare_serodata(chagas2012) -#' prepare_bin_data(serodata) -#' @keywords internal -#' @noRd +#' prepare_bin_data(chagas2012) +#' } +#' @export prepare_bin_data <- function(serodata) { if (!any(colnames(serodata) == "age_mean_f")) { serodata <- serodata %>% diff --git a/R/visualisation.R b/R/visualisation.R index 3e1c79f1..714908f9 100644 --- a/R/visualisation.R +++ b/R/visualisation.R @@ -8,9 +8,16 @@ #' data of a given seroprevalence survey with its corresponding binomial #' confidence interval. #' @examples -#' data(chagas2012) -#' serodata <- prepare_serodata(chagas2012) -#' plot_seroprev(serodata, size_text = 15) +#' \dontrun{ +#' data(chagas2012) +#' serodata <- prepare_serodata(chagas2012) +#' seromodel_object <- run_seromodel( +#' serodata = serodata, +#' foi_model = "constant", +#' n_iters = 1000 +#') +#' plot_seroprev(seromodel_object, size_text = 15) +#' } #' @export plot_seroprev <- function(serodata, size_text = 6) { @@ -48,17 +55,14 @@ plot_seroprev <- function(serodata, #' @return A ggplot object containing the seropositivity-vs-age graph including #' the data, the fitted model and their corresponding confidence intervals. #' @examples +#' \dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(chagas2012) -#' seromodel_object <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant", -#' iter = 1000 -#' ) -#' plot_seroprev_fitted(seromodel_object, -#' serodata = serodata, -#' size_text = 15 -#' ) +#' seromodel_object <- run_seromodel(serodata = serodata, +#' foi_model = "constant", +#' n_iters = 1000) +#' plot_seroprev_fitted(seromodel_object, size_text = 15) +#' } #' @export plot_seroprev_fitted <- function(seromodel_object, serodata, @@ -148,19 +152,16 @@ plot_seroprev_fitted <- function(seromodel_object, #' @return A ggplot2 object containing the Force-of-infection-vs-time including #' the corresponding confidence interval. #' @examples -#' data(chagas2012) -#' serodata <- prepare_serodata(chagas2012) -#' seromodel_object <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant", -#' iter = 1000 -#' ) -#' cohort_ages <- get_cohort_ages(serodata) -#' plot_foi( -#' seromodel_object = seromodel_object, -#' cohort_ages = cohort_ages, -#' size_text = 15 -#' ) +#' \dontrun{ +#' data(chagas2012) +#' serodata <- prepare_serodata(chagas2012) +#' seromodel_object <- run_seromodel( +#' serodata = serodata, +#' foi_model = "constant", +#' n_iters = 1000 +#' ) +#' plot_foi(seromodel_object, size_text = 15) +#' } #' @export plot_foi <- function(seromodel_object, cohort_ages, @@ -266,18 +267,17 @@ plot_foi <- function(seromodel_object, #' function. #' @return The rhats-convergence plot of the selected model. #' @examples +#' \dontrun{ #' data(chagas2012) #' serodata <- prepare_serodata(chagas2012) #' seromodel_object <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant", -#' iter = 1000 -#' ) -#' cohort_ages <- get_cohort_ages(serodata = serodata) +#' serodata = serodata, +#' foi_model = "constant", +#' n_iters = 1000 +#' ) #' plot_rhats(seromodel_object, -#' cohort_ages = cohort_ages, -#' size_text = 15 -#' ) +#' size_text = 15) +#' } #' @export plot_rhats <- function(seromodel_object, cohort_ages, @@ -344,17 +344,16 @@ plot_rhats <- function(seromodel_object, #' @return A ggplot object with a vertical arrange containing the #' seropositivity, force of infection, and convergence plots. #' @examples -#' data(chagas2012) -#' serodata <- prepare_serodata(chagas2012) -#' seromodel_object <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant", -#' iter = 1000 -#' ) -#' plot_seromodel(seromodel_object, -#' serodata = serodata, -#' size_text = 15 -#' ) +#' \dontrun{ +#' data(chagas2012) +#' serodata <- prepare_serodata(chagas2012) +#' seromodel_object <- run_seromodel( +#' serodata = serodata, +#' foi_model = "constant", +#' n_iters = 1000 +#' ) +#' plot_seromodel(seromodel_object, size_text = 15) +#' } #' @export plot_seromodel <- function(seromodel_object, serodata, @@ -451,18 +450,16 @@ plot_seromodel <- function(seromodel_object, #' @param size_text Text size of the graph returned by the function #' @return p the plot for the given table #' @examples -#' serodata <- prepare_serodata(chagas2012) -#' seromodel_object <- run_seromodel( -#' serodata = serodata, -#' foi_model = "constant", -#' iter = 1000 -#' ) -#' seromodel_summary <- extract_seromodel_summary( -#' seromodel_object = seromodel_object, -#' serodata = serodata -#' ) -#' info <- t(seromodel_summary) -#' plot_info_table(info, size_text = 15) +#' \dontrun{ +#' serodata <- prepare_serodata(chagas2012) +#' seromodel_object <- run_seromodel( +#' serodata = serodata, +#' foi_model = "constant", +#' n_iters = 1000 +#' ) +#' info = t(seromodel_object$model_summary) +#' plot_info_table (info, size_text = 15) +#' } #' @export plot_info_table <- function(info, size_text) { dato <- data.frame( diff --git a/man/extract_seromodel_summary.Rd b/man/extract_seromodel_summary.Rd index 5b417bfd..0cc8db7d 100644 --- a/man/extract_seromodel_summary.Rd +++ b/man/extract_seromodel_summary.Rd @@ -51,13 +51,11 @@ convergence of the model, like the expected log pointwise predictive density \code{elpd} and its corresponding standard deviation. } \examples{ +\dontrun{ data(chagas2012) serodata <- prepare_serodata(chagas2012) -seromodel_object <- run_seromodel( - serodata = serodata, - foi_model = "constant" -) -extract_seromodel_summary(seromodel_object, - serodata = serodata -) +seromodel_object <- run_seromodel(serodata = serodata, + foi_model = "constant") +extract_seromodel_summary(seromodel_object) +} } diff --git a/man/fit_seromodel.Rd b/man/fit_seromodel.Rd index 31a911d8..b0a16f08 100644 --- a/man/fit_seromodel.Rd +++ b/man/fit_seromodel.Rd @@ -75,11 +75,11 @@ function determines whether the corresponding stan model object needs to be compiled by rstan. } \examples{ +\dontrun{ data(chagas2012) serodata <- prepare_serodata(chagas2012) -seromodel_fit <- fit_seromodel( - serodata = serodata, - foi_model = "constant" -) +seromodel_fit <- fit_seromodel(serodata = serodata, + foi_model = "constant") +} } diff --git a/man/get_exposure_ages.Rd b/man/get_exposure_ages.Rd new file mode 100644 index 00000000..3a1e2ca1 --- /dev/null +++ b/man/get_exposure_ages.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/modelling.R +\name{get_exposure_ages} +\alias{get_exposure_ages} +\title{Function that generates an atomic vector containing the corresponding exposition years of a serological survey} +\usage{ +get_exposure_ages(serodata) +} +\arguments{ +\item{serodata}{A data frame containing the data from a seroprevalence survey. This data frame must contain the year of birth for each individual (birth_year) and the time of the survey (tsur). birth_year can be constructed by means of the \link{prepare_serodata} function.} +} +\value{ +\code{exposure_ages}. An atomic vector with the numeration of the exposition years in serodata +} +\description{ +This function generates an atomic vector containing the exposition years corresponding to the specified serological survey data \code{serodata}. +The exposition years to the disease for each individual corresponds to the time from birth to the moment of the survey. +} +\examples{ +\dontrun{ +data(chagas2012) +serodata <- prepare_serodata(serodata = chagas2012, alpha = 0.05) +exposure_ages <- get_exposure_ages(serodata) +} +} diff --git a/man/get_exposure_matrix.Rd b/man/get_exposure_matrix.Rd new file mode 100644 index 00000000..b92816f5 --- /dev/null +++ b/man/get_exposure_matrix.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/modelling.R +\name{get_exposure_matrix} +\alias{get_exposure_matrix} +\title{Function that generates the exposure matrix corresponding to a serological survey} +\usage{ +get_exposure_matrix(serodata) +} +\arguments{ +\item{serodata}{A data frame containing the data from a seroprevalence survey. This data frame must contain the year of birth for each individual (birth_year) and the time of the survey (tsur). birth_year can be constructed by means of the \link{prepare_serodata} function.} +} +\value{ +\code{exposure_output}. An atomic matrix containing the expositions for each entry of \code{serodata} by year. +} +\description{ +Function that generates the exposure matrix corresponding to a serological survey +} +\examples{ +\dontrun{ +data(chagas2012) +serodata <- prepare_serodata(serodata = chagas2012) +exposure_matrix <- get_exposure_matrix(serodata = serodata) +} +} diff --git a/man/get_prev_expanded.Rd b/man/get_prev_expanded.Rd index 7d0cc34e..cdc84431 100644 --- a/man/get_prev_expanded.Rd +++ b/man/get_prev_expanded.Rd @@ -50,12 +50,12 @@ the obtained prevalence based on a fitting of the Force-of-Infection \code{foi} for plotting an analysis purposes. } \examples{ +\dontrun{ data(chagas2012) serodata <- prepare_serodata(chagas2012) -seromodel_object <- run_seromodel( - serodata = serodata, - foi_model = "constant" -) -foi <- rstan::extract(seromodel_object, "foi")[[1]] -get_prev_expanded(foi, serodata) +seromodel_object <- run_seromodel(serodata = serodata, + foi_model = "constant") +foi <- rstan::extract(seromodel_object$fit, "foi")[[1]] +get_prev_expanded <- function(foi, serodata) +} } diff --git a/man/get_table_rhats.Rd b/man/get_table_rhats.Rd index a5c73615..2a59fcb8 100644 --- a/man/get_table_rhats.Rd +++ b/man/get_table_rhats.Rd @@ -23,16 +23,12 @@ R-hat estimates of the serological model object \code{seromodel_object} and returns a table a dataframe with the estimates for each year of birth. } \examples{ +\dontrun{ data(chagas2012) -serodata <- prepare_serodata(serodata = chagas2012) -model_constant <- run_seromodel( - serodata = serodata, - foi_model = "constant", - iter = 1500 -) -cohort_ages <- get_cohort_ages(serodata) -get_table_rhats( - seromodel_object = model_constant, - cohort_ages = cohort_ages -) +data_test <- prepare_serodata(serodata = chagas2012) +model_constant <- run_seromodel(serodata = data_test, + foi_model = "constant", + n_iters = 1500) +get_table_rhats(model_object = model_constant) +} } diff --git a/man/plot_foi.Rd b/man/plot_foi.Rd index de0dafb8..d6653d59 100644 --- a/man/plot_foi.Rd +++ b/man/plot_foi.Rd @@ -38,17 +38,14 @@ confidence interval. The x axis corresponds to the decades covered by the survey the y axis to the Force-of-Infection. } \examples{ -data(chagas2012) -serodata <- prepare_serodata(chagas2012) -seromodel_object <- run_seromodel( - serodata = serodata, - foi_model = "constant", - iter = 1000 -) -cohort_ages <- get_cohort_ages(serodata) -plot_foi( - seromodel_object = seromodel_object, - cohort_ages = cohort_ages, - size_text = 15 -) +\dontrun{ + data(chagas2012) + serodata <- prepare_serodata(chagas2012) + seromodel_object <- run_seromodel( + serodata = serodata, + foi_model = "constant", + n_iters = 1000 + ) +plot_foi(seromodel_object, size_text = 15) +} } diff --git a/man/plot_info_table.Rd b/man/plot_info_table.Rd index da98d7a9..1ec2cd73 100644 --- a/man/plot_info_table.Rd +++ b/man/plot_info_table.Rd @@ -18,16 +18,14 @@ p the plot for the given table Function that generates a plot for a given table } \examples{ -serodata <- prepare_serodata(chagas2012) -seromodel_object <- run_seromodel( - serodata = serodata, - foi_model = "constant", - iter = 1000 -) -seromodel_summary <- extract_seromodel_summary( - seromodel_object = seromodel_object, - serodata = serodata -) -info <- t(seromodel_summary) -plot_info_table(info, size_text = 15) +\dontrun{ + serodata <- prepare_serodata(chagas2012) + seromodel_object <- run_seromodel( + serodata = serodata, + foi_model = "constant", + n_iters = 1000 + ) +info = t(seromodel_object$model_summary) +plot_info_table (info, size_text = 15) +} } diff --git a/man/plot_rhats.Rd b/man/plot_rhats.Rd index 0afc1b42..a5d730f5 100644 --- a/man/plot_rhats.Rd +++ b/man/plot_rhats.Rd @@ -28,16 +28,15 @@ rhats. All rhats must be smaller than 1 to ensure convergence (for further details check \link[bayesplot:bayesplot-extractors]{rhat}). } \examples{ +\dontrun{ data(chagas2012) serodata <- prepare_serodata(chagas2012) seromodel_object <- run_seromodel( - serodata = serodata, - foi_model = "constant", - iter = 1000 -) -cohort_ages <- get_cohort_ages(serodata = serodata) + serodata = serodata, + foi_model = "constant", + n_iters = 1000 + ) plot_rhats(seromodel_object, - cohort_ages = cohort_ages, - size_text = 15 -) + size_text = 15) +} } diff --git a/man/plot_seromodel.Rd b/man/plot_seromodel.Rd index a8612ffa..efe2a71e 100644 --- a/man/plot_seromodel.Rd +++ b/man/plot_seromodel.Rd @@ -57,15 +57,14 @@ model, the estimated seroprevalence, the Force-of-Infection fit and the R-hat estimates plots. } \examples{ -data(chagas2012) -serodata <- prepare_serodata(chagas2012) -seromodel_object <- run_seromodel( - serodata = serodata, - foi_model = "constant", - iter = 1000 -) -plot_seromodel(seromodel_object, - serodata = serodata, - size_text = 15 -) +\dontrun{ + data(chagas2012) + serodata <- prepare_serodata(chagas2012) + seromodel_object <- run_seromodel( + serodata = serodata, + foi_model = "constant", + n_iters = 1000 + ) +plot_seromodel(seromodel_object, size_text = 15) +} } diff --git a/man/plot_seroprev.Rd b/man/plot_seroprev.Rd index 9fce0e9a..847fe885 100644 --- a/man/plot_seroprev.Rd +++ b/man/plot_seroprev.Rd @@ -39,7 +39,14 @@ Function that generates the seropositivity plot from a raw serological survey dataset } \examples{ -data(chagas2012) -serodata <- prepare_serodata(chagas2012) -plot_seroprev(serodata, size_text = 15) +\dontrun{ + data(chagas2012) + serodata <- prepare_serodata(chagas2012) + seromodel_object <- run_seromodel( + serodata = serodata, + foi_model = "constant", + n_iters = 1000 +) +plot_seroprev(seromodel_object, size_text = 15) +} } diff --git a/man/plot_seroprev_fitted.Rd b/man/plot_seroprev_fitted.Rd index e386f185..2cff8f8c 100644 --- a/man/plot_seroprev_fitted.Rd +++ b/man/plot_seroprev_fitted.Rd @@ -50,15 +50,12 @@ obtained fitting from the model implementation. Age is located on the x axis and seropositivity on the y axis with its corresponding confidence interval. } \examples{ +\dontrun{ data(chagas2012) serodata <- prepare_serodata(chagas2012) -seromodel_object <- run_seromodel( - serodata = serodata, - foi_model = "constant", - iter = 1000 -) -plot_seroprev_fitted(seromodel_object, - serodata = serodata, - size_text = 15 -) +seromodel_object <- run_seromodel(serodata = serodata, + foi_model = "constant", + n_iters = 1000) +plot_seroprev_fitted(seromodel_object, size_text = 15) +} } diff --git a/man/prepare_bin_data.Rd b/man/prepare_bin_data.Rd new file mode 100644 index 00000000..bcb324e8 --- /dev/null +++ b/man/prepare_bin_data.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/seroprevalence_data.R +\name{prepare_bin_data} +\alias{prepare_bin_data} +\title{Function that prepares a pre-processed serological survey dataset to plot the binomial confidence intervals of the seroprevalence grouped by +age group} +\usage{ +prepare_bin_data(serodata) +} +\arguments{ +\item{serodata}{A data frame containing the data from a seroprevalence survey. For more information see the function \link{run_seromodel}. +This data frame must contain the following columns: +\tabular{ll}{ +\code{survey} \tab survey Label of the current survey \cr \tab \cr +\code{total} \tab Number of samples for each age group\cr \tab \cr +\code{counts} \tab Number of positive samples for each age group\cr \tab \cr +\code{age_min} \tab age_min \cr \tab \cr +\code{age_max} \tab age_max \cr \tab \cr +\code{tsur} \tab Year in which the survey took place \cr \tab \cr +\code{country} \tab The country where the survey took place \cr \tab \cr +\code{test} \tab The type of test taken \cr \tab \cr +\code{antibody} \tab antibody \cr \tab \cr +\code{age_mean_f} \tab Floor value of the average between age_min and age_max \cr \tab \cr +\code{sample_size} \tab The size of the sample \cr \tab \cr +\code{birth_year} \tab The year in which the individuals of each age group were bornt \cr \tab \cr +\code{prev_obs} \tab Observed prevalence \cr \tab \cr +\code{prev_obs_lower} \tab Lower limit of the confidence interval for the observed prevalence \cr \tab \cr +\code{prev_obs_upper} \tab Upper limit of the confidence interval for the observed prevalence \cr \tab \cr +} +The last six colums can be added to \code{serodata} by means of the function \code{\link{prepare_serodata}}.} +} +\value{ +data set with the binomial confidence intervals +} +\description{ +This function prepapares a given pre-processed serological dataset (see \code{\link{prepare_serodata}}) to plot the binomial confidence intervals +of its corresponding seroprevalence grouped by age group. +} +\examples{ +\dontrun{ +data(chagas2012) +prepare_bin_data(chagas2012) +} +} diff --git a/man/prepare_serodata.Rd b/man/prepare_serodata.Rd index eccb3737..e6bc5b5a 100644 --- a/man/prepare_serodata.Rd +++ b/man/prepare_serodata.Rd @@ -49,6 +49,8 @@ This function adds the necessary additional variables to the given dataset \code{serodata} corresponding to a serological survey. } \examples{ +\dontrun{ data(chagas2012) serodata <- prepare_serodata(chagas2012) } +} diff --git a/man/run_seromodel.Rd b/man/run_seromodel.Rd index e5513b21..bdd33fe5 100644 --- a/man/run_seromodel.Rd +++ b/man/run_seromodel.Rd @@ -79,10 +79,9 @@ using the data from a seroprevalence survey \code{serodata} as the input data. S \link{fit_seromodel} for further details. } \examples{ -data(chagas2012) +\dontrun{ serodata <- prepare_serodata(chagas2012) -run_seromodel( - serodata, - foi_model = "constant" -) +run_seromodel (chagas2012, + foi_model = "constant") +} } diff --git a/tests/testthat/test_modelling.R b/tests/testthat/test_modelling.R new file mode 100644 index 00000000..edea5876 --- /dev/null +++ b/tests/testthat/test_modelling.R @@ -0,0 +1,60 @@ +test_that("individual models", { + # So far we are skipping tests on these platforms until + # we find an efficient way to update rstan testthat snapshots on all of them + + skip_on_os(c("windows", "mac")) + source("testing_utils.R") + set.seed(1234) # For reproducibility + + library(devtools) + library(vdiffr) + + #----- Read and prepare data + data(chagas2012) + serodata <- prepare_serodata(chagas2012, alpha = 0.05) + + data_constant_path <- testthat::test_path("extdata", "prev_expanded_constant.RDS") + data_tv_normal_path <- testthat::test_path("extdata", "prev_expanded_tv_normal.RDS") + data_tv_normal_log_path <- testthat::test_path("extdata", "prev_expanded_tv_normal_log.RDS") + + prev_expanded_tv_normal_log <- readRDS(data_constant_path) + + #----- Test for the constant model + + model_name <- "constant" + model_object <- run_seromodel(serodata = serodata, + foi_model = model_name, + n_iters = 1000, + print_summary = FALSE) + + foi <- rstan::extract(model_object$fit, "foi", inc_warmup = FALSE)[[1]] + prev_expanded <- get_prev_expanded(foi, serodata = model_object$serodata) + prev_expanded_constant <- readRDS(data_constant_path) + + testthat::expect_equal(prev_expanded, prev_expanded_constant, tolerance = TRUE) + + #----- Test for the tv_normal model + + model_name <- "tv_normal" + model_object <- run_seromodel(serodata = serodata, + foi_model = model_name, + n_iters = 1000) + + foi <- rstan::extract(model_object$fit, "foi", inc_warmup = FALSE)[[1]] + prev_expanded <- get_prev_expanded(foi, serodata = model_object$serodata) + prev_expanded_tv_normal <- readRDS(data_tv_normal_path) + testthat::expect_equal(prev_expanded, prev_expanded_tv_normal, tolerance = TRUE) + + #----- Test for the tv_normal_log model + + model_name <- "tv_normal_log" + model_object <- run_seromodel(serodata = serodata, + foi_model = model_name, + n_iters = 1000) + + foi <- rstan::extract(model_object$fit, "foi", inc_warmup = FALSE)[[1]] + prev_expanded <- get_prev_expanded(foi, serodata = model_object$serodata) + prev_expanded_tv_normal <- readRDS(data_tv_normal_path) + testthat::expect_equal(prev_expanded, prev_expanded_tv_normal_log, tolerance = TRUE) + +})