Skip to content

Commit

Permalink
Merge pull request #129 from mrc-ide/issue-91
Browse files Browse the repository at this point in the history
Add separate model variant for demographic projection
  • Loading branch information
r-ash authored Feb 5, 2025
2 parents 18fd798 + 3571cb7 commit b4e9cd9
Show file tree
Hide file tree
Showing 35 changed files with 1,117 additions and 898 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
4 changes: 3 additions & 1 deletion R/generate_cpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,9 @@ generate_state_saver_func_body <- function(output) {

model_variant_to_name <- function(model_variant) {
if (model_variant == "ModelVariant") {
"base"
"demographic"
} else if (model_variant == "HivSimulation") {
"hiv"
} else if (model_variant == "ChildModel") {
"children"
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/generate_cpp_validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ validate_and_parse_output <- function(output, filename, row_num) {
validate_output_dims(output)
if (output$model_variant == "ModelVariant") {
## If it is included for all model variants then data is stored on
## structs called "BaseModel"
output$struct <- "BaseModel"
## structs called "DemographicProjection"
output$struct <- "DemographicProjection"
} else {
output$struct <- output$model_variant
}
Expand Down
16 changes: 11 additions & 5 deletions R/run_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param sim_years Simulation years to run model for default 1970:2030
#' @param hts_per_year Number of HIV time steps per year, default 10
#' @param output_steps Which sim years to output for default same as sim_years
#' @param run_hiv_simulation If TRUE then runs HIV simulation
#' @param hiv_age_stratification The age stratification for HIV population,
#' "coarse" or "full"
#' @param run_child_model If TRUE then run the child model
Expand All @@ -14,6 +15,7 @@
run_model <- function(data, parameters, sim_years,
hts_per_year,
output_steps = NULL,
run_hiv_simulation = TRUE,
hiv_age_stratification = "full",
run_child_model = TRUE) {
if (is.null(sim_years)) {
Expand All @@ -36,23 +38,27 @@ run_model <- function(data, parameters, sim_years,
output_steps <- which(sim_years %in% output_steps)
}

assert_enum(hiv_age_stratification, c("full", "coarse"))
if (hiv_age_stratification == "full" && !run_child_model) {
model_variant <- "BaseModelFullAgeStratification"
assert_enum(hiv_age_stratification, c("full", "coarse", "none"))
if (!run_hiv_simulation) {
model_variant <- "DemographicProjection"
} else if (hiv_age_stratification == "full" && !run_child_model) {
model_variant <- "HivFullAgeStratification"
} else if (hiv_age_stratification == "coarse" && !run_child_model) {
model_variant <- "BaseModelCoarseAgeStratification"
model_variant <- "HivCoarseAgeStratification"
} else if (hiv_age_stratification == "full" && run_child_model) {
model_variant <- "ChildModel"
} else if (hiv_age_stratification == "coarse" && run_child_model) {
stop("Cannot run child model with coarse age stratification")
} else if (hiv_age_stratification == "none" && run_hiv_simulation) {
stop("Cannot run HIV simulation with unspecified age stratification")
}
if (hiv_age_stratification == "full") {
parameters$hAG_SPAN <- parameters[["hAG_SPAN_full"]]
parameters$cd4_initdist <- parameters[["cd4_initdist_full"]]
parameters$cd4_prog <- parameters[["cd4_prog_full"]]
parameters$cd4_mort <- parameters[["cd4_mort_full"]]
parameters$art_mort <- parameters[["art_mort_full"]]
} else {
} else if (hiv_age_stratification == "coarse") {
parameters$hAG_SPAN <- parameters[["hAG_SPAN_coarse"]]
parameters$cd4_initdist <- parameters[["cd4_initdist_coarse"]]
parameters$cd4_prog <- parameters[["cd4_prog_coarse"]]
Expand Down
20 changes: 11 additions & 9 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.cap = "",
out.width = "100%"
)
```
Expand Down Expand Up @@ -42,8 +43,9 @@ The simulation model is callable in R via a wrapper function `run_model()` creat

You can control how the simulation model is run with the following arguments:

* `run_hiv_simulation` which is `TRUE` by default. Set to `FALSE` to turn off the HIV simulation and run only the demographic projection.
* `hiv_age_stratification` which must be "coarse" or "full". Coarse is run with 5-year age groups and full with single year ages.
* `run_child_model` which is `FALSE` by default. Set to `TRUE` to run the paediatric portion of the model.
* `run_child_model` which is `FALSE` by default. Set to `TRUE` to run the child portion of the model.

## Example

Expand All @@ -55,7 +57,7 @@ Prepare model inputs.
```{r example}
library(frogger)
pjnz <- system.file("pjnz/bwa_aim-adult-art-no-special-elig_v6.13_2022-04-18.PJNZ",
pjnz <- system.file("pjnz/bwa_aim-adult-art-no-special-elig_v6.13_2022-04-18.PJNZ",
package = "frogger", mustWork = TRUE)
demp <- prepare_leapfrog_demp(pjnz)
Expand All @@ -66,14 +68,14 @@ Simulate adult 'full' age group (single-year age) and 'coarse' age group (collap
age groups) models from 1970 to 2030 with 10 HIV time steps per year.

```{r simulate_leapfrog}
lsimF <- run_model(demp, hivp, 1970:2030, 10L,
lsimF <- run_model(demp, hivp, 1970:2030, 10L,
hiv_age_stratification = "full", run_child_model = FALSE)
lsimC <- run_model(demp, hivp, 1970:2030, 10L,
lsimC <- run_model(demp, hivp, 1970:2030, 10L,
hiv_age_stratification = "coarse", run_child_model = FALSE)
```

Compare the HIV prevalence age 15-49 years and AIDS deaths 50+ years. Deaths 50+
years are to show some noticeable divergence between the `"full"` and `"coarse"`
Compare the HIV prevalence age 15-49 years and AIDS deaths 50+ years. Deaths 50+
years are to show some noticeable divergence between the `"full"` and `"coarse"`
age group simulations.

```{r sim_prev}
Expand All @@ -88,7 +90,7 @@ lines(1970:2030, prevC, col = 2)
plot(1970:2030, deathsF, type = "l", main = "AIDS Deaths 50+ years")
lines(1970:2030, deathsC, col = 2)
```
```

## Benchmarking

Expand Down Expand Up @@ -119,8 +121,8 @@ The code uses header-only open source libraries to maximize portability.

### R functions

The file `src/frogger.cpp` contains R wrapper functions for the model simulation
via [Rcpp](http://dirk.eddelbuettel.com/code/rcpp.html) and
The file `src/frogger.cpp` contains R wrapper functions for the model simulation
via [Rcpp](http://dirk.eddelbuettel.com/code/rcpp.html) and
[RcppEigen](http://dirk.eddelbuettel.com/code/rcpp.eigen.html).


Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ The simulation model is callable in R via a wrapper function
You can control how the simulation model is run with the following
arguments:

- `run_hiv_simulation` which is `TRUE` by default. Set to `FALSE` to
turn off the HIV simulation and run only the demographic projection.
- `hiv_age_stratification` which must be “coarse” or “full”. Coarse is
run with 5-year age groups and full with single year ages.
- `run_child_model` which is `FALSE` by default. Set to `TRUE` to run
the paediatric portion of the model.
the child portion of the model.

## Example

Expand All @@ -60,7 +62,7 @@ Prepare model inputs.
``` r
library(frogger)

pjnz <- system.file("pjnz/bwa_aim-adult-art-no-special-elig_v6.13_2022-04-18.PJNZ",
pjnz <- system.file("pjnz/bwa_aim-adult-art-no-special-elig_v6.13_2022-04-18.PJNZ",
package = "frogger", mustWork = TRUE)

demp <- prepare_leapfrog_demp(pjnz)
Expand All @@ -72,9 +74,9 @@ Simulate adult ‘full’ age group (single-year age) and ‘coarse’ age group
per year.

``` r
lsimF <- run_model(demp, hivp, 1970:2030, 10L,
lsimF <- run_model(demp, hivp, 1970:2030, 10L,
hiv_age_stratification = "full", run_child_model = FALSE)
lsimC <- run_model(demp, hivp, 1970:2030, 10L,
lsimC <- run_model(demp, hivp, 1970:2030, 10L,
hiv_age_stratification = "coarse", run_child_model = FALSE)
```

Expand Down
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

$R_HOME/bin/Rscript inst/cpp_generation/generate.R
##$R_HOME/bin/Rscript inst/cpp_generation/generate.R
1 change: 1 addition & 0 deletions frogger.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: f4ca7c6e-22e0-47c2-85b1-781bf7e5f806

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
32 changes: 16 additions & 16 deletions inst/cpp_generation/model_input.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ Sx,Demography.survival_probability,real_type,,,FALSE,3,base.pAG + 1,base.NS,proj
netmigr_adj,Demography.net_migration,real_type,,,FALSE,3,base.pAG,base.NS,proj_years,
asfr,Demography.age_specific_fertility_rate,real_type,,,FALSE,2,options.p_fertility_age_groups,proj_years,,
births_sex_prop,Demography.births_sex_prop,real_type,,,FALSE,2,base.NS,proj_years,,
incidinput,Incidence.total_rate,real_type,,,FALSE,1,proj_years,,,
incrr_age,Incidence.relative_risk_age,real_type,,,FALSE,3,base.pAG - options.p_idx_hiv_first_adult,base.NS,proj_years,
incrr_sex,Incidence.relative_risk_sex,real_type,,,FALSE,1,proj_years,,,
cd4_mort,NaturalHistory.cd4_mortality,real_type,,,FALSE,3,base.hDS,base.hAG,base.NS,
cd4_prog,NaturalHistory.cd4_progression,real_type,,,FALSE,3,base.hDS - 1,base.hAG,base.NS,
artcd4elig_idx,Art.idx_hm_elig,int,,,TRUE,1,proj_years,,,
cd4_initdist,NaturalHistory.cd4_initial_distribution,real_type,,,FALSE,3,base.hDS,base.hAG,base.NS,
art_mort,Art.mortality,real_type,,,FALSE,4,base.hTS,base.hDS,base.hAG,base.NS
artmx_timerr,Art.mortaility_time_rate_ratio,real_type,,,FALSE,2,base.hTS,proj_years,,
art_dropout_recover_cd4,Art.dropout_recover_cd4,int,,,FALSE,1,1,,,
art_dropout_rate,Art.dropout_rate,real_type,,,FALSE,1,proj_years,,,
art15plus_num,Art.adults_on_art,real_type,,,FALSE,2,base.NS,proj_years,,
art15plus_isperc,Art.adults_on_art_is_percent,int,,,FALSE,2,base.NS,proj_years,,
,Art.h_art_stage_dur,real_type,,0.5,FALSE,1,base.hTS - 1,,,
scale_cd4_mort,NaturalHistory.scale_cd4_mortality,int,,,FALSE,1,1,,,
art_alloc_mxweight,Art.initiation_mortality_weight,real_type,,,FALSE,1,1,,,
incidinput,Incidence.total_rate,real_type,ModelVariant::run_hiv_simulation,,FALSE,1,proj_years,,,
incrr_age,Incidence.relative_risk_age,real_type,ModelVariant::run_hiv_simulation,,FALSE,3,base.pAG - options.p_idx_hiv_first_adult,base.NS,proj_years,
incrr_sex,Incidence.relative_risk_sex,real_type,ModelVariant::run_hiv_simulation,,FALSE,1,proj_years,,,
cd4_mort,NaturalHistory.cd4_mortality,real_type,ModelVariant::run_hiv_simulation,,FALSE,3,base.hDS,base.hAG,base.NS,
cd4_prog,NaturalHistory.cd4_progression,real_type,ModelVariant::run_hiv_simulation,,FALSE,3,base.hDS - 1,base.hAG,base.NS,
artcd4elig_idx,Art.idx_hm_elig,int,ModelVariant::run_hiv_simulation,,TRUE,1,proj_years,,,
cd4_initdist,NaturalHistory.cd4_initial_distribution,real_type,ModelVariant::run_hiv_simulation,,FALSE,3,base.hDS,base.hAG,base.NS,
art_mort,Art.mortality,real_type,ModelVariant::run_hiv_simulation,,FALSE,4,base.hTS,base.hDS,base.hAG,base.NS
artmx_timerr,Art.mortaility_time_rate_ratio,real_type,ModelVariant::run_hiv_simulation,,FALSE,2,base.hTS,proj_years,,
art_dropout_recover_cd4,Art.dropout_recover_cd4,int,ModelVariant::run_hiv_simulation,,FALSE,1,1,,,
art_dropout_rate,Art.dropout_rate,real_type,ModelVariant::run_hiv_simulation,,FALSE,1,proj_years,,,
art15plus_num,Art.adults_on_art,real_type,ModelVariant::run_hiv_simulation,,FALSE,2,base.NS,proj_years,,
art15plus_isperc,Art.adults_on_art_is_percent,int,ModelVariant::run_hiv_simulation,,FALSE,2,base.NS,proj_years,,
,Art.h_art_stage_dur,real_type,ModelVariant::run_hiv_simulation,0.5,FALSE,1,base.hTS - 1,,,
scale_cd4_mort,NaturalHistory.scale_cd4_mortality,int,ModelVariant::run_hiv_simulation,,FALSE,1,1,,,
art_alloc_mxweight,Art.initiation_mortality_weight,real_type,ModelVariant::run_hiv_simulation,,FALSE,1,1,,,
paed_incid_input,Children.hc_nosocomial,real_type,ModelVariant::run_child_model,,FALSE,1,proj_years,,,
paed_cd4_dist,Children.hc1_cd4_dist,real_type,ModelVariant::run_child_model,,FALSE,1,children.hc2DS,,,
paed_cd4_transition,Children.hc_cd4_transition,real_type,ModelVariant::run_child_model,,FALSE,2,children.hc2DS,children.hc1DS,,
Expand Down
18 changes: 9 additions & 9 deletions inst/cpp_generation/model_output.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ r_name,cpp_name,r_type,model_variant,output_when,dims,dim1,dim2,dim3,dim4,dim5
p_total_pop,p_total_pop,REAL,ModelVariant,,3,base.pAG,base.NS,output_years,,
births,births,REAL,ModelVariant,,1,output_years,,,,
p_total_pop_natural_deaths,p_total_pop_natural_deaths,REAL,ModelVariant,,3,base.pAG,base.NS,output_years,,
p_hiv_pop,p_hiv_pop,REAL,ModelVariant,,3,base.pAG,base.NS,output_years,,
p_hiv_pop_natural_deaths,p_hiv_pop_natural_deaths,REAL,ModelVariant,,3,base.pAG,base.NS,output_years,,
h_hiv_adult,h_hiv_adult,REAL,ModelVariant,,4,base.hDS,base.hAG,base.NS,output_years,
h_art_adult,h_art_adult,REAL,ModelVariant,,5,base.hTS,base.hDS,base.hAG,base.NS,output_years
h_hiv_deaths_no_art,h_hiv_deaths_no_art,REAL,ModelVariant,,4,base.hDS,base.hAG,base.NS,output_years,
p_infections,p_infections,REAL,ModelVariant,,3,base.pAG,base.NS,output_years,,
h_hiv_deaths_art,h_hiv_deaths_art,REAL,ModelVariant,,5,base.hTS,base.hDS,base.hAG,base.NS,output_years
h_art_initiation,h_art_initiation,REAL,ModelVariant,,4,base.hDS,base.hAG,base.NS,output_years,
p_hiv_deaths,p_hiv_deaths,REAL,ModelVariant,,3,base.pAG,base.NS,output_years,,
p_hiv_pop,p_hiv_pop,REAL,HivSimulation,ModelVariant::run_hiv_simulation,3,base.pAG,base.NS,output_years,,
p_hiv_pop_natural_deaths,p_hiv_pop_natural_deaths,REAL,HivSimulation,ModelVariant::run_hiv_simulation,3,base.pAG,base.NS,output_years,,
h_hiv_adult,h_hiv_adult,REAL,HivSimulation,ModelVariant::run_hiv_simulation,4,base.hDS,base.hAG,base.NS,output_years,
h_art_adult,h_art_adult,REAL,HivSimulation,ModelVariant::run_hiv_simulation,5,base.hTS,base.hDS,base.hAG,base.NS,output_years
h_hiv_deaths_no_art,h_hiv_deaths_no_art,REAL,HivSimulation,ModelVariant::run_hiv_simulation,4,base.hDS,base.hAG,base.NS,output_years,
p_infections,p_infections,REAL,HivSimulation,ModelVariant::run_hiv_simulation,3,base.pAG,base.NS,output_years,,
h_hiv_deaths_art,h_hiv_deaths_art,REAL,HivSimulation,ModelVariant::run_hiv_simulation,5,base.hTS,base.hDS,base.hAG,base.NS,output_years
h_art_initiation,h_art_initiation,REAL,HivSimulation,ModelVariant::run_hiv_simulation,4,base.hDS,base.hAG,base.NS,output_years,
p_hiv_deaths,p_hiv_deaths,REAL,HivSimulation,ModelVariant::run_hiv_simulation,3,base.pAG,base.NS,output_years,,
hc1_hiv_pop,hc1_hiv_pop,REAL,ChildModel,ModelVariant::run_child_model,5,children.hc1DS,children.hcTT,children.hc1AG,base.NS,output_years
hc2_hiv_pop,hc2_hiv_pop,REAL,ChildModel,ModelVariant::run_child_model,5,children.hc2DS,children.hcTT,children.hc2AG,base.NS,output_years
hc1_art_pop,hc1_art_pop,REAL,ChildModel,ModelVariant::run_child_model,5,base.hTS,children.hc1DS,children.hc1AG,base.NS,output_years
Expand Down
Loading

0 comments on commit b4e9cd9

Please sign in to comment.