Skip to content

Commit

Permalink
refac: move data simulation validation functions to validation module
Browse files Browse the repository at this point in the history
  • Loading branch information
ntorresd committed Aug 1, 2024
1 parent ea07ee8 commit ea6093f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
49 changes: 0 additions & 49 deletions R/simulate_serosurvey.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 48 additions & 0 deletions R/validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

0 comments on commit ea6093f

Please sign in to comment.