diff --git a/DESCRIPTION b/DESCRIPTION index d92fa304..76eb8165 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -93,7 +93,7 @@ Suggests: ggplot2, covr, mgcv -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Roxygen: list(markdown = TRUE) LinkingTo: Rcpp, diff --git a/R/antimalarial_resistance.R b/R/antimalarial_resistance.R index 18d14d9a..121de96c 100644 --- a/R/antimalarial_resistance.R +++ b/R/antimalarial_resistance.R @@ -5,55 +5,54 @@ #' @param parameters the model parameters #' @param drug the index of the drug which resistance is being set, as set by the set_drugs() function, in the parameter list #' @param timesteps vector of time steps for each update to resistance proportion and resistance outcome probability -#' @param artemisinin_resistance vector of updates to the proportions of infections that are artemisinin resistant at time t -#' @param partner_drug_resistance vector of updates to the proportions of infections that are partner-drug resistant at time t -#' @param slow_parasite_clearance_prob vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure -#' @param early_treatment_failure_prob vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance -#' @param late_clinical_failure_prob vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure -#' @param late_parasitological_prob vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure -#' @param reinfection_prob vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis +#' @param artemisinin_resistance_proportion vector of updates to the proportions of infections that are artemisinin resistant at time t +#' @param partner_drug_resistance_proportion vector of updates to the proportions of infections that are partner-drug resistant at time t +#' @param slow_parasite_clearance_probability vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure +#' @param early_treatment_failure_probability vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance +#' @param late_clinical_failure_probability vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure +#' @param late_parasitological_failure_probability vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure +#' @param reinfection_during_prophylaxis_probability vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis #' @param slow_parasite_clearance_time single value representing the mean time individual's experiencing slow parasite clearance reside in the treated state #' @export set_antimalarial_resistance <- function(parameters, drug, timesteps, - artemisinin_resistance, - partner_drug_resistance, - slow_parasite_clearance_prob, - early_treatment_failure_prob, - late_clinical_failure_prob, - late_parasitological_prob, - reinfection_prob, + artemisinin_resistance_proportion, + partner_drug_resistance_proportion, + slow_parasite_clearance_probability, + early_treatment_failure_probability, + late_clinical_failure_probability, + late_parasitological_failure_probability, + reinfection_during_prophylaxis_probability, slow_parasite_clearance_time) { - if(any(partner_drug_resistance > 0, - slow_parasite_clearance_prob > 0, - late_clinical_failure_prob > 0, - late_parasitological_prob > 0, - reinfection_prob > 0)) { + if(any(partner_drug_resistance_proportion > 0, + late_clinical_failure_probability > 0, + late_parasitological_failure_probability > 0, + reinfection_during_prophylaxis_probability > 0)) { stop("Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") } - if(any(c(length(artemisinin_resistance), - length(partner_drug_resistance), - length(slow_parasite_clearance_prob), - length(early_treatment_failure_prob), - length(late_clinical_failure_prob), - length(late_parasitological_prob), - length(reinfection_prob)) != length(timesteps))) { + if(any(c(length(artemisinin_resistance_proportion), + length(partner_drug_resistance_proportion), + length(slow_parasite_clearance_probability), + length(early_treatment_failure_probability), + length(late_clinical_failure_probability), + length(late_parasitological_failure_probability), + length(reinfection_during_prophylaxis_probability)) != length(timesteps))) { stop("Length of one or more resistance parameter vectors does not match time steps specified for update") } - if(any(artemisinin_resistance < 0 | artemisinin_resistance > 1 | - partner_drug_resistance < 0 | partner_drug_resistance > 1)) { + if(any(artemisinin_resistance_proportion < 0 | artemisinin_resistance_proportion > 1 | + partner_drug_resistance_proportion < 0 | partner_drug_resistance_proportion > 1)) { stop("Artemisinin and partner-drug resistance proportions must fall between 0 and 1") } - if(any(slow_parasite_clearance_prob < 0 | slow_parasite_clearance_prob > 1 | - early_treatment_failure_prob < 0 | early_treatment_failure_prob > 1 | - late_clinical_failure_prob < 0 | late_clinical_failure_prob > 1 | - late_parasitological_prob < 0 | late_parasitological_prob > 1 | - reinfection_prob < 0 | reinfection_prob > 1)) { + if(any(slow_parasite_clearance_probability < 0 | slow_parasite_clearance_probability > 1 | + early_treatment_failure_probability < 0 | early_treatment_failure_probability > 1 | + late_clinical_failure_probability < 0 | late_clinical_failure_probability > 1 | + late_parasitological_failure_probability < 0 | late_parasitological_failure_probability > 1 | + reinfection_during_prophylaxis_probability < 0 | reinfection_during_prophylaxis_probability > 1)) { stop("Resistance outcome probabilities must fall between 0 and 1") } @@ -81,13 +80,13 @@ set_antimalarial_resistance <- function(parameters, parameters$antimalarial_resistance_drug[[drug_index]] <- drug parameters$antimalarial_resistance_timesteps[[drug_index]] <- timesteps - parameters$prop_artemisinin_resistant[[drug_index]] <- artemisinin_resistance - parameters$prop_partner_drug_resistant[[drug_index]] <- partner_drug_resistance - parameters$slow_parasite_clearance_prob[[drug_index]] <- slow_parasite_clearance_prob - parameters$early_treatment_failure_prob[[drug_index]] <- early_treatment_failure_prob - parameters$late_clinical_failure_prob[[drug_index]] <- late_clinical_failure_prob - parameters$late_parasitological_failure_prob[[drug_index]] <- late_parasitological_prob - parameters$reinfection_during_prophylaxis[[drug_index]] <- reinfection_prob + parameters$artemisinin_resistance_proportion[[drug_index]] <- artemisinin_resistance_proportion + parameters$partner_drug_resistance_proportion[[drug_index]] <- partner_drug_resistance_proportion + parameters$slow_parasite_clearance_probability[[drug_index]] <- slow_parasite_clearance_probability + parameters$early_treatment_failure_probability[[drug_index]] <- early_treatment_failure_probability + parameters$late_clinical_failure_probability[[drug_index]] <- late_clinical_failure_probability + parameters$late_parasitological_failure_probability[[drug_index]] <- late_parasitological_failure_probability + parameters$reinfection_during_prophylaxis_probability[[drug_index]] <- reinfection_during_prophylaxis_probability parameters$dt_slow_parasite_clearance[[drug_index]] <- slow_parasite_clearance_time return(parameters) @@ -122,13 +121,13 @@ get_antimalarial_resistance_parameters <- function(parameters, drugs, timestep) drug <- parameters$antimalarial_resistance_drug[[i]] treated_with_drug <- which(drugs == drug) resistance_timestep <- match_timestep(ts = parameters$antimalarial_resistance_timesteps[[i]], t = timestep) - artemisinin_resistance_proportion[treated_with_drug] <- parameters$prop_artemisinin_resistant[[i]][resistance_timestep] - partner_drug_resistance_proportion[treated_with_drug] <- parameters$prop_partner_drug_resistant[[i]][resistance_timestep] - slow_parasite_clearance_probability[treated_with_drug] <- parameters$slow_parasite_clearance_prob[[i]][resistance_timestep] - early_treatment_failure_probability[treated_with_drug] <- parameters$early_treatment_failure_prob[[i]][resistance_timestep] - late_clinical_failure_probability[treated_with_drug] <- parameters$late_clinical_failure_prob[[i]][resistance_timestep] - late_parasitological_failure_probability[treated_with_drug] <- parameters$late_parasitological_failure_prob[[i]][resistance_timestep] - reinfection_during_prophylaxis_probability[treated_with_drug] <- parameters$reinfection_during_prophylaxis[[i]][resistance_timestep] + artemisinin_resistance_proportion[treated_with_drug] <- parameters$artemisinin_resistance_proportion[[i]][resistance_timestep] + partner_drug_resistance_proportion[treated_with_drug] <- parameters$partner_drug_resistance_proportion[[i]][resistance_timestep] + slow_parasite_clearance_probability[treated_with_drug] <- parameters$slow_parasite_clearance_probability[[i]][resistance_timestep] + early_treatment_failure_probability[treated_with_drug] <- parameters$early_treatment_failure_probability[[i]][resistance_timestep] + late_clinical_failure_probability[treated_with_drug] <- parameters$late_clinical_failure_probability[[i]][resistance_timestep] + late_parasitological_failure_probability[treated_with_drug] <- parameters$late_parasitological_failure_probability[[i]][resistance_timestep] + reinfection_during_prophylaxis_probability[treated_with_drug] <- parameters$reinfection_during_prophylaxis_probability[[i]][resistance_timestep] dt_slow_parasite_clearance[treated_with_drug] <- parameters$dt_slow_parasite_clearance[[i]] } @@ -144,4 +143,4 @@ get_antimalarial_resistance_parameters <- function(parameters, drugs, timestep) return(resistance_parameters) -} \ No newline at end of file +} diff --git a/R/disease_progression.R b/R/disease_progression.R index 05367252..d9ac11e4 100644 --- a/R/disease_progression.R +++ b/R/disease_progression.R @@ -19,15 +19,27 @@ update_infection <- function( } create_progression_process <- function( - state, - from_state, - to_state, - rate, - infectivity, - new_infectivity - ) { + state, + from_state, + to_state, + rate, + infectivity, + new_infectivity +) { function(timestep) { - to_move <- state$get_index_of(from_state)$sample(1/rate) + + # Retrieve the indices of all individuals in the to_move state: + index <- state$get_index_of(from_state) + + # If the length of rate is greater than 1 (when it's a variable): + if (inherits(rate, "DoubleVariable")) { + rate <- rate$get_values(index) + } + + # Sample the individuals to be moved into a new Bitset using the transition rate(s): + to_move <- index$sample(1/rate) + + # Update the infection status of those individuals who are moving: update_infection( state, to_state, diff --git a/R/human_infection.R b/R/human_infection.R index 6b2ac3cf..d62b16f0 100644 --- a/R/human_infection.R +++ b/R/human_infection.R @@ -295,50 +295,81 @@ calculate_treated <- function( ) ]) + #+++ DRUG EFFICACY +++# + #+++++++++++++++++++++# + effectively_treated_index <- bernoulli_multi_p(parameters$drug_efficacy[drugs]) + effectively_treated <- bitset_at(seek_treatment, effectively_treated_index) + drugs <- drugs[effectively_treated_index] + n_drug_efficacy_failures <- n_treat - effectively_treated$size() + renderer$render('n_drug_efficacy_failures', n_drug_efficacy_failures, timestep) + + #+++ ANTIMALARIAL RESISTANCE +++# + #+++++++++++++++++++++++++++++++# if(parameters$antimalarial_resistance) { resistance_parameters <- get_antimalarial_resistance_parameters( parameters = parameters, drugs = drugs, timestep = timestep ) - unsuccessful_treatment_probability <- resistance_parameters$artemisinin_resistance_proportion * resistance_parameters$early_treatment_failure_probability - susceptible_to_treatment_index <- bernoulli_multi_p(p = 1 - unsuccessful_treatment_probability) - susceptible_to_treatment <- bitset_at(seek_treatment, susceptible_to_treatment_index) - drugs <- drugs[susceptible_to_treatment_index] + + #+++ EARLY TREATMENT FAILURE +++# + #+++++++++++++++++++++++++++++++# + early_treatment_failure_probability <- resistance_parameters$artemisinin_resistance_proportion * resistance_parameters$early_treatment_failure_probability + successfully_treated_indices <- bernoulli_multi_p(p = 1 - early_treatment_failure_probability) + successfully_treated <- bitset_at(effectively_treated, successfully_treated_indices) + n_early_treatment_failure <- effectively_treated$size() - successfully_treated$size() + renderer$render('n_early_treatment_failure', n_early_treatment_failure, timestep) + drugs <- drugs[successfully_treated_indices] + dt_slow_parasite_clearance <- resistance_parameters$dt_slow_parasite_clearance[successfully_treated_indices] + + #+++ SLOW PARASITE CLEARANCE +++# + #+++++++++++++++++++++++++++++++# + slow_parasite_clearance_probability <- resistance_parameters$artemisinin_resistance_proportion[successfully_treated_indices] * + resistance_parameters$slow_parasite_clearance_probability[successfully_treated_indices] + slow_parasite_clearance_indices <- bernoulli_multi_p(p = slow_parasite_clearance_probability) + slow_parasite_clearance_individuals <- bitset_at(successfully_treated, slow_parasite_clearance_indices) + renderer$render('n_slow_parasite_clearance', slow_parasite_clearance_individuals$size(), timestep) + non_slow_parasite_clearance_individuals <- successfully_treated$copy()$set_difference(slow_parasite_clearance_individuals) + renderer$render('n_successfully_treated', successfully_treated$size(), timestep) + dt_slow_parasite_clearance <- dt_slow_parasite_clearance[slow_parasite_clearance_indices] + } else { - susceptible_to_treatment <- seek_treatment + + successfully_treated <- effectively_treated + renderer$render('n_successfully_treated', successfully_treated$size(), timestep) } - n_early_treatment_failure <- n_treat - susceptible_to_treatment$size() - successfully_treated_index <- bernoulli_multi_p(parameters$drug_efficacy[drugs]) - successfully_treated <- bitset_at(susceptible_to_treatment, successfully_treated_index) - successfully_treated_drugs <- drugs[successfully_treated_index] - n_treat_eff_fail <- susceptible_to_treatment$size() - length(successfully_treated_index) - renderer$render('n_early_treatment_failure', n_early_treatment_failure, timestep) - renderer$render('n_treat_eff_fail', n_treat_eff_fail, timestep) - renderer$render('n_treat_success', successfully_treated$size(), timestep) - - # Update variables of those who have been successfully treated: if (successfully_treated$size() > 0) { variables$state$queue_update("Tr", successfully_treated) variables$infectivity$queue_update( - parameters$cd * parameters$drug_rel_c[successfully_treated_drugs], + parameters$cd * parameters$drug_rel_c[drugs], successfully_treated ) variables$drug$queue_update( - successfully_treated_drugs, + drugs, successfully_treated ) variables$drug_time$queue_update( timestep, successfully_treated ) + if(parameters$antimalarial_resistance) { + variables$dt$queue_update( + parameters$dt, + non_slow_parasite_clearance_individuals + ) + variables$dt$queue_update( + dt_slow_parasite_clearance, + slow_parasite_clearance_individuals + ) + } } + successfully_treated + } - #' @title Schedule infections #' @description #' Schedule infections in humans after the incubation period diff --git a/R/model.R b/R/model.R index 34bbfb46..def6db23 100644 --- a/R/model.R +++ b/R/model.R @@ -74,9 +74,10 @@ #' susceptible #' * net_usage: the number people protected by a bed net #' * mosquito_deaths: number of adult female mosquitoes who die this timestep -#' * n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure in this timestep -#' * n_treat_eff_fail: number of clinically treated individuals who's treatment failed due to drug efficacy -#' * n_treat_success: number of successfully treated individuals in this timestep +#' * n_drug_efficacy_failures: number of clinically treated individuals whose treatment failed due to drug efficacy +#' * n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure +#' * n_successfully_treated: number of clinically treated individuals who are treated successfully (includes individuals who experience slow parasite clearance) +#' * n_slow_parasite_clearance: number of clinically treated individuals who experienced slow parasite clearance #' #' @param timesteps the number of timesteps to run the simulation for (in days) #' @param parameters a named list of parameters to use diff --git a/R/mortality_processes.R b/R/mortality_processes.R index f0f1b1f9..37792f6c 100644 --- a/R/mortality_processes.R +++ b/R/mortality_processes.R @@ -27,7 +27,7 @@ create_mortality_process <- function(variables, events, renderer, parameters) { died <- individual::Bitset$new(pop)$insert(bernoulli_multi_p(deathrates)) renderer$render('natural_deaths', died$size(), timestep) } - reset_target(variables, events, died, 'S', timestep) + reset_target(variables, events, died, 'S', parameters, timestep) sample_maternal_immunity(variables, died, timestep, parameters) } } @@ -74,7 +74,7 @@ sample_maternal_immunity <- function(variables, target, timestep, parameters) { } } -reset_target <- function(variables, events, target, state, timestep) { +reset_target <- function(variables, events, target, state, parameters, timestep) { if (target$size() > 0) { # clear events to_clear <- c( @@ -113,6 +113,11 @@ reset_target <- function(variables, events, target, state, timestep) { # onwards infectiousness variables$infectivity$queue_update(0, target) + + # treated compartment residence time: + if(!is.null(variables$dt)) { + variables$dt$queue_update(parameters$dt, target) + } # zeta and zeta group and vector controls survive rebirth } diff --git a/R/parameters.R b/R/parameters.R index 12f4c4be..647b78b2 100644 --- a/R/parameters.R +++ b/R/parameters.R @@ -200,14 +200,14 @@ #' * antimalarial_resistance - boolean for if antimalarial resistance is enabled; default = FALSE #' * antimalarial_resistance_drug - vector of drugs for which resistance can be parameterised; default = NULL #' * antimalarial_resistance_timesteps - vector of time steps on which resistance updates occur; default = NULL -#' * prop_artemisinin_resistant - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL -#' * prop_partner_drug_resistant - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL (currently unimplemented) -#' * slow_parasite_clearance_prob - vector of probabilities of slow parasite clearance for a given drug; default = NULL (currently unimplemented) -#' * early_treatment_failure_prob - vector of probabilities of early treatment failure for a given drug; default = NULL -#' * late_clinical_failure_prob - vector of probabilities of late clinical failure for a given drug; default = NULL (currently unimplemented) -#' * late_parasitological_failure_prob - vector of probabilities of late parasitological failure for a given drug; default = NULL (currently unimplemented) -#' * reinfection_during_prophylaxis - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL (currently unimplemented) -#' * dt_slow_parasite_clearance - the delay for humans experiencing slow parasite clearance to move from state Tr to S; default = NULL (currently unimplemented) +#' * artemisinin_resistant_proportion - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL +#' * partner_drug_resistance_proportion - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL +#' * slow_parasite_clearance_probability - vector of probabilities of slow parasite clearance for a given drug; default = NULL +#' * early_treatment_failure_probability - vector of probabilities of early treatment failure for a given drug; default = NULL +#' * late_clinical_failure_probability - vector of probabilities of late clinical failure for a given drug; default = NULL +#' * late_parasitological_failure_probability - vector of probabilities of late parasitological failure for a given drug; default = NULL +#' * reinfection_during_prophylaxis_probability - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL +#' * dt_slow_parasite_clearance - the delay for humans experiencing slow parasite clearance to move from state Tr to S; default = NULL #' #' rendering: #' All values are in timesteps and all ranges are inclusive @@ -397,13 +397,14 @@ get_parameters <- function(overrides = list()) { antimalarial_resistance = FALSE, antimalarial_resistance_drug = NULL, antimalarial_resistance_timesteps = NULL, - prop_artemisinin_resistant = NULL, - prop_partner_drug_resistant = NULL, - slow_parasite_clearance_prob = NULL, - early_treatment_failure_prob = NULL, - late_clinical_failure_prob = NULL, - late_parasitological_failure_prob = NULL, - reinfection_during_prophylaxis = NULL, + artemisinin_resistance_proportion = NULL, + partner_drug_resistance_proportion = NULL, + slow_parasite_clearance_probability = NULL, + early_treatment_failure_probability = NULL, + late_clinical_failure_probability = NULL, + late_parasitological_failure_probability = NULL, + reinfection_during_prophylaxis_probability = NULL, + dt_slow_parasite_clearance = NULL, # flexible carrying capacity carrying_capacity = FALSE, carrying_capacity_timesteps = NULL, diff --git a/R/processes.R b/R/processes.R index 28810263..657ba2e9 100644 --- a/R/processes.R +++ b/R/processes.R @@ -104,12 +104,32 @@ create_processes <- function( parameters$du, variables$infectivity, 0 - ), + ) + ) + + # ======================= + # Antimalarial Resistance + # ======================= + # Add an a new process which governs the transition from Tr to S when + # antimalarial resistance is simulated. The rate of transition switches + # from a parameter to a variable when antimalarial resistance == TRUE. + + # Assign the dt input to a separate object with the default single parameter value: + dt_input <- parameters$dt + + # If antimalarial resistance is switched on, assign dt variable values to the + if(parameters$antimalarial_resistance) { + dt_input <- variables$dt + } + + # Create the progression process for Tr --> S specifying dt_input as the rate: + processes <- c( + processes, create_progression_process( variables$state, 'Tr', 'S', - parameters$dt, + dt_input, variables$infectivity, 0 ) diff --git a/R/variables.R b/R/variables.R index f9c75e6e..092a6431 100644 --- a/R/variables.R +++ b/R/variables.R @@ -33,6 +33,9 @@ #' * drug - The last prescribed drug #' * drug_time - The timestep of the last drug #' +#' Antimalarial resistance variables are: +#' * dt - the delay for humans to move from state Tr to state S +#' #' Mosquito variables are: #' * mosquito_state - the state of the mosquito, a category Sm|Pm|Im|NonExistent #' * species - the species of mosquito, this is a category gamb|fun|arab @@ -226,6 +229,15 @@ create_variables <- function(parameters) { net_time = net_time, spray_time = spray_time ) + + # Add variables for antimalarial resistance state residency times (dt) + if(parameters$antimalarial_resistance) { + dt <- individual::DoubleVariable$new(rep(parameters$dt, size)) + variables <- c( + variables, + dt = dt + ) + } # Add variables for individual mosquitoes if (parameters$individual_mosquitoes) { diff --git a/man/get_parameters.Rd b/man/get_parameters.Rd index 8afae241..e43792d5 100644 --- a/man/get_parameters.Rd +++ b/man/get_parameters.Rd @@ -221,14 +221,14 @@ please set antimalarial resistance parameters with the convenience functions in \item antimalarial_resistance - boolean for if antimalarial resistance is enabled; default = FALSE \item antimalarial_resistance_drug - vector of drugs for which resistance can be parameterised; default = NULL \item antimalarial_resistance_timesteps - vector of time steps on which resistance updates occur; default = NULL -\item prop_artemisinin_resistant - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL -\item prop_partner_drug_resistant - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL (currently unimplemented) -\item slow_parasite_clearance_prob - vector of probabilities of slow parasite clearance for a given drug; default = NULL (currently unimplemented) -\item early_treatment_failure_prob - vector of probabilities of early treatment failure for a given drug; default = NULL -\item late_clinical_failure_prob - vector of probabilities of late clinical failure for a given drug; default = NULL (currently unimplemented) -\item late_parasitological_failure_prob - vector of probabilities of late parasitological failure for a given drug; default = NULL (currently unimplemented) -\item reinfection_during_prophylaxis - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL (currently unimplemented) -\item dt_slow_parasite_clearance - the delay for humans experiencing slow parasite clearance to move from state Tr to S; default = NULL (currently unimplemented) +\item artemisinin_resistant_proportion - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL +\item partner_drug_resistance_proportion - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL +\item slow_parasite_clearance_probability - vector of probabilities of slow parasite clearance for a given drug; default = NULL +\item early_treatment_failure_probability - vector of probabilities of early treatment failure for a given drug; default = NULL +\item late_clinical_failure_probability - vector of probabilities of late clinical failure for a given drug; default = NULL +\item late_parasitological_failure_probability - vector of probabilities of late parasitological failure for a given drug; default = NULL +\item reinfection_during_prophylaxis_probability - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL +\item dt_slow_parasite_clearance - the delay for humans experiencing slow parasite clearance to move from state Tr to S; default = NULL } rendering: diff --git a/man/run_simulation.Rd b/man/run_simulation.Rd index 07871b13..383b38f6 100644 --- a/man/run_simulation.Rd +++ b/man/run_simulation.Rd @@ -90,8 +90,9 @@ subpatent susceptible \item net_usage: the number people protected by a bed net \item mosquito_deaths: number of adult female mosquitoes who die this timestep -\item n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure in this timestep -\item n_treat_eff_fail: number of clinically treated individuals who's treatment failed due to drug efficacy -\item n_treat_success: number of successfully treated individuals in this timestep +\item n_drug_efficacy_failures: number of clinically treated individuals whose treatment failed due to drug efficacy +\item n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure +\item n_successfully_treated: number of clinically treated individuals who are treated successfully (includes individuals who experience slow parasite clearance) +\item n_slow_parasite_clearance: number of clinically treated individuals who experienced slow parasite clearance } } diff --git a/man/set_antimalarial_resistance.Rd b/man/set_antimalarial_resistance.Rd index 82c65405..56627125 100644 --- a/man/set_antimalarial_resistance.Rd +++ b/man/set_antimalarial_resistance.Rd @@ -8,13 +8,13 @@ set_antimalarial_resistance( parameters, drug, timesteps, - artemisinin_resistance, - partner_drug_resistance, - slow_parasite_clearance_prob, - early_treatment_failure_prob, - late_clinical_failure_prob, - late_parasitological_prob, - reinfection_prob, + artemisinin_resistance_proportion, + partner_drug_resistance_proportion, + slow_parasite_clearance_probability, + early_treatment_failure_probability, + late_clinical_failure_probability, + late_parasitological_failure_probability, + reinfection_during_prophylaxis_probability, slow_parasite_clearance_time ) } @@ -25,19 +25,19 @@ set_antimalarial_resistance( \item{timesteps}{vector of time steps for each update to resistance proportion and resistance outcome probability} -\item{artemisinin_resistance}{vector of updates to the proportions of infections that are artemisinin resistant at time t} +\item{artemisinin_resistance_proportion}{vector of updates to the proportions of infections that are artemisinin resistant at time t} -\item{partner_drug_resistance}{vector of updates to the proportions of infections that are partner-drug resistant at time t} +\item{partner_drug_resistance_proportion}{vector of updates to the proportions of infections that are partner-drug resistant at time t} -\item{slow_parasite_clearance_prob}{vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure} +\item{slow_parasite_clearance_probability}{vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure} -\item{early_treatment_failure_prob}{vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance} +\item{early_treatment_failure_probability}{vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance} -\item{late_clinical_failure_prob}{vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure} +\item{late_clinical_failure_probability}{vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure} -\item{late_parasitological_prob}{vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure} +\item{late_parasitological_failure_probability}{vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure} -\item{reinfection_prob}{vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis} +\item{reinfection_during_prophylaxis_probability}{vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis} \item{slow_parasite_clearance_time}{single value representing the mean time individual's experiencing slow parasite clearance reside in the treated state} } diff --git a/tests/testthat/test-antimalarial-resistance.R b/tests/testthat/test-antimalarial-resistance.R index feb7f67a..ae45a20e 100644 --- a/tests/testthat/test-antimalarial-resistance.R +++ b/tests/testthat/test-antimalarial-resistance.R @@ -1,102 +1,103 @@ test_that('set_antimalarial_resistance() toggles resistance on', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, - slow_parasite_clearance_time = 10) -> simparams - expect_identical(object = simparams$antimalarial_resistance, expected = TRUE) + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, + slow_parasite_clearance_time = 10) -> parameters + expect_identical(object = parameters$antimalarial_resistance, expected = TRUE) }) test_that('set_antimalarial_resistance() errors if parameter inputs of different length to timesteps', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = c(1, 10), - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10)) }) test_that('set_antimalarial_resistance() errors if resistance proportions outside of range 0-1', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 1.01, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 1.01, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10), regexp = "Artemisinin and partner-drug resistance proportions must fall between 0 and 1") }) test_that('set_antimalarial_resistance() errors if resistance phenotype probabilities outside bound of 0-1', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.4, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = -0.6, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.4, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = -0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0.2, + late_parasitological_failure_probability = 0.3, + reinfection_during_prophylaxis_probability = 0.4, slow_parasite_clearance_time = 5)) }) test_that('set_antimalarial_resistance() errors if drug index > than number of drugs assigned using set_drugs()', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0.4, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0)) + artemisinin_resistance_proportion = 0.4, + partner_drug_resistance_proportion = 0.3, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0.4, + slow_parasite_clearance_time = 10)) }) test_that('set_antimalarial_resistance() assigns parameters correctly despite order in which resistance parameters are specified', { @@ -109,52 +110,52 @@ test_that('set_antimalarial_resistance() assigns parameters correctly despite or parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.41, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 5) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 3, timesteps = 1, - artemisinin_resistance = 0.43, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.27, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.9, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.27, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.23, + early_treatment_failure_probability = 0.9, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 20) expect_identical(parameters$antimalarial_resistance, TRUE) expect_identical(unlist(parameters$antimalarial_resistance_drug), c(2, 3, 1)) expect_identical(unlist(parameters$antimalarial_resistance_timesteps), rep(1, 3)) - expect_identical(unlist(parameters$prop_artemisinin_resistant), c(0.5, 0.43, 0.27)) - expect_identical(unlist(parameters$prop_partner_drug_resistant), c(0, 0, 0)) - expect_identical(unlist(parameters$slow_parasite_clearance_prob), c(0, 0, 0)) - expect_identical(unlist(parameters$early_treatment_failure_prob), c(0.2, 0, 0.9)) - expect_identical(unlist(parameters$late_clinical_failure_prob), c(0, 0, 0)) - expect_identical(unlist(parameters$late_parasitological_failure_prob), c(0, 0, 0)) - expect_identical(unlist(parameters$reinfection_during_prophylaxis), c(0, 0, 0)) + expect_identical(unlist(parameters$artemisinin_resistance_proportion), c(0.5, 0, 0.27)) + expect_identical(unlist(parameters$partner_drug_resistance_proportion), c(0, 0, 0)) + expect_identical(unlist(parameters$slow_parasite_clearance_probability), c(0.41, 0, 0.23)) + expect_identical(unlist(parameters$early_treatment_failure_probability), c(0.2, 0, 0.9)) + expect_identical(unlist(parameters$late_clinical_failure_probability), c(0, 0, 0)) + expect_identical(unlist(parameters$late_parasitological_failure_probability), c(0, 0, 0)) + expect_identical(unlist(parameters$reinfection_during_prophylaxis_probability), c(0, 0, 0)) expect_identical(unlist(parameters$dt_slow_parasite_clearance), c(5, 10, 20)) }) -test_that(desc = "set_antimalarial_resistance errors if length slow_parasite_clearance_time > 1", code = { +test_that("set_antimalarial_resistance errors if length slow_parasite_clearance_time > 1", { parameters <- get_parameters() parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) @@ -167,18 +168,18 @@ test_that(desc = "set_antimalarial_resistance errors if length slow_parasite_cle parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = c(0, 10), - artemisinin_resistance = c(0.4, 0.8), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0, 0.45), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), + artemisinin_resistance_proportion = c(0.4, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.2, 0.4), + early_treatment_failure_probability = c(0, 0.45), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), slow_parasite_clearance_time = c(10 ,11)), "Error: length of slow_parasite_clearance_time not equal to 1") }) -test_that(desc = "set_antimalarial_resistance errors if slow_parasite_clearance_time not positive", code = { +test_that("set_antimalarial_resistance errors if slow_parasite_clearance_time not positive", { parameters <- get_parameters() parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) @@ -191,217 +192,132 @@ test_that(desc = "set_antimalarial_resistance errors if slow_parasite_clearance_ parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = c(0, 10), - artemisinin_resistance = c(0.4, 0.8), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0, 0.45), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), + artemisinin_resistance_proportion = c(0.4, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.2, 0.4), + early_treatment_failure_probability = c(0, 0.45), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), slow_parasite_clearance_time = c(0)), "Error: slow_parasite_clearance_time is non-positive") }) -test_that("set_antimalarial_resistance() errors when users attempt to use undeveloped model features", { - - # Partner Drug Resistance - expect_error(get_parameters() |> - set_drugs(drugs = list(SP_AQ_params)) |> - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.6) |> - set_antimalarial_resistance(drug = 1, - timesteps = c(1, 10), - artemisinin_resistance = c(0.4, 0.5), - partner_drug_resistance = c(0, 0.5), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.8, 0.8), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 10), - "Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") +test_that('get_antimalarial_resistance_parameters() correctly retrieves parameters when multiple drugs assigned', { + get_parameters(overrides = list(human_population = 10000)) |> + set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) |> + set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) |> + set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) |> + set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) |> + set_equilibrium(init_EIR = 20) |> + set_antimalarial_resistance(drug = 2, + timesteps = c(0, 20), + artemisinin_resistance_proportion = c(0.02, 0.2), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.02, 0.2), + early_treatment_failure_probability = c(0.02, 0.2), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 20) |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, 10), + artemisinin_resistance_proportion = c(0.01, 0.1), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.01, 0.1), + early_treatment_failure_probability = c(0.01, 0.1), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 10) |> + set_antimalarial_resistance(drug = 3, + timesteps = c(0, 30), + artemisinin_resistance_proportion = c(0.03, 0.3), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.03, 0.3), + early_treatment_failure_probability = c(0.03, 0.3), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 30) -> parameters - # Slow Parasite Clearance - expect_error(get_parameters() |> - set_drugs(drugs = list(SP_AQ_params)) |> - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.6) |> - set_antimalarial_resistance(drug = 1, - timesteps = c(1, 10), - artemisinin_resistance = c(0.4, 0.5), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0.0001), - early_treatment_failure_prob = c(0.8, 0.8), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 10), - "Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") + drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) + timestep <- 25 - # Late Clinical Failure - expect_error(get_parameters() |> - set_drugs(drugs = list(SP_AQ_params)) |> - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.6) |> - set_antimalarial_resistance(drug = 1, - timesteps = c(1, 10), - artemisinin_resistance = c(0.4, 0.5), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.8, 0.8), - late_clinical_failure_prob = c(0.6, 0.43), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 10), - "Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") + resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, + drugs = drugs, + timestep = timestep) - # Late Parasitological Failure - expect_error(get_parameters() |> - set_drugs(drugs = list(SP_AQ_params)) |> - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.6) |> - set_antimalarial_resistance(drug = 1, - timesteps = c(1, 10), - artemisinin_resistance = c(0.4, 0.5), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.8, 0.8), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(1, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 10), - "Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") + expected_resistance_parameters <- list() + expected_resistance_parameters$artemisinin_resistance_proportion <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) + expected_resistance_parameters$slow_parasite_clearance_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$early_treatment_failure_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) + expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) + expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) + expected_resistance_parameters$dt_slow_parasite_clearance <- c(10, 30, 20, 10, 20, 30, 30, 30, 20, 10, 30, 10, 20, 30, 20) - # Reinfection During Prophylaxis - expect_error(get_parameters() |> - set_drugs(drugs = list(SP_AQ_params)) |> - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.6) |> - set_antimalarial_resistance(drug = 1, - timesteps = c(1, 10), - artemisinin_resistance = c(0.4, 0.5), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.8, 0.8), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0.21, 0), - slow_parasite_clearance_time = 10), - "Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") + expect_identical(resistance_parameters, expected = expected_resistance_parameters) }) -test_that('get_antimalarial_resistance_parameters() correctly retrieves parameters when multiple drugs assigned', { - - get_parameters(overrides = list(human_population = 10000)) %>% - set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% - set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% - set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% - set_equilibrium(init_EIR = 20) %>% - set_antimalarial_resistance(drug = 2, - timesteps = c(0, 20), - artemisinin_resistance = c(0.02, 0.2), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.02, 0.2), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 20) %>% - set_antimalarial_resistance(drug = 1, - timesteps = c(0, 10), - artemisinin_resistance = c(0.01, 0.1), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.01, 0.1), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 10) %>% - set_antimalarial_resistance(drug = 3, - timesteps = c(0, 30), - artemisinin_resistance = c(0.03, 0.3), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.03, 0.3), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 30) -> parameters - - drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) - timestep <- 25 - - resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, - drugs = drugs, - timestep = timestep) - - expected_resistance_parameters <- list() - expected_resistance_parameters$artemisinin_resistance_proportion <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$partner_drug_resistance_proportion <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$slow_parasite_clearance_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$early_treatment_failure_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$late_clinical_failure_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$late_parasitological_failure_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$reinfection_during_prophylaxis_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$dt_slow_parasite_clearance <- c(10, 30, 20, 10, 20, 30, 30, 30, 20, 10, 30, 10, 20, 30, 20) - - expect_identical(resistance_parameters, expected = expected_resistance_parameters) - - }) - test_that('get_antimalarial_resistance_parameters() correctly retrieves parameters when not all drugs assigned resistance', { - - get_parameters(overrides = list(human_population = 10000)) %>% - set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% - set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% - set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% - set_equilibrium(init_EIR = 20) %>% - set_antimalarial_resistance(drug = 2, - timesteps = c(0, 20), - artemisinin_resistance = c(0.02, 0.2), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0.02, 0.2), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 20) -> parameters - - drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) - timestep <- 25 - - resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, - drugs = drugs, - timestep = timestep) - - expected_resistance_parameters <- list() - expected_resistance_parameters$artemisinin_resistance_proportion <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$partner_drug_resistance_proportion <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$slow_parasite_clearance_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$early_treatment_failure_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$late_clinical_failure_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$late_parasitological_failure_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$reinfection_during_prophylaxis_probability <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - expected_resistance_parameters$dt_slow_parasite_clearance <- c(5, 5, 20, 5, 20, 5, 5, 5, 20, 5, 5, 5, 20, 5, 20) - - expect_identical(resistance_parameters, expected = expected_resistance_parameters) - - }) + + get_parameters(overrides = list(human_population = 10000)) %>% + set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% + set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% + set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% + set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% + set_equilibrium(init_EIR = 20) %>% + set_antimalarial_resistance(drug = 2, + timesteps = c(0, 20), + artemisinin_resistance_proportion = c(0.02, 0.2), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.02, 0.2), + early_treatment_failure_probability = c(0.02, 0.2), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 20) -> parameters + + drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) + timestep <- 25 + + resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, + drugs = drugs, + timestep = timestep) + + expected_resistance_parameters <- list() + expected_resistance_parameters$artemisinin_resistance_proportion <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) + expected_resistance_parameters$slow_parasite_clearance_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$early_treatment_failure_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) + expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) + expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) + expected_resistance_parameters$dt_slow_parasite_clearance <- c(5, 5, 20, 5, 20, 5, 5, 5, 20, 5, 5, 5, 20, 5, 20) + + expect_identical(resistance_parameters, expected = expected_resistance_parameters) + +}) test_that('get_antimalarial_resistance_parameters() returns an error when antimalarial resistance has not been parameterised', { - - get_parameters(overrides = list(human_population = 10000)) %>% - set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% - set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% - set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% - set_equilibrium(init_EIR = 20) -> parameters - - drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) - timestep <- 25 - - - expect_error(get_antimalarial_resistance_parameters(parameters = parameters, - drugs = drugs, - timestep = timestep), - "Error: Antimalarial resistance has not been parameterised; antimalarial_resistance = FALSE") -}) \ No newline at end of file + + get_parameters(overrides = list(human_population = 10000)) %>% + set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% + set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% + set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% + set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% + set_equilibrium(init_EIR = 20) -> parameters + + drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) + timestep <- 25 + + + expect_error(get_antimalarial_resistance_parameters(parameters = parameters, + drugs = drugs, + timestep = timestep), + "Error: Antimalarial resistance has not been parameterised; antimalarial_resistance = FALSE") +}) diff --git a/tests/testthat/test-infection-integration.R b/tests/testthat/test-infection-integration.R index 70a0ec6f..83d7def9 100644 --- a/tests/testthat/test-infection-integration.R +++ b/tests/testthat/test-infection-integration.R @@ -6,7 +6,7 @@ test_that('simulate_infection integrates different types of infection and schedu )) events <- create_events(parameters) renderer <- mock_render(timestep) - + age <- c(20, 24, 5, 39, 20, 24, 5, 39) * 365 immunity <- c(.2, .3, .5, .9, .2, .3, .5, .9) asymptomatics <- mockery::mock() @@ -15,7 +15,7 @@ test_that('simulate_infection integrates different types of infection and schedu id = individual::DoubleVariable$new(immunity), state = list(get_index_of = mockery::mock(asymptomatics)) ) - + bitten <- individual::Bitset$new(population)$insert(c(1, 3, 5, 7)) boost_immunity_mock <- mockery::mock() infected <- individual::Bitset$new(population)$insert(c(1, 3, 5)) @@ -27,7 +27,7 @@ test_that('simulate_infection integrates different types of infection and schedu treated <- individual::Bitset$new(population)$insert(3) treated_mock <- mockery::mock(treated) schedule_mock <- mockery::mock() - + mockery::stub(simulate_infection, 'boost_immunity', boost_immunity_mock) mockery::stub(simulate_infection, 'calculate_infections', infection_mock) mockery::stub(simulate_infection, 'calculate_clinical_infections', clinical_infection_mock) @@ -45,7 +45,7 @@ test_that('simulate_infection integrates different types of infection and schedu timestep, renderer ) - + mockery::expect_args( boost_immunity_mock, 1, @@ -55,7 +55,7 @@ test_that('simulate_infection integrates different types of infection and schedu 5, parameters$ub ) - + mockery::expect_args( infection_mock, 1, @@ -65,7 +65,7 @@ test_that('simulate_infection integrates different types of infection and schedu renderer, timestep ) - + mockery::expect_args( clinical_infection_mock, 1, @@ -75,7 +75,7 @@ test_that('simulate_infection integrates different types of infection and schedu renderer, timestep ) - + mockery::expect_args( severe_infection_mock, 1, @@ -85,7 +85,7 @@ test_that('simulate_infection integrates different types of infection and schedu parameters, renderer ) - + mockery::expect_args( treated_mock, 1, @@ -95,7 +95,7 @@ test_that('simulate_infection integrates different types of infection and schedu timestep, renderer ) - + mockery::expect_args( schedule_mock, 1, @@ -125,7 +125,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati booster_coverage = matrix(1), booster_profile = list(rtss_booster_profile) ) - + variables <- list( state = individual::CategoricalVariable$new( c('D', 'S', 'A', 'U', 'Tr'), @@ -137,7 +137,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati pev_profile = individual::IntegerVariable$new(c(-1, 1, 2, -1)), ib = individual::DoubleVariable$new(c(.2, .3, .5, .9)) ) - + immunity_mock <- mockery::mock(c(.2, .3, .4)) weibull_mock <- mockery::mock(.2) vaccine_antibodies_mock <- mockery::mock(c(2, 3)) @@ -148,7 +148,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati mockery::stub(calculate_infections, 'calculate_pev_antibodies', vaccine_antibodies_mock) mockery::stub(calculate_infections, 'calculate_pev_efficacy', vaccine_efficacy_mock) mockery::stub(calculate_infections, 'bernoulli_multi_p', bernoulli_mock) - + # remove randomness from vaccine parameters mockery::stub( calculate_infections, @@ -158,9 +158,9 @@ test_that('calculate_infections works various combinations of drug and vaccinati }, depth = 4 ) - + bitten_humans <- individual::Bitset$new(4)$insert(c(1, 2, 3, 4)) - + infections <- calculate_infections( variables, bitten_humans, @@ -168,9 +168,9 @@ test_that('calculate_infections works various combinations of drug and vaccinati mock_render(timestep), timestep ) - + expect_equal(infections$to_vector(), 3) - + mockery::expect_args(immunity_mock, 1, c(.3, .5, .9), parameters) mockery::expect_args( weibull_mock, @@ -179,7 +179,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati parameters$drug_prophylaxis_shape[[2]], parameters$drug_prophylaxis_scale[[2]] ) - + mockery::expect_args( vaccine_antibodies_mock, 1, @@ -203,13 +203,13 @@ test_that('calculate_infections works various combinations of drug and vaccinati 1, c(.2 * .8 * .8, .3 * .7, .4) ) - + }) test_that('calculate_clinical_infections correctly samples clinically infected', { timestep <- 5 parameters <- get_parameters() - + variables <- list( ica = individual::DoubleVariable$new(c(.2, .3, .5, .9)), icm = individual::DoubleVariable$new(c(.2, .3, .5, .9)), @@ -217,25 +217,25 @@ test_that('calculate_clinical_infections correctly samples clinically infected', last_boosted_ica = individual::DoubleVariable$new(c(-1, -1, 1, -1)), last_boosted_id = individual::DoubleVariable$new(c(-1, -1, 1, -1)) ) - + immunity_mock <- mockery::mock(c(.2, .3, .4)) boost_mock <- mockery::mock() mockery::stub(calculate_clinical_infections, 'boost_immunity', boost_mock) - + mockery::stub(calculate_clinical_infections, 'clinical_immunity', immunity_mock) bernoulli_mock <- mockery::mock(c(1, 3)) mockery::stub(calculate_clinical_infections, 'bernoulli_multi_p', bernoulli_mock) - + infections <- individual::Bitset$new(4)$insert(c(2, 3, 4)) - + clinical_infections <- calculate_clinical_infections( variables, infections, parameters ) - + expect_equal(clinical_infections$to_vector(), c(2, 4)) - + mockery::expect_args( immunity_mock, 1, @@ -243,7 +243,7 @@ test_that('calculate_clinical_infections correctly samples clinically infected', c(.3, .5, .9), parameters ) - + mockery::expect_args( bernoulli_mock, 1, @@ -324,24 +324,24 @@ test_that('calculate_treated correctly samples treated and updates the drug stat parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.9, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.3, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.9, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 15) clinical_infections <- individual::Bitset$new(20)$insert(1:20) @@ -351,17 +351,24 @@ test_that('calculate_treated correctly samples treated and updates the drug stat state = list(queue_update = mockery::mock()), infectivity = list(queue_update = mockery::mock()), drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) + drug_time = list(queue_update = mockery::mock()), + dt = list(queue_update = mockery::mock()) ) + renderer <- individual::Render$new(timesteps = timestep) + # Set up seek_treatment mock and instruct calculate_treated() to return it when sample_bitset() called: seek_treatment <- individual::Bitset$new(20)$insert(c(1:10)) seek_treatment_mock <- mockery::mock(seek_treatment) mockery::stub(where = calculate_treated, what = 'sample_bitset', how = seek_treatment_mock) + # Set up drugs mock and instruct it to return it when sample.int() called: mock_drugs <- mockery::mock(c(2, 1, 1, 1, 2, 2, 2, 1, 2, 1)) mockery::stub(calculate_treated, 'sample.int', mock_drugs) - bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), c(1, 2, 3, 4, 5, 6, 7)) + # Set up bernoulli mock and instruct calculate_treated to return it when bernoulli_multi_p() called: + bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), + c(1, 2, 3, 4, 5, 6, 7), + c(1)) mockery::stub(calculate_treated, 'bernoulli_multi_p', bernoulli_mock) calculate_treated( @@ -369,7 +376,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat clinical_infections, parameters, timestep, - mock_render(timestep) + renderer ) mockery::expect_args( @@ -391,13 +398,25 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::expect_args( bernoulli_mock, 1, - c(1.0, 0.9, 0.9, 0.9, 1.0, 1.0, 1.0, 0.9, 1, 0.9) + c(0.9, 0.95, 0.95, 0.95, 0.9, 0.9, 0.9, 0.95, 0.9, 0.95) + ) + + mockery::expect_args( + bernoulli_mock, + 2, + c(0.73, 0.9, 0.9, 0.9, 0.73, 0.73, 0.73, 0.9, 0.73) ) mockery::expect_args( bernoulli_mock, 2, - parameters$drug_efficacy[c(2, 1, 1, 1, 2, 2, 2, 1, 2)] + 1 - (unlist(parameters$artemisinin_resistance_proportion[c(2, 1, 1, 1, 2, 2, 2, 1, 2)]) * unlist(parameters$early_treatment_failure_probability[c(2, 1, 1, 1, 2, 2, 2, 1, 2)])) + ) + + mockery::expect_args( + bernoulli_mock, + 3, + unlist(parameters$artemisinin_resistance_proportion[c(2, 1, 1, 1, 2, 2, 2)]) * unlist(parameters$slow_parasite_clearance_probability[c(2, 1, 1, 1, 2, 2, 2)]) ) expect_bitset_update(variables$state$queue_update, 'Tr', c(1, 2, 3, 4, 5, 6, 7)) @@ -408,7 +427,8 @@ test_that('calculate_treated correctly samples treated and updates the drug stat ) expect_bitset_update(variables$drug$queue_update, c(2, 1, 1, 1, 2, 2, 2), c(1, 2, 3, 4, 5, 6, 7)) expect_bitset_update(variables$drug_time$queue_update, 5, c(1, 2, 3, 4, 5, 6, 7)) - + expect_bitset_update(variables$dt$queue_update, 5, c(2, 3, 4, 5, 6, 7), 1) + expect_bitset_update(variables$dt$queue_update, 15, c(1), 2) }) test_that('calculate_treated correctly samples treated and updates the drug state when resistance not set for all drugs', { @@ -421,13 +441,13 @@ test_that('calculate_treated correctly samples treated and updates the drug stat parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0.8, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.9, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.8, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.2, + early_treatment_failure_probability = 0.3, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 20) # Establish Bitset of clinically infected individuals @@ -444,7 +464,8 @@ test_that('calculate_treated correctly samples treated and updates the drug stat state = list(queue_update = mockery::mock()), infectivity = list(queue_update = mockery::mock()), drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) + drug_time = list(queue_update = mockery::mock()), + dt = list(queue_update = mockery::mock()) ) # Create a Bitset of individuals seeking treatment individuals: @@ -463,7 +484,9 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::stub(calculate_treated, 'sample.int', mock_drugs) # Create a bernoulli_mock of i) individuals susceptible, and ii) individuals successfully treated: - bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), c(1, 2, 3, 4, 5, 6, 7)) + bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), + c(1, 2, 3, 4, 5, 6, 7), + c(1)) # Specify that when calculate_treated() calls bernoulli_multi_p() it returns the bernoulli_mock: mockery::stub(calculate_treated, 'bernoulli_multi_p', bernoulli_mock) @@ -502,7 +525,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::expect_args( bernoulli_mock, 1, - c(0.28, 1, 1, 1, 0.28, 0.28, 0.28, 1, 0.28, 1) # (1 - (art_prop * etf_prob)) + parameters$drug_efficacy[c(2, 1, 1, 1, 2, 2, 2, 1, 2, 1)] ) # Check that the secnd time bernoulli_mock was called (bernoulli_multi_p()) the arguments used in @@ -510,7 +533,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::expect_args( bernoulli_mock, 2, - parameters$drug_efficacy[c(2, 1, 1, 1, 2, 2, 2, 1, 2)] + c(0.76, 1, 1, 1, 0.76, 0.76, 0.76, 1, 0.76) ) # Check that update queued that updates the state of successfully treated individuals to "Tr" @@ -543,22 +566,37 @@ test_that('calculate_treated correctly samples treated and updates the drug stat 5, c(1, 2, 3, 4, 5, 6, 7) ) + + # Check that update queued for dt for the non-slow parasite clearance individuals is correct: + expect_bitset_update( + variables$dt$queue_update, + parameters$dt, + c(2, 3, 4, 5, 6, 7), + 1) + + # Check that update queued for dt for the slow parasite clearance individuals is correct: + expect_bitset_update( + variables$dt$queue_update, + unlist(parameters$dt_slow_parasite_clearance), + c(1), + 2) + }) test_that('schedule_infections correctly schedules new infections', { parameters <- get_parameters(list(human_population = 20)) variables <- create_variables(parameters) - + infections <- individual::Bitset$new(20)$insert(1:20) clinical_infections <- individual::Bitset$new(20)$insert(5:15) treated <- individual::Bitset$new(20)$insert(7:12) - + infection_mock <- mockery::mock() asymp_mock <- mockery::mock() - + mockery::stub(schedule_infections, 'update_infection', infection_mock) mockery::stub(schedule_infections, 'update_to_asymptomatic_infection', asymp_mock) - + schedule_infections( variables, clinical_infections, @@ -567,15 +605,15 @@ test_that('schedule_infections correctly schedules new infections', { parameters, 42 ) - + actual_infected <- mockery::mock_args(infection_mock)[[1]][[5]]$to_vector() actual_asymp_infected <- mockery::mock_args(asymp_mock)[[1]][[4]]$to_vector() - + expect_equal( actual_infected, c(5, 6, 13, 14, 15) ) - + expect_equal( actual_asymp_infected, c(1, 2, 3, 4, 16, 17, 18, 19, 20) @@ -587,7 +625,7 @@ test_that('prophylaxis is considered for medicated humans', { parameters <- set_drugs(parameters, list(AL_params, DHA_PQP_params)) events <- create_events(parameters) timestep <- 50 - + variables = list( state = individual::CategoricalVariable$new( c('D', 'S', 'A', 'U', 'Tr'), @@ -599,11 +637,11 @@ test_that('prophylaxis is considered for medicated humans', { pev_profile = individual::IntegerVariable$new(c(-1, -1, -1, -1)), ib = individual::DoubleVariable$new(c(.2, .3, .5, .9)) ) - + bitten <- individual::Bitset$new(4)$insert(seq(4)) m <- mockery::mock(seq(3)) mockery::stub(calculate_infections, 'bernoulli_multi_p', m) - + calculate_infections( variables, bitten, @@ -611,7 +649,7 @@ test_that('prophylaxis is considered for medicated humans', { mock_render(timestep), timestep ) - + expect_equal( mockery::mock_args(m)[[1]][[1]], c(2.491951e-07, 2.384032e-01, 5.899334e-01), @@ -623,17 +661,17 @@ test_that('boost_immunity respects the delay period', { level <- c(2.4, 1.2, 0., 4.) immunity <- individual::DoubleVariable$new(level) last_boosted <- individual::DoubleVariable$new(c(11, 5, 1, 13)) - + level_mock <- mockery::mock() mockery::stub(boost_immunity, 'immunity_variable$queue_update', level_mock) - + last_mock <- mockery::mock() mockery::stub(boost_immunity, 'last_boosted_variable$queue_update', last_mock) - + index <- individual::Bitset$new(4)$insert(seq(4)) timestep <- 15 delay <- 4 - + boost_immunity( immunity, index, @@ -641,14 +679,14 @@ test_that('boost_immunity respects the delay period', { timestep, delay ) - + mockery::expect_args( level_mock, 1, c(3.4, 2.2, 1), seq(3) ) - + mockery::expect_args( last_mock, 1, @@ -661,18 +699,18 @@ test_that('boost_immunity respects the delay period', { level <- c(2.4, 1.2, 0., 4., 0.) immunity <- individual::DoubleVariable$new(level) last_boosted <- individual::DoubleVariable$new(c(11, 5, 1, 13, -1)) - + index <- individual::Bitset$new(5) index$insert(seq(5)) timestep <- 15 delay <- 4 - + level_mock <- mockery::mock() mockery::stub(boost_immunity, 'immunity_variable$queue_update', level_mock) - + last_mock <- mockery::mock() mockery::stub(boost_immunity, 'last_boosted_variable$queue_update', last_mock) - + boost_immunity( immunity, index, @@ -680,14 +718,14 @@ test_that('boost_immunity respects the delay period', { timestep, delay ) - + mockery::expect_args( level_mock, 1, c(3.4, 2.2, 1, 1), c(seq(3), 5) ) - + mockery::expect_args( last_mock, 1, @@ -700,18 +738,18 @@ test_that('boost_immunity does not update when there is no-one to update', { level <- c(2.4, 1.2, 0., 4., 0.) immunity <- individual::DoubleVariable$new(level) last_boosted <- individual::DoubleVariable$new(c(11, 5, 1, 13, -1)) - + index <- individual::Bitset$new(5) index$insert(seq(5)) timestep <- 15 delay <- 4 - + level_mock <- mockery::mock() mockery::stub(boost_immunity, 'immunity$queue_update', level_mock) - + last_mock <- mockery::mock() mockery::stub(boost_immunity, 'last_boosted$queue_update', last_mock) - + boost_immunity( immunity, index, @@ -733,11 +771,11 @@ test_that('update_severe_disease renders with no infections', { severe_incidence_rendering_max_ages = 5 * 365 )) variables <- create_variables(parameters) - + render_function <- mockery::mock() mockery::stub(update_severe_disease, 'incidence_renderer', render_function) empty <- individual::Bitset$new(population) - + update_severe_disease( timestep, empty, @@ -745,7 +783,7 @@ test_that('update_severe_disease renders with no infections', { parameters, renderer ) - + mockery::expect_args( render_function, 1, @@ -768,13 +806,13 @@ test_that('calculate_treated returns empty Bitset when there is no clinical trea parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) clinical_infections <- individual::Bitset$new(20)$insert(1:20) timestep <- 5 @@ -804,13 +842,13 @@ test_that('calculate_treated returns empty Bitset when the clinically_infected i parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) clinical_infections <- individual::Bitset$new(20) timestep <- 5 @@ -835,30 +873,30 @@ test_that('calculate_treated returns empty Bitset when the clinically_infected i test_that('calculate_treated() returns an empty Bitset when the parameter list contains no clinical treatment or resistance parameters', { - parameters <- get_parameters() - clinical_infections <- individual::Bitset$new(20)$insert(1:20) - timestep <- 5 - events <- create_events(parameters) - variables <- list( - state = list(queue_update = mockery::mock()), - infectivity = list(queue_update = mockery::mock()), - drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) - ) - renderer <- individual::Render$new(timesteps = 10) - - treated <- calculate_treated(variables = variables, - clinical_infections = clinical_infections, - parameters = parameters, - timestep = timestep, - renderer = renderer) - - expect_identical(object = treated$size(), expected = 0, info = "Error: calculate_treated() returning non-zero number of treated individuals + parameters <- get_parameters() + clinical_infections <- individual::Bitset$new(20)$insert(1:20) + timestep <- 5 + events <- create_events(parameters) + variables <- list( + state = list(queue_update = mockery::mock()), + infectivity = list(queue_update = mockery::mock()), + drug = list(queue_update = mockery::mock()), + drug_time = list(queue_update = mockery::mock()) + ) + renderer <- individual::Render$new(timesteps = 10) + + treated <- calculate_treated(variables = variables, + clinical_infections = clinical_infections, + parameters = parameters, + timestep = timestep, + renderer = renderer) + + expect_identical(object = treated$size(), expected = 0, info = "Error: calculate_treated() returning non-zero number of treated individuals in the absence of clinical treatment or resistance parameters") }) -test_that('Number of treatment failures matches number of individuals treated when artemisinin resistance proportion and - early treatment failure probability both set to 1', { +test_that('Number of treatment failures matches number of individuals treated when artemisinin resistance + proportion and early treatment failure probability both set to 1', { parameters <- get_parameters() parameters <- set_drugs(parameters = parameters, drugs = list(AL_params, SP_AQ_params)) parameters <- set_clinical_treatment(parameters = parameters, @@ -874,24 +912,24 @@ test_that('Number of treatment failures matches number of individuals treated wh parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 1, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 1, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 1, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 1, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 1, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 1, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 1, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 1, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 20) clinical_infections <- individual::Bitset$new(100) @@ -907,8 +945,8 @@ test_that('Number of treatment failures matches number of individuals treated wh timestep = timestep, renderer = renderer) - expect_identical(renderer$to_dataframe()[timestep,'n_early_treatment_failure'], renderer$to_dataframe()[timestep,'n_treated'], info = "Error: Number of - early treatment failures does not match number of treated individuals when artemisinin resistance proportion and + expect_identical(renderer$to_dataframe()[timestep,'n_early_treatment_failure'], renderer$to_dataframe()[timestep,'n_treated'] - renderer$to_dataframe()[timestep,'n_drug_efficacy_failures'], info = "Error: Number of + early treatment failures does not match number of treated individuals (minus drug efficacy failures) when artemisinin resistance proportion and and early treatment failure probability both equal 1") }) @@ -919,13 +957,13 @@ test_that('calculate_treated() successfully adds additional resistance columns t parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.5, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.5, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) clinical_infections <- individual::Bitset$new(20)$insert(1:20) @@ -935,7 +973,8 @@ test_that('calculate_treated() successfully adds additional resistance columns t state = list(queue_update = mockery::mock()), infectivity = list(queue_update = mockery::mock()), drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) + drug_time = list(queue_update = mockery::mock()), + dt = list(queue_update = mockery::mock()) ) renderer <- individual::Render$new(timesteps = 10) @@ -945,7 +984,12 @@ test_that('calculate_treated() successfully adds additional resistance columns t timestep = timestep, renderer = renderer) - calculate_treated_column_names <- c("ft", "n_treated", "n_early_treatment_failure", "n_treat_eff_fail", "n_treat_success") + calculate_treated_column_names <- c("ft", + "n_treated", + "n_drug_efficacy_failures", + "n_early_treatment_failure", + "n_slow_parasite_clearance", + "n_successfully_treated") expect_identical(sum(calculate_treated_column_names %in% colnames(renderer$to_dataframe())), length(calculate_treated_column_names), "calculate_treated() not renderering all resistance columns when resistance is present, clinical treatment coverage is non-zero, and the Bitset of clinically_infected individuals input is of non-zero length.") diff --git a/vignettes/Antimalarial_Resistance.Rmd b/vignettes/Antimalarial_Resistance.Rmd index 7bf197b7..5b6acd40 100644 --- a/vignettes/Antimalarial_Resistance.Rmd +++ b/vignettes/Antimalarial_Resistance.Rmd @@ -14,7 +14,6 @@ knitr::opts_chunk$set( ) ``` - ```{r setup} # Load the requisite packages: @@ -31,7 +30,7 @@ One of the major threats to the continued success of efforts to reduce the burde Resistance to the artemisinin component of an ACT can result either in slow parasite clearance (SPC), in which treatment with an ACT takes longer than 3 days to fully clear patients with resistant parasites, or early treatment failure (ETF), in which the ACT fails to clear the infection and the individual develops a clinical infection. Resistance to the partner drug, where the partner drug fails to clear the parasite after the artemisinin derivative is depleted, results in infections recrudescing to either clinical (D) or asymptomatic infections (A). Resistance to the partner drug can also result in individuals developing a novel, resistant infection following treatment, as the prophylaxis provided by the ACT fails to protect the individual against reinfection by a resistant strain. In the following vignette, we illustrate how to parameterise and run `malariasimulation` simulations with resistance to ACTs deployed as a clinical treatment ## Using set_antimalarial_resistance() to parameterise resistance -Simulations capturing the effects of resistance to clinical treatment using antimalarial drugs are parameterised using the `set_antimalarial_resistance()` function. This function appends user-defined resistance parameters to a `malariasimulation` parameter list and accepts ten inputs. The first is a list of `malariasimulation` parameters to append the resistance parameters to, and the second the index of the `drug` for which resistance is being parameterised, as set using the `set_drugs()` function. The `set_antimalarial_resistance()` function requires the `timesteps`, `artemisinin_resistance`, `partner_drug_resistance`, `slow_parasite_clearance_prob`, `early_treatment_failure_prob`, `late_clinical_failure_prob`, `late_parasitological_failure_prob`, and `reinfection_prob` inputs to be of equal length so that, for each time step in which an update occurs, a value is available for each parameter. Finally, the `slow_parasite_clearance_time` parameter represents the mean residence time, in days, for artemisinin-resistant individuals experiencing slow parasite clearance (SPC) in the Treated compartment, and must be input as a single, positive value. +Simulations capturing the effects of resistance to clinical treatment using antimalarial drugs are parameterised using the `set_antimalarial_resistance()` function. This function appends user-defined resistance parameters to a `malariasimulation` parameter list and accepts ten inputs. The first is a list of `malariasimulation` parameters to append the resistance parameters to, and the second the index of the `drug` for which resistance is being parameterised, as set using the `set_drugs()` function. The `set_antimalarial_resistance()` function requires the `timesteps`, `artemisinin_resistance_proportion`, `partner_drug_resistance_proportion_proportion`, `slow_parasite_clearance_probability`, `early_treatment_failure_probability`, `late_clinical_failure_probability`, `late_parasitological_failure_prob`, and `reinfection_during_prophylaxis_probability` inputs to be of equal length so that, for each time step in which an update occurs, a value is available for each parameter. Finally, the `slow_parasite_clearance_time` parameter represents the mean residence time, in days, for artemisinin-resistant individuals experiencing slow parasite clearance (SPC) in the Treated compartment, and must be input as a single, positive value. ## Simulating static resistance To illustrate how to parameterise resistance to an ACT using the `set_antimalarial_resistance()` function, we'll set-up and run three simulations. The first simulates malaria transmission in the absence of interventions or resistance. The second simulates a simple regime of clinical treatment in which 80% of clinical cases are treated with artemether lumefantrine (AL), beginning after one year, in the absence of antimalarial resistance. The third simulates the same clinical treatment programme but with resistance to the artemisinin component of AL emerging after two years. For illustrative purposes, we assume that the proportion of infections resistant to the artemisinin component of AL increases from 0% to 80%, and that these infections have a 90% chance of resulting in early treatment failure. @@ -75,13 +74,13 @@ simparams_clin_treatment <- set_clinical_treatment(parameters = simparams_clin_t simparams_resistance <- set_antimalarial_resistance(parameters = simparams_clin_treatment, drug = 1, timesteps = c(0, resistance_start), - artemisinin_resistance = c(0, 0.8), - partner_drug_resistance = rep(0, 2), - slow_parasite_clearance_prob = rep(0, 2), - early_treatment_failure_prob = c(0, 0.9), - late_clinical_failure_prob = rep(0, 2), - late_parasitological_prob = rep(0, 2), - reinfection_prob = rep(0, 2), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = rep(0, 2), + slow_parasite_clearance_probability = rep(0, 2), + early_treatment_failure_probability = c(0, 0.9), + late_clinical_failure_probability = rep(0, 2), + late_parasitological_failure_probability = rep(0, 2), + reinfection_during_prophylaxis_probability = rep(0, 2), slow_parasite_clearance_time = 10) ``` @@ -196,13 +195,13 @@ simparams <- set_clinical_treatment(parameters = simparams, simparams <- set_antimalarial_resistance(parameters = simparams, drug = 1, timesteps = resistance_update_timesteps, - artemisinin_resistance = resistance_updates, - partner_drug_resistance = rep(0, length(resistance_update_timesteps)), - slow_parasite_clearance_prob = rep(0, length(resistance_update_timesteps)), - early_treatment_failure_prob = early_treatment_failure_updates, - late_clinical_failure_prob = rep(0, length(resistance_update_timesteps)), - late_parasitological_prob = rep(0, length(resistance_update_timesteps)), - reinfection_prob = rep(0, length(resistance_update_timesteps)), + artemisinin_resistance_proportion = resistance_updates, + partner_drug_resistance_proportion = rep(0, length(resistance_update_timesteps)), + slow_parasite_clearance_probability = rep(0, length(resistance_update_timesteps)), + early_treatment_failure_probability = early_treatment_failure_updates, + late_clinical_failure_probability = rep(0, length(resistance_update_timesteps)), + late_parasitological_failure_probability = rep(0, length(resistance_update_timesteps)), + reinfection_during_prophylaxis_probability = rep(0, length(resistance_update_timesteps)), slow_parasite_clearance_time = 10) # Calibrate the parameters to an initial EIR: @@ -259,5 +258,153 @@ text(x = resistance_update_timesteps[2:5] + 30, y = 0.6, labels = paste0("Art. R Looking at the figure, we can see that the *Pf*PR~2-10~ decreases over the two years following the onset of clinical treatment in the absence of artemisinin resistance. However, as resistance is introduced and increases through time, the *Pf*PR~2-10~ increases towards the pre-intervention seasonal peak as SP-AQ becomes increasingly ineffective in the treatment of clinical cases of malaria. +## Simulating antimalarial resistance with multiple resistance outcomes + +As we've discussed, resistance to an ACT can manifest in multiple ways. For instance, resistance to the artemisinin component of an ACT can result in either early treatment failure or slow parasite clearance. + +Using `malariasimulation`, we can simulate the effects of multiple potential outcomes of resistance on malaria transmission dynamics. To illustrate, we'll parameterise and run a series of simulations in is i) no clinical treatment, ii) no resistance, iii) resistance with early treatment failure, iv) resistance with slow parasite clearance, and v) resistance with early treatment failure and slow parasite clearance. + +### Parameterisation +```{r} + +# Determine the number of timesteps to run for: +timesteps <- 365 * 6 + +# Set up a list to store the simulation parameter lists in: +simulation_parameters <- list() + +# Establish a list of the base parameters with no clinical treatment or antimalarial resistance: +get_parameters(overrides = list(human_population = 1000)) -> simulation_parameters$base + +# Establish a parameter list with clinical treatment starting after one year: +simulation_parameters$base |> + set_drugs(drugs = list(AL_params)) |> + set_clinical_treatment(drug = 1, timesteps = (365 * 1) + 1, coverages = c(0.8)) |> + set_equilibrium(init_EIR = 16) -> simulation_parameters$treatment + +# Set the equilibrium for the base parameters: +simulation_parameters$base |> + set_equilibrium(init_EIR = 16) -> simulation_parameters$base + +# Establish a parameter list with clinical treatment and early treatment failure +simulation_parameters$treatment |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, (365 * 3) + 1), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0, 0), + early_treatment_failure_probability = c(0, 0.8), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 10) -> simulation_parameters$etf + +# Establish a parameter list with clinical treatment and slow parasite clearance +simulation_parameters$treatment |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, (365 * 3) + 1), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0, 0.8), + early_treatment_failure_probability = c(0, 0), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 10) -> simulation_parameters$spc + +# Establish a parameter list with clinical treatment, early treatment failure and slow parasite clearance: +simulation_parameters$treatment |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, (365 * 3) + 1), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0, 0.8), + early_treatment_failure_probability = c(0, 0.8), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 10) -> simulation_parameters$etf_spc + +``` + +### Simulation +We can now use our lists of `malariasimulation` parameters to run the simulations. + +```{r} + +# Open a list to store the simulation outputs in: +simulation_outputs <- list() + +# Run the simulations +for(i in seq(length(simulation_parameters))) { + + # Run the i-th simulation + simulation_temp <- run_simulation(timesteps = timesteps, + parameters = simulation_parameters[[i]]) + + # Append the simulation identifier: + simulation_temp$identifier <- names(simulation_parameters)[i] + + # Append the ith simulation outputs to the combined simulation dataframe: + simulation_outputs[[names(simulation_parameters)[i]]] <- simulation_temp + + # Print the number of columns in the i-th simulation outputs dataframe: + print(ncol(simulation_temp)) +} + +``` + +### Visualisation + +We can compare the effects of independent resistance outcomes with combined resistance outcomes visually. In the following plot, we compare the *Pf*PR~2-10~ between a baseline without any clinical treatment or antimalarial resistance, a clinical-treatment only run, a clinical treatment with early treatment failure run, a clinical treatment with slow parasite clearance run, and a clinical treatment with both early treatment failure and slow parasite clearance run. + +```{R} + +# Open a new plotting window and add a grid: +plot.new(); par(mar = c(4, 4, 1, 1), new = TRUE) + +# Plot malaria prevalence in 2-10 years through time: +plot(x = simulation_outputs$base$timestep, + y = simulation_outputs$base$n_detect_730_3650/simulation_outputs$base$n_730_3650, + xlab = "Time (days)", + ylab = expression(paste(italic(Pf),"PR"[2-10])), cex = 0.8, + ylim = c(0, 1), type = "l", lwd = 2, xaxs = "i", yaxs = "i", + col = cols[3]) + +# Add the dynamics for no-intervention simulation +lines(x = simulation_outputs$treatment$timestep, + y = simulation_outputs$treatment$n_detect_730_3650/simulation_outputs$treatment$n_730_3650, + col = cols[4]) + +lines(x = simulation_outputs$etf$timestep, + y = simulation_outputs$etf$n_detect_730_3650/simulation_outputs$etf$n_730_3650, + col = cols[5]) + +lines(x = simulation_outputs$spc$timestep, + y = simulation_outputs$spc$n_detect_730_3650/simulation_outputs$spc$n_730_3650, + col = cols[6]) + +lines(x = simulation_outputs$etf_spc$timestep, + y = simulation_outputs$etf_spc$n_detect_730_3650/simulation_outputs$etf_spc$n_730_3650, + col = cols[7]) + +# Add vlines to indicate when SP-AQ were administered: +abline(v = 365 + 1, lty = "dashed", lwd = 1) +text(x = (365 * 1) - 40, y = 0.6, labels = "Treatment Introduced", adj = 0, cex = 0.8, srt = 90) + +abline(v = (365 * 3) + 1, lty = "dashed", lwd = 1) +text(x = (365 * 3) - 40, y = 0.6, labels = "Resistance Introduced", adj = 0, cex = 0.8, srt = 90) + +# Add gridlines: +grid(lty = 2, col = "grey80", nx = NULL, ny = NULL, lwd = 0.5); box() + +# Add a legend: +legend(x = 3000, y = 0.99, legend = c("Baseline", "Treatment", "ETF-only", "SPC-only", "ETF and SPC"), + col= c(cols[3:7]), box.col = "white", + lwd = 1, lty = c(1, 1), cex = 0.8) + + +``` + ## References Slater, H.C., Griffin, J.T., Ghani, A.C. and Okell, L.C., 2016. Assessing the potential impact of artemisinin and partner drug resistance in sub-Saharan Africa. Malaria journal, 15(1), pp.1-11. \ No newline at end of file