-
Notifications
You must be signed in to change notification settings - Fork 18
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
Function for setting age-structured outputs. #297
Changes from 1 commit
1ba67c0
08b1f62
c5a7bec
4e14e50
4b54e80
4a58966
3236f76
8e759c1
247432c
f3669f5
1d4f4a2
e568127
0057fda
a591eb0
ba4bedc
462b0fe
610c5cb
55d83ea
abdcef0
8786b81
a542d10
482fc83
c9c90f5
8f11a06
09a8c3e
5746a6b
b852847
24a0253
3989fdc
7bd8c0d
6d34636
06c1e8d
91f8270
d05b335
a4f66e2
e51c6bc
b4825c7
4701748
e004d5b
65e5899
0ff9f95
1886755
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#' @title Parameterise age grouped output rendering | ||
#' | ||
#' @details this function produces discrete and contiguous age groups, inclusive of the lower | ||
#' age limit and exclusive of the upper age limit: e.g., list(c(0, 10, 100), c(200, 250) will produce | ||
#' three age groups: 0-9, 10-99 and 200-249 in days. | ||
#' @param parameters the model parameters | ||
#' @param age_group age breaks for population size outputs; default = NULL | ||
#' @param incidence age breaks for incidence outputs (D+Tr+A); default = NULL | ||
#' @param clinical_incidence age breaks for clinical incidence outputs (symptomatic); default = c(0, 1825) | ||
#' @param severe_incidence age breaks for severe incidence outputs; default = NULL | ||
#' @param prevalence age breaks for clinical prevalence outputs (pcr and lm detectable infections); default = c(730, 3650) | ||
#' @export | ||
#' | ||
set_epi_outputs <- function(parameters, | ||
age_group = NULL, | ||
incidence = NULL, | ||
clinical_incidence = NULL, | ||
severe_incidence = NULL, | ||
prevalence = NULL, | ||
ica = NULL, | ||
icm = NULL, | ||
iva = NULL, | ||
ivm = NULL, | ||
id = NULL, | ||
ib = NULL | ||
){ | ||
|
||
parent_formals <- names(formals()) | ||
parent_formals <- parent_formals[which(parent_formals != "parameters")] | ||
outputs <- parent_formals[!unlist(lapply(parent_formals, function(x){is.null(get(x))}))] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to validate that output_rendering... is in parameters? I feel like R lets you put any kwarg in without complaining. Something to double check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry - would you mind explaining this a bit more? Are you saying to just check that the parameters appear in the parameter list after this function is used? I have a test that should check for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is obsolete now since we don't use |
||
for (output in outputs) { | ||
if(!is.list(get(output))){stop("Each age input must be a list of vectors")} | ||
parameters[[paste0(output, "_rendering_min_ages")]] <- unlist(lapply(get(output), function(x){x[-length(x)]})) | ||
parameters[[paste0(output, "_rendering_max_ages")]] <- unlist(lapply(get(output), function(x){x[-1]-1})) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with Pete that it's a little terse. You could perhaps make it more readable with names for the functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing - happy to put this into a function for readability. |
||
} | ||
|
||
parameters | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
test_that('Test age parameter function works', { | ||
parameters <- get_parameters() | ||
age_limits <- c(0,1,2,3)*365 | ||
parameters <- set_epi_outputs(parameters, | ||
age_group = list(age_limits), | ||
incidence = list(age_limits+1), | ||
clinical_incidence = list(age_limits+2), | ||
severe_incidence = list(age_limits+3), | ||
prevalence = list(age_limits+4), | ||
ica = list(age_limits+5), | ||
icm = list(age_limits+6), | ||
id = list(age_limits+7), | ||
ib = list(age_limits+8), | ||
iva = list(age_limits+9), | ||
ivm = list(age_limits+10) | ||
) | ||
|
||
sim <- run_simulation(timesteps = 1, parameters) | ||
|
||
expect_true( | ||
all( | ||
paste0(rep(c("n_age", | ||
"n", "n_inc", "p_inc", | ||
"n","n_inc_clinical","p_inc_clinical", | ||
"n","n_inc_severe","p_inc_severe", | ||
"n","n_detect","p_detect", | ||
"ica_mean", "icm_mean","id_mean","ib_mean","iva_mean","ivm_mean"), each = 3),"_", | ||
age_limits[-4]+rep(c(0,rep(c(1:4), each = 3),5:10), each = 3),"_",age_limits[-1]-1+rep(c(0,rep(c(1:4), each = 3),5:10), each = 3) | ||
) %in% | ||
names(sim))) | ||
|
||
}) | ||
|
||
test_that('Test age parameter multiple sequences function works', { | ||
parameters <- get_parameters() | ||
age_limits <- c(0,1,2,3)*365 | ||
parameters <- set_epi_outputs(parameters, | ||
age_group = list(age_limits, age_limits+100)) | ||
|
||
sim <- run_simulation(timesteps = 1, parameters) | ||
|
||
expect_true( | ||
all( | ||
paste0(rep(c("n_age"), each = 3),"_", | ||
c(age_limits[-4], age_limits[-4]+100), "_", c(age_limits[-1], age_limits[-1]+100)-1 | ||
) %in% | ||
names(sim))) | ||
|
||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this a little tricky to parse (although I think it all works!).
I was wondering if we can avoid having to input lists, and (maybe) make the process inthe function a bit simpler. Does the following work in the correct way? I haven't tested it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to capture discrete age blocks with the lists, e.g., if you want to output 1-100 and 200-300, but not 101-199. I don't think this code would do that, but maybe that's not so important for it to do.