Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example correlation diagnostics: #302

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions R/correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ CorrelationParameters <- R6::R6Class(
#' @param parameters model parameters
#' @export
#' @examples
#'
#'
#' # get the default model parameters
#' parameters <- get_parameters()
#'
#'
#' # Set some vaccination strategy
#' parameters <- set_mass_pev(
#' parameters,
Expand All @@ -208,7 +208,7 @@ CorrelationParameters <- R6::R6Class(
#' booster_coverage = numeric(0),
#' booster_profile = NULL
#' )
#'
#'
#' # Set some smc strategy
#' parameters <- set_drugs(parameters, list(SP_AQ_params))
#' parameters <- set_smc(
Expand All @@ -219,14 +219,14 @@ CorrelationParameters <- R6::R6Class(
#' min_age = 100,
#' max_age = 1000
#' )
#'
#'
#' # Correlate the vaccination and smc targets
#' correlations <- get_correlation_parameters(parameters)
#' correlations$inter_intervention_rho('pev', 'smc', 1)
#'
#'
#' # Correlate the rounds of smc
#' correlations$inter_round_rho('smc', 1)
#'
#'
#' # You can now pass the correlation parameters to the run_simulation function
get_correlation_parameters <- function(parameters) {
# Find a list of enabled interventions
Expand Down Expand Up @@ -331,3 +331,31 @@ rcondmvnorm <- function(n, mean, sigma, given, dependent.ind, given.ind) {
samples + cond_mu
}
}

used_intervention <- function(variable, timestep, window) {
variable$get_index_of(set=-1)$not()$and(
variable$get_index_of(a=timestep - window, b=timestep)
)
}

create_combined_intervention_rendering_process <- function(
int_1,
variable_1,
int_2,
variable_2,
window,
renderer
) {
name <- paste0('n_combined_', int_1, '_', int_2)
renderer$set_default(name, 0)
function (timestep) {
n <- used_intervention(variable_1, timestep, window)$and(
used_intervention(variable_2, timestep, window)
)$size()
renderer$render(
name,
n,
timestep
)
}
}
3 changes: 2 additions & 1 deletion R/events.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ attach_event_listeners <- function(
variables,
events,
parameters,
correlations
correlations,
renderer
)
)
attach_pev_dose_listeners(
Expand Down
3 changes: 2 additions & 1 deletion R/mda_processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ create_mda_listeners <- function(
target <- in_age[sample_intervention(in_age, int_name, coverage, correlations)]

renderer$render(paste0('n_', int_name, '_treated'), length(target), timestep)

successful_treatments <- bernoulli(
length(target),
parameters$drug_efficacy[[drug]]
Expand Down Expand Up @@ -75,6 +75,7 @@ create_mda_listeners <- function(
# Update drug
variables$drug$queue_update(drug, to_move)
variables$drug_time$queue_update(timestep, to_move)
variables[[paste0(int_name, '_time')]]$queue_update(timestep, to_move)
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion R/mortality_processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,21 @@ reset_target <- function(variables, events, target, state, parameters, timestep)
# treatment
variables$drug$queue_update(0, target)
variables$drug_time$queue_update(-1, target)
variables$smc_time$queue_update(-1, target)
variables$mda_time$queue_update(-1, target)

# vaccination
variables$last_pev_timestep$queue_update(-1, target)
variables$last_eff_pev_timestep$queue_update(-1, target)
variables$pev_profile$queue_update(-1, target)
variables$tbv_vaccinated$queue_update(-1, target)

# spraying
variables$spray_time$queue_update(-1, target)

# onwards infectiousness
variables$infectivity$queue_update(0, target)

# treated compartment residence time:
if(!is.null(variables$dt)) {
variables$dt$queue_update(parameters$dt, target)
Expand Down
19 changes: 16 additions & 3 deletions R/pev.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ create_epi_pev_process <- function(
if(coverage == 0){
return()
}

to_vaccinate <- variables$birth$get_index_of(
set = timestep - parameters$pev_epi_age
)
Expand Down Expand Up @@ -77,13 +77,17 @@ create_epi_pev_process <- function(
#' @param events a list of events in the model
#' @param parameters the model parameters
#' @param correlations correlation parameters
#' @param renderer the model renderer object
#' @noRd
create_mass_pev_listener <- function(
variables,
events,
parameters,
correlations
correlations,
renderer
) {
renderer$set_default('n_new_vaccinations', 0)
renderer$set_default('n_pev_last_year', 0)
function(timestep) {
in_age_group <- individual::Bitset$new(parameters$human_population)
for (i in seq_along(parameters$mass_pev_min_ages)) {
Expand All @@ -109,7 +113,7 @@ create_mass_pev_listener <- function(
} else {
target <- target$to_vector()
}

time_index = which(parameters$mass_pev_timesteps == timestep)
target <- target[
sample_intervention(
Expand All @@ -120,6 +124,15 @@ create_mass_pev_listener <- function(
)
]

n_new_vaccinations <- variables$last_pev_timestep$get_index_of(
set=-1
)$not()$and(
individual::Bitset$new(parameters$human_population)$insert(target)
)$size()

renderer$render('n_new_vaccinations', n_new_vaccinations, timestep)
renderer$render('n_pev_last_year', used_intervention(variables$last_pev_timestep, timestep, 365)$size(), timestep)

# Update the latest vaccination time
variables$last_pev_timestep$queue_update(timestep, target)

Expand Down
119 changes: 110 additions & 9 deletions R/processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ create_processes <- function(
correlations,
lagged_eir,
lagged_infectivity,
timesteps,
timesteps,
mixing = 1,
mixing_index = 1
) {
Expand Down Expand Up @@ -105,22 +105,22 @@ create_processes <- function(
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 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,
Expand All @@ -143,7 +143,7 @@ create_processes <- function(
)

# =========
# RTS,S EPI
# PEV EPI
# =========
if (!is.null(parameters$pev_epi_coverage)) {
processes <- c(
Expand Down Expand Up @@ -239,6 +239,7 @@ create_processes <- function(
processes,
distribute_nets(
variables,
renderer,
events$throw_away_net,
parameters,
correlations
Expand All @@ -250,7 +251,12 @@ create_processes <- function(
if (parameters$spraying) {
processes <- c(
processes,
indoor_spraying(variables$spray_time, parameters, correlations)
indoor_spraying(
variables$spray_time,
renderer,
parameters,
correlations
)
)
}

Expand All @@ -267,7 +273,102 @@ create_processes <- function(
# Mortality step
processes <- c(
processes,
create_mortality_process(variables, events, renderer, parameters))
create_mortality_process(variables, events, renderer, parameters)
)

# ======================
# Combined interventions
# ======================
# PEV & bednets
if (parameters$pev & parameters$bednets) {
processes <- c(
processes,
create_combined_intervention_rendering_process(
'pev',
variables$last_eff_pev_timestep,
'bednets',
variables$net_time,
365,
renderer
)
)
}

# SMC & bednets
if (parameters$smc & parameters$bednets) {
processes <- c(
processes,
create_combined_intervention_rendering_process(
'smc',
variables$smc_time,
'bednets',
variables$net_time,
365,
renderer
)
)
}

# PEV & SMC
if (parameters$pev & parameters$smc) {
processes <- c(
processes,
create_combined_intervention_rendering_process(
'pev',
variables$last_eff_pev_timestep,
'smc',
variables$smc_time,
365,
renderer
)
)
}

# Spraying & bednets
if (parameters$spraying & parameters$bednets) {
processes <- c(
processes,
create_combined_intervention_rendering_process(
'spraying',
variables$spray_time,
'bednets',
variables$net_time,
365,
renderer
)
)
}

# Spraying & SMC
if (parameters$spraying & parameters$smc) {
processes <- c(
processes,
create_combined_intervention_rendering_process(
'spraying',
variables$spray_time,
'smc',
variables$smc_time,
365,
renderer
)
)
}

# Spraying & PEV
if (parameters$spraying & parameters$pev) {
processes <- c(
processes,
create_combined_intervention_rendering_process(
'spraying',
variables$spray_time,
'pev',
variables$last_eff_pev_timestep,
365,
renderer
)
)
}


processes
}
Expand Down
12 changes: 9 additions & 3 deletions R/variables.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @title Define model variables
#' @title Define model variables
#' @description
#' create_variables creates the human and mosquito variables for
#' the model. Variables are used to track real data for each individual over
Expand Down Expand Up @@ -32,11 +32,13 @@
#' * infectivity - The onward infectiousness to mosquitos
#' * drug - The last prescribed drug
#' * drug_time - The timestep of the last drug
#' * smc_time - The timestep of the last smc drug
#' * mda_time - The timestep of the last mda drug
#'
#' Antimalarial resistance variables are:
#' * dt - the delay for humans to move from state Tr to state S
#'
#' Mosquito variables are:
#' 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
#'
Expand Down Expand Up @@ -193,6 +195,8 @@ create_variables <- function(parameters) {

drug <- individual::IntegerVariable$new(rep(0, size))
drug_time <- individual::IntegerVariable$new(rep(-1, size))
smc_time <- individual::IntegerVariable$new(rep(-1, size))
mda_time <- individual::IntegerVariable$new(rep(-1, size))

last_pev_timestep <- individual::IntegerVariable$new(rep(-1, size))
last_eff_pev_timestep <- individual::IntegerVariable$new(rep(-1, size))
Expand Down Expand Up @@ -222,14 +226,16 @@ create_variables <- function(parameters) {
infectivity = infectivity,
drug = drug,
drug_time = drug_time,
smc_time = smc_time,
mda_time = mda_time,
last_pev_timestep = last_pev_timestep,
last_eff_pev_timestep = last_eff_pev_timestep,
pev_profile = pev_profile,
tbv_vaccinated = tbv_vaccinated,
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))
Expand Down
Loading
Loading