Skip to content

Commit

Permalink
Merge pull request #273 from InstituteforDiseaseModeling/charles/issu…
Browse files Browse the repository at this point in the history
…e119

Rate limits caps for fertility, mortality, and prevalence rates
  • Loading branch information
MeWu-IDM authored Jun 16, 2023
2 parents 1b2e88c + e108dfe commit 29525a2
Show file tree
Hide file tree
Showing 22 changed files with 1,446 additions and 211 deletions.
Binary file modified config/model_inputs_demo.xlsx
Binary file not shown.
7 changes: 5 additions & 2 deletions pacehrh/R/pace_exp_control.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' pacehrh::InitializeScenarios()
#' pacehrh::InitializeStochasticParameters()
#' pacehrh::InitializeSeasonality()
#' pacehrh::InitializeCadreRoles()
#'
#' scenario <- "ScenarioName"
#'
Expand Down Expand Up @@ -148,7 +149,9 @@ SaveBaseSettings <- function(scenarioName = ""){
expression(BVE$stochasticParams),
expression(BVE$initialPopulation),
expression(BVE$populationLabels),
expression(BVE$populationRangesTable)
expression(BVE$populationRangesTable),
expression(BVE$cadreRoles)
# expression(BVE$changeRateLimits)
)

checks <- sapply(varsToCheck, function(v){
Expand Down Expand Up @@ -302,7 +305,7 @@ ConfigureExperimentValues <- function(){
#' \dontrun{
#' initPop <- pacehrh:::loadInitialPopulation(sheetName = "Flat_Population")
#' pcr <- pacehrh:::loadPopulationChangeRates(sheetName = "Flat_Rates")
#' pars <- pacehrh:::loadStochasticParameters(sheetName = "Flat_StochasticParms")
#' pars <- pacehrh:::loadStochasticParameters(stochasticParametersSheetName = "Flat_StochasticParms")
#' years <- 2020:2040
#' pcr <- pacehrh:::addRatesMatricesToPopulationChangeRates(pcr, years, NULL)
#' }
Expand Down
2 changes: 1 addition & 1 deletion pacehrh/R/pace_initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PaceInitialize <- function(globalConfigFile = NULL,
if (is.null(stochasticParametersSheet)) {
InitializeStochasticParameters()
} else {
InitializeStochasticParameters(sheetName = stochasticParametersSheet)
InitializeStochasticParameters(stochasticParametersSheetName = stochasticParametersSheet)
}

# Local seasonality curves sheet and seasonality offsets sheets
Expand Down
1 change: 1 addition & 0 deletions pacehrh/R/pace_package_env.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ NULL
.defaultStochasticParametersSheet <- "StochasticParameters"
.defaultCadreRolesSheet <- "CadreRoles"
.defaultTaskCadresSheet <- "TaskCadres"
.defaultChangeRateLimitsSheet <- "ChangeRateLimits"

# GLOBAL VARIABLES

Expand Down
103 changes: 55 additions & 48 deletions pacehrh/R/pace_prevalence_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

#' Generate A Matrix Of Stochastically Varied Prevalence/Incidence Rates
#'
#' @param debugEnv Caller-create environment into which the
#' `generatePrevalenceRatesMatrix()` call can insert some debugging
#' information to return to the caller. Intended for unit testing.
#'
#' @return Rates matrix
#'
#' @noRd
generatePrevalenceRatesMatrix <- function(){
generatePrevalenceRatesMatrix <- function(debugEnv = NULL){
if (!GPE$stochasticity){
return(.generateNonStochPrm())
return(.generateNonStochPrm(debugEnv))
}

# Gather stuff we're going to need
Expand All @@ -33,24 +37,23 @@ generatePrevalenceRatesMatrix <- function(){
# Mask for tasks affected by prevalence stochasticity
sMask <- tasks$applyStochasticity

# Determine which rows refer to Malaria/HIV/TB
mhivtbMask <-
(
tasks$ServiceCat == "Malaria" |
tasks$ServiceCat == "Tuberculosis" | tasks$ServiceCat == "HIV"
)

# Determine which rows refer to childhood diseases
childDisMask <- (tasks$RelevantPop == "1-4")

# Grab the initial prevalence/incidence values, and apply a stochastic tweak
p = pars[pars$Value == "Incidence rates", ]$p

initRates <- rep(1.0, nRows)

initRates[sMask] <-
.varyInitialIncidenceRates(tasks[sMask,]$StartingRateInPop, p)

# Calculate min/max constraints on rates
limits <- .getRatesLimits("Incidence")
rateLimits <- .getMinMaxRates(initRates, limits)
maskedRateLimits <- lapply(rateLimits, function(x){x[sMask]})

# Write some information back to the user-provided debugging environment
if (!is.null(debugEnv)) {
assign("rateLimits", rateLimits, envir = debugEnv)
}

# Initialize a rates matrix
m <-
matrix(
Expand All @@ -66,17 +69,7 @@ generatePrevalenceRatesMatrix <- function(){
q = pars[pars$Value == "Annual delta incidence rates", ]$q
lims = p * q * c(-1, 1)

# Grab the annual prevalence deltas
deltaRatios <- tasks$AnnualDeltaRatio

# Set annual prevalence deltas to unity if the scenario requires it
if (BVE$scenario$o_MHIVTB_decr == FALSE){
deltaRatios[mhivtbMask] <- 1
}

if (BVE$scenario$o_ChildDis_decr == FALSE){
deltaRatios[childDisMask] <- 1
}
deltaRatios <- .computeAdjustedDeltaRatios(tasks)

for (j in 2:nCols){
e <- truncnorm::rtruncnorm(
Expand All @@ -87,13 +80,38 @@ generatePrevalenceRatesMatrix <- function(){
b = lims[2]
)
deltas <- deltaRatios[sMask] * (1 + e)
m[sMask, j] <- m[sMask, j-1] * deltas
m[sMask, j] <- .applyRateLimits(m[sMask, j-1] * deltas, maskedRateLimits)
}

return(m)
}

.generateNonStochPrm <- function(){
.computeAdjustedDeltaRatios <- function(tasks) {
deltaRatios <- tasks$AnnualDeltaRatio

# Set annual prevalence deltas to unity if the scenario requires it
if (BVE$scenario$o_MHIVTB_decr == FALSE){
# Determine which rows refer to Malaria/HIV/TB
mhivtbMask <-
(
tasks$ServiceCat == "Malaria" |
tasks$ServiceCat == "Tuberculosis" | tasks$ServiceCat == "HIV"
)

deltaRatios[mhivtbMask] <- 1
}

if (BVE$scenario$o_ChildDis_decr == FALSE){
# Determine which rows refer to childhood diseases
childDisMask <- (tasks$RelevantPop == "1-4")

deltaRatios[childDisMask] <- 1
}

return(deltaRatios)
}

.generateNonStochPrm <- function(debugEnv = NULL){
years <- BVE$years

tasks <-
Expand All @@ -112,19 +130,18 @@ generatePrevalenceRatesMatrix <- function(){
# Mask for tasks affected by prevalence stochasticity
sMask <- tasks$applyStochasticity

# Determine which rows refer to Malaria/HIV/TB
mhivtbMask <-
(
tasks$ServiceCat == "Malaria" |
tasks$ServiceCat == "Tuberculosis" | tasks$ServiceCat == "HIV"
)

# Determine which rows refer to childhood diseases
childDisMask <- (tasks$RelevantPop == "1-4")

initRates <- rep(1.0, nRows)
initRates[sMask] <- tasks[sMask,]$StartingRateInPop

# Calculate min/max constraints on rates
limits <- .getRatesLimits("Incidence")
rateLimits <- .getMinMaxRates(initRates, limits)
maskedRateLimits <- lapply(rateLimits, function(x){x[sMask]})

if (!is.null(debugEnv)) {
assign("rateLimits", rateLimits, envir = debugEnv)
}

# Initialize a rates matrix
m <-
matrix(
Expand All @@ -135,22 +152,12 @@ generatePrevalenceRatesMatrix <- function(){
)
m[, 1] <- initRates

# Grab the annual prevalence deltas
deltaRatios <- tasks$AnnualDeltaRatio

# Set annual prevalence deltas to unity if the scenario requires it
if (BVE$scenario$o_MHIVTB_decr == FALSE){
deltaRatios[mhivtbMask] <- 1
}

if (BVE$scenario$o_ChildDis_decr == FALSE){
deltaRatios[childDisMask] <- 1
}
deltaRatios <- .computeAdjustedDeltaRatios(tasks)

# Derive the rest of the matrix
deltas <- deltaRatios[sMask]
for (j in 2:nCols){
m[sMask, j] <- m[sMask, j-1] * deltas
m[sMask, j] <- .applyRateLimits(m[sMask, j-1] * deltas, maskedRateLimits)
}

return(m)
Expand Down
Loading

0 comments on commit 29525a2

Please sign in to comment.