Skip to content

Commit

Permalink
Add input validation (#92)
Browse files Browse the repository at this point in the history
* Added argument matching in fit_seromodel()

* Added allowed options to arg foi_model

* Added input validation to prepare_serodata()

* Fixed the check for required columns

* Regenerated the docs

---------

Co-authored-by: ntorresd <[email protected]>
  • Loading branch information
jamesmbaazam and ntorresd authored Oct 5, 2023
1 parent 6c32c49 commit 6afc7fc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ LazyData: true
RoxygenNote: 7.2.3
Depends:
R (>= 3.5.0)
Imports:
Imports:
checkmate,
methods,
Rcpp (>= 0.12.0),
RcppParallel (>= 5.0.1),
Expand Down
8 changes: 6 additions & 2 deletions R/modelling.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
#' foi_model = "constant")
#' @export
run_seromodel <- function(serodata,
foi_model = "constant",
foi_model = c("constant", "tv_normal_log",
"tv_normal"),
n_iters = 1000,
n_thin = 2,
delta = 0.90,
m_treed = 10,
decades = 0,
print_summary = TRUE) {
foi_model <- match.arg(foi_model)
survey <- unique(serodata$survey)
if (length(survey) > 1) warning("You have more than 1 surveys or survey codes")
seromodel_object <- fit_seromodel(serodata = serodata,
Expand Down Expand Up @@ -102,13 +104,15 @@ run_seromodel <- function(serodata,
#'
#' @export
fit_seromodel <- function(serodata,
foi_model,
foi_model = c("constant", "tv_normal_log",
"tv_normal"),
n_iters = 1000,
n_thin = 2,
delta = 0.90,
m_treed = 10,
decades = 0) {
# TODO Add a warning because there are exceptions where a minimal amount of iterations is needed
foi_model <- match.arg(foi_model)
model <- stanmodels[[foi_model]]
cohort_ages <- get_cohort_ages(serodata = serodata)
exposure_matrix <- get_exposure_matrix(serodata)
Expand Down
11 changes: 11 additions & 0 deletions R/seroprevalence_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@
#' @export
prepare_serodata <- function(serodata = serodata,
alpha = 0.05) {
checkmate::assert_numeric(alpha, lower = 0, upper = 1)
#Check that serodata has the right columns
stopifnot("serodata must contain the right columns" =
all(c("survey", "total", "counts", "age_min", "age_max", "tsur",
"country","test","antibody"
) %in%
colnames(serodata)
)
)
if(!any(colnames(serodata) == "age_mean_f")){
serodata <- serodata %>%
dplyr::mutate(age_mean_f = floor((age_min + age_max) / 2), sample_size = sum(total))
}

if(!any(colnames(serodata) == "birth_year")){
serodata <- serodata %>%
dplyr::mutate(birth_year = .data$tsur - .data$age_mean_f)
}

serodata <- serodata %>%
cbind(
Hmisc::binconf(
Expand Down
2 changes: 1 addition & 1 deletion man/fit_seromodel.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/run_seromodel.Rd

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

0 comments on commit 6afc7fc

Please sign in to comment.