From c71e9b0673e4c3f7fc7f6acd3196989f54e589e4 Mon Sep 17 00:00:00 2001 From: Thomas Brewer Date: Thu, 8 Feb 2024 18:12:31 +0000 Subject: [PATCH] Implented the second round of changes requested in the pull request --- R/antimalarial_resistance.R | 100 ++++++++++++++---- R/human_infection.R | 27 ++--- R/model.R | 3 +- R/parameters.R | 1 + man/get_antimalarial_resistance_parameters.Rd | 19 ++++ man/get_parameters.Rd | 1 + man/run_simulation.Rd | 3 +- man/set_antimalarial_resistance.Rd | 5 +- vignettes/Antimalarial_Resistance.Rmd | 8 +- 9 files changed, 119 insertions(+), 48 deletions(-) create mode 100644 man/get_antimalarial_resistance_parameters.Rd diff --git a/R/antimalarial_resistance.R b/R/antimalarial_resistance.R index 9d6e7dfe..0aad5315 100644 --- a/R/antimalarial_resistance.R +++ b/R/antimalarial_resistance.R @@ -1,5 +1,7 @@ #' @title Parameterise antimalarial resistance -#' +#' @description +#' Parameterise antimalarial resistance +#' #' @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 @@ -10,6 +12,7 @@ #' @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 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, @@ -20,9 +23,9 @@ set_antimalarial_resistance <- function(parameters, early_treatment_failure_prob, late_clinical_failure_prob, late_parasitological_prob, - reinfection_prob) { + reinfection_prob, + slow_parasite_clearance_time) { - # Check that the number of values input is equal for each resistance parameter if(any(c(length(artemisinin_resistance), length(partner_drug_resistance), length(slow_parasite_clearance_prob), @@ -33,13 +36,11 @@ set_antimalarial_resistance <- function(parameters, stop("Length of one or more resistance parameter vectors does not match time steps specified for update") } - # Ensure resistance proportions bounded between 0 and 1: - if(any(artemisinin_resistance < 0 | artemisinin_resistance > 1 | + if(any(artemisinin_resistance < 0 | artemisinin_resistance > 1 | partner_drug_resistance < 0 | partner_drug_resistance > 1)) { stop("Artemisinin and partner-drug resistance proportions must fall between 0 and 1") } - # Ensure resistance outcome probabilities bounded 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 | @@ -47,45 +48,102 @@ set_antimalarial_resistance <- function(parameters, reinfection_prob < 0 | reinfection_prob > 1)) { stop("Resistance outcome probabilities must fall between 0 and 1") } + + if(length(slow_parasite_clearance_time) != 1) { + stop("Error: length of slow_parasite_clearance_time not equal to 1") + } + + if(slow_parasite_clearance_time <= 0) { + stop("Error: slow_parasite_clearance_time is non-positive") + } - # Set antimalarial_resistance to TRUE: parameters$antimalarial_resistance <- TRUE - # Store the number of drugs for which parameters are available in the parameter list: n_drugs <- length(parameters$drug_efficacy) - # If the drug index falls outside range 1:n_drugs, terminate the operation: if (drug < 1 | drug > n_drugs) { stop('Drug index is invalid, please set drugs using set_drugs') } - # Check the drug_index for the drug we're setting parameters for: drug_index <- which(parameters$antimalarial_resistance_drug == drug) - # If drug_index not already assigned, assign the drug the next available index: if (length(drug_index) == 0) { drug_index <- length(parameters$antimalarial_resistance_drug) + 1 } - # Append the drug for which resistance is being assigned to the generated index: parameters$antimalarial_resistance_drug[[drug_index]] <- drug - - # Append the timesteps on which the resistance proportions are to be updated: parameters$antimalarial_resistance_timesteps[[drug_index]] <- timesteps - - # Append the proportions of all malarial infections that are artemisinin or partner-drug - # resistant: parameters$prop_artemisinin_resistant[[drug_index]] <- artemisinin_resistance parameters$prop_partner_drug_resistant[[drug_index]] <- partner_drug_resistance - - # Append the probabilities that individuals will experience the resistance outcomes: 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$dt_slow_parasite_clearance[[drug_index]] <- slow_parasite_clearance_time + + return(parameters) + +} + +#' @title Retrieve resistance parameters +#' @description +#' Retrieve the resistance parameters associated with the drug each individual receiving clinical +#' treatment has been administered in the current time step. +#' +#' @param parameters the model parameters +#' @param drug vector of integers representing the drugs administered to each individual receiving treatment +#' @param timestep the current time step +get_antimalarial_resistance_parameters <- function(parameters, drugs, timestep) { + + if(!parameters$antimalarial_resistance) { + stop("Error: Antimalarial resistance has not been parameterised; antimalarial_resistance = FALSE") + } + + artemisinin_resistance_proportion <- numeric(length = length(drugs)) + partner_drug_resistance_proportion <- numeric(length = length(drugs)) + slow_parasite_clearance_probability <- numeric(length = length(drugs)) + early_treatment_failure_probability <- numeric(length = length(drugs)) + late_clinical_failure_probability <- numeric(length = length(drugs)) + late_parasitological_failure_probability <- numeric(length = length(drugs)) + reinfection_during_prophylaxis_probability <- numeric(length = length(drugs)) + dt_slow_parasite_clearance <- rep(parameters$dt, length = length(drugs)) + + for(i in seq_along(parameters$antimalarial_resistance_drug)) { + 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] + dt_slow_parasite_clearance[treated_with_drug] <- parameters$dt_slow_parasite_clearance[[i]] + } + + resistance_parameters <- list() + resistance_parameters$artemisinin_resistance_proportion <- artemisinin_resistance_proportion + resistance_parameters$partner_drug_resistance_proportion <- partner_drug_resistance_proportion + resistance_parameters$slow_parasite_clearance_probability <- slow_parasite_clearance_probability + resistance_parameters$early_treatment_failure_probability <- early_treatment_failure_probability + resistance_parameters$late_clinical_failure_probability <- late_clinical_failure_probability + resistance_parameters$late_parasitological_failure_probability <- late_parasitological_failure_probability + resistance_parameters$reinfection_during_prophylaxis_probability <- reinfection_during_prophylaxis_probability + resistance_parameters$dt_slow_parasite_clearance <- dt_slow_parasite_clearance + + if(any(c(length(resistance_parameters$artemisinin_resistance_proportion), + length(resistance_parameters$partner_drug_resistance_proportion), + length(resistance_parameters$slow_parasite_clearance_probability), + length(resistance_parameters$early_treatment_failure_probability), + length(resistance_parameters$late_clinical_failure_probability), + length(resistance_parameters$late_parasitological_failure_probability), + length(resistance_parameters$reinfection_during_prophylaxis_probability), + length(resistance_parameters$dt_slow_parasite_clearance)) != length(drugs))) { + stop("Length of one or more resistance parameter vectors does not match length of drugs vector") + } - # Return the parameter list: - parameters + return(resistance_parameters) } \ No newline at end of file diff --git a/R/human_infection.R b/R/human_infection.R index 59a8c493..d0b0030e 100644 --- a/R/human_infection.R +++ b/R/human_infection.R @@ -269,8 +269,6 @@ calculate_treated <- function( renderer ) { - renderer$render('n_clin_infected', clinical_infections$size(), timestep) - if(clinical_infections$size() == 0) { return(individual::Bitset$new(parameters$human_population)) } @@ -296,27 +294,18 @@ calculate_treated <- function( ) ]) - if(parameters$antimalarial_resistance == TRUE) { - antimalarial_resistance_drug_index <- as.numeric(parameters$antimalarial_resistance_drug)[drugs] - artemisinin_resistance_proportion <- vector() - early_treatment_failure_probability <- vector() - drug_indices <- list(); drug_index <- vector() - - for(i in seq_along(parameters$antimalarial_resistance_drug)) { - drug_indices[[i]] <- which(drugs == i) - drug_index[i] <- which(parameters$antimalarial_resistance_drug == i) - matched_t <- match_timestep(ts = parameters$antimalarial_resistance_timesteps[[drug_index[i]]], t = timestep) - artemisinin_resistance_proportion[drug_indices[[i]]] <- parameters$prop_artemisinin_resistant[[drug_index[i]]][matched_t] - early_treatment_failure_probability[drug_indices[[i]]] <- parameters$early_treatment_failure_prob[[drug_index[i]]][matched_t] - } - - unsuccessful_treatment_probability <- artemisinin_resistance_proportion * early_treatment_failure_probability + 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) - drugs <- drugs[susceptible_to_treatment_index] susceptible_to_treatment <- bitset_at(seek_treatment, susceptible_to_treatment_index) + drugs <- drugs[susceptible_to_treatment_index] n_early_treatment_failure <- n_treat - susceptible_to_treatment$size() renderer$render('n_early_treatment_failure', n_early_treatment_failure, timestep) - } else { susceptible_to_treatment <- seek_treatment n_early_treatment_failure <- n_treat - susceptible_to_treatment$size() diff --git a/R/model.R b/R/model.R index c610f80c..e0c93cb1 100644 --- a/R/model.R +++ b/R/model.R @@ -74,10 +74,9 @@ #' susceptible #' * net_usage: the number people protected by a bed net #' * mosquito_deaths: number of adult female mosquitoes who die this timestep -#' * n_clin_infected: number of new clinically infected individuals in 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 newly successfully treated individuals in this timestep +#' * n_treat_success: number of successfully treated individuals in this timestep #' #' @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/parameters.R b/R/parameters.R index 619b307c..84afda7e 100644 --- a/R/parameters.R +++ b/R/parameters.R @@ -207,6 +207,7 @@ #' * late_clinical_failure_prob - vector of probabilities of late clinical failure for a given drug; default = NULL #' * late_parasitological_failure_prob - vector of probabilities of late parasitological failure for a given drug; default = NULL #' * reinfection_during_prophylaxis - 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 diff --git a/man/get_antimalarial_resistance_parameters.Rd b/man/get_antimalarial_resistance_parameters.Rd new file mode 100644 index 00000000..34c0a9c6 --- /dev/null +++ b/man/get_antimalarial_resistance_parameters.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/antimalarial_resistance.R +\name{get_antimalarial_resistance_parameters} +\alias{get_antimalarial_resistance_parameters} +\title{Retrieve resistance parameters} +\usage{ +get_antimalarial_resistance_parameters(parameters, drugs, timestep) +} +\arguments{ +\item{parameters}{the model parameters} + +\item{timestep}{the current time step} + +\item{drug}{vector of integers representing the drugs administered to each individual receiving treatment} +} +\description{ +Retrieve the resistance parameters associated with the drug each individual receiving clinical +treatment has been administered in the current time step. +} diff --git a/man/get_parameters.Rd b/man/get_parameters.Rd index d3ebfe76..1343492a 100644 --- a/man/get_parameters.Rd +++ b/man/get_parameters.Rd @@ -228,6 +228,7 @@ please set antimalarial resistance parameters with the convenience functions in \item late_clinical_failure_prob - vector of probabilities of late clinical failure for a given drug; default = NULL \item late_parasitological_failure_prob - vector of probabilities of late parasitological failure for a given drug; default = NULL \item reinfection_during_prophylaxis - 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 548fbd7e..07871b13 100644 --- a/man/run_simulation.Rd +++ b/man/run_simulation.Rd @@ -90,9 +90,8 @@ 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_clin_infected: number of new clinically infected individuals in 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 newly successfully treated individuals in this timestep +\item n_treat_success: number of successfully treated individuals in this timestep } } diff --git a/man/set_antimalarial_resistance.Rd b/man/set_antimalarial_resistance.Rd index 30b31433..82c65405 100644 --- a/man/set_antimalarial_resistance.Rd +++ b/man/set_antimalarial_resistance.Rd @@ -14,7 +14,8 @@ set_antimalarial_resistance( early_treatment_failure_prob, late_clinical_failure_prob, late_parasitological_prob, - reinfection_prob + reinfection_prob, + slow_parasite_clearance_time ) } \arguments{ @@ -37,6 +38,8 @@ set_antimalarial_resistance( \item{late_parasitological_prob}{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{slow_parasite_clearance_time}{single value representing the mean time individual's experiencing slow parasite clearance reside in the treated state} } \description{ Parameterise antimalarial resistance diff --git a/vignettes/Antimalarial_Resistance.Rmd b/vignettes/Antimalarial_Resistance.Rmd index 5730ae99..eaeb7e89 100644 --- a/vignettes/Antimalarial_Resistance.Rmd +++ b/vignettes/Antimalarial_Resistance.Rmd @@ -34,7 +34,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. +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. ## 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. @@ -84,7 +84,8 @@ simparams_resistance <- set_antimalarial_resistance(parameters = simparams_clin_ 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)) + reinfection_prob = rep(0, 2), + slow_parasite_clearance_time = 10) ``` @@ -204,7 +205,8 @@ simparams <- set_antimalarial_resistance(parameters = simparams, 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))) + reinfection_prob = rep(0, length(resistance_update_timesteps)), + slow_parasite_clearance_time = 10) # Calibrate the parameters to an initial EIR: simparams <- set_equilibrium(parameters = simparams, init_EIR = initial_eir)