From ea6093f9e46b376994f6a4082443ecba6fbc398f Mon Sep 17 00:00:00 2001 From: ntorresd Date: Wed, 31 Jul 2024 18:42:37 -0500 Subject: [PATCH] refac: move data simulation validation functions to validation module --- R/simulate_serosurvey.R | 49 ----------------------------------------- R/validation.R | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/R/simulate_serosurvey.R b/R/simulate_serosurvey.R index cc6f7afb..934e5441 100644 --- a/R/simulate_serosurvey.R +++ b/R/simulate_serosurvey.R @@ -415,55 +415,6 @@ check_age_constraints <- function(df) { return(TRUE) } -validate_survey <- function(survey_features) { - - if (!is.data.frame(survey_features) || !all(c("age_min", "age_max", "sample_size") %in% names(survey_features))) { - stop("survey_features must be a dataframe with columns 'age_min', 'age_max', and 'sample_size'.") - } - - # check that the age_max of a bin does not coincide with - # the age min of a different bin - is_age_ok <- check_age_constraints(survey_features) - if(!is_age_ok) - stop("Age bins in a survey are inclusive of both bounds, so the age_max of - one bin cannot equal the age_min of another.") -} - -validate_foi_df <- function(foi_df, cnames_additional) { - if (!is.data.frame(foi_df) || !all(cnames_additional %in% names(foi_df)) || ncol(foi_df) != (1 + length(cnames_additional))) { - if(length(cnames_additional) == 1) - message_end <- paste0(" and ", cnames_additional, ".") - else - message_end <- paste0(", ", paste(cnames_additional, collapse=" and "), ".") - message_beginning <- "foi must be a dataframe with columns foi" - stop(paste0(message_beginning, message_end)) - } -} - -validate_seroreversion_rate <- function(seroreversion_rate) { - if (!is.numeric(seroreversion_rate) || seroreversion_rate < 0) { - stop("seroreversion_rate must be a non-negative numeric value.") - } -} - -validate_survey_and_foi_consistency <- function( - survey_features, - foi_df -) { - max_age_foi_df <- nrow(foi_df) - if(max_age_foi_df > max(survey_features$age_max)) - stop("maximum age implicit in foi_df should not exceed max age in survey_features.") -} - -validate_survey_and_foi_consistency_age_time <- function( - survey_features, - foi_df -) { - max_age_foi_df <- max(foi_df$year) - min(foi_df$year) + 1 - if(max_age_foi_df > max(survey_features$age_max)) - stop("maximum age implicit in foi_df should not exceed max age in survey_features.") -} - generate_seropositive_counts_by_age_bin <- function( probability_seropositive_by_age, sample_size_by_age_random, diff --git a/R/validation.R b/R/validation.R index 14e2570a..53110175 100644 --- a/R/validation.R +++ b/R/validation.R @@ -55,3 +55,51 @@ validate_serosurvey <- function(serosurvey) { return(serosurvey) } + +validate_survey <- function(survey_features) { + + if (!is.data.frame(survey_features) || !all(c("age_min", "age_max", "sample_size") %in% names(survey_features))) { + stop("survey_features must be a dataframe with columns 'age_min', 'age_max', and 'sample_size'.") + } + + # check that the age_max of a bin does not coincide with + # the age min of a different bin + is_age_ok <- check_age_constraints(survey_features) + if(!is_age_ok) + stop("Age bins in a survey are inclusive of both bounds, so the age_max of one bin cannot equal the age_min of another.") +} + +validate_foi_df <- function(foi_df, cnames_additional) { + if (!is.data.frame(foi_df) || !all(cnames_additional %in% names(foi_df)) || ncol(foi_df) != (1 + length(cnames_additional))) { + if(length(cnames_additional) == 1) + message_end <- paste0(" and ", cnames_additional, ".") + else + message_end <- paste0(", ", paste(cnames_additional, collapse=" and "), ".") + message_beginning <- "foi must be a dataframe with columns foi" + stop(paste0(message_beginning, message_end)) + } +} + +validate_seroreversion_rate <- function(seroreversion_rate) { + if (!is.numeric(seroreversion_rate) || seroreversion_rate < 0) { + stop("seroreversion_rate must be a non-negative numeric value.") + } +} + +validate_survey_and_foi_consistency <- function( + survey_features, + foi_df +) { + max_age_foi_df <- nrow(foi_df) + if(max_age_foi_df > max(survey_features$age_max)) + stop("maximum age implicit in foi_df should not exceed max age in survey_features.") +} + +validate_survey_and_foi_consistency_age_time <- function( + survey_features, + foi_df +) { + max_age_foi_df <- max(foi_df$year) - min(foi_df$year) + 1 + if(max_age_foi_df > max(survey_features$age_max)) + stop("maximum age implicit in foi_df should not exceed max age in survey_features.") +}