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

Function for setting age-structured outputs. #297

Merged
merged 42 commits into from
Jul 16, 2024
Merged

Conversation

RJSheppard
Copy link
Member

Sets parameters that output age-structured incidence, prevalence and immunity levels. Discrete age groups are set using a list, where vectors within the list result in contiguous age group rendering.

(I also corrected sp. of prevelance -> prevalence :) )

giovannic and others added 16 commits July 18, 2023 12:11
 * Add rdt testing parameters
 * Update biting_process to consider rdt testing for mixing
- updating metapop vignette
- clarifying row & col meanings
 * Add rdt testing parameters
 * Update biting_process to consider rdt testing for mixing
 * refactor transmission mixing code
 * fix regressions
 * add time caching
 * Added export_mixing and import_mixing arguments to run_metapop_...
 * Update create_transmission_mixer to mix eir on import and foim on
   export
 * Fix regression tests
Add asymmetric transmission in mixing:
@RJSheppard RJSheppard force-pushed the new_epi_outputs_function branch 2 times, most recently from 80eb376 to dc12862 Compare May 8, 2024 13:24
… and immunity. Discrete age groups are set using a list, where vectors within the list result in contiguous age group rendering.

Also correcting spelling of prevalence in create_prevalence_renderer function.

This is a correction attempting to remove issues around line endings.
@RJSheppard RJSheppard force-pushed the new_epi_outputs_function branch from dc12862 to 610c5cb Compare May 8, 2024 13:54
@@ -0,0 +1,45 @@
#' @title Parameterise age grouped output rendering
Copy link
Member

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!

#' @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., c(0, 10, 100, 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)
#' @param ica age breaks for average acquired clinical immunity; default = NULL
#' @param icm age breaks for average maternal clinical immunity; default = NULL
#' @param iva age breaks for average acquired severe immunity; default = NULL
#' @param ivm age breaks for average maternal severe immunity; default = NULL
#' @param id age breaks for average immunity to detectability; default = NULL
#' @param ib age breaks for average blood immunity; default = NULL
#' @export
#'
set_epi_outputs <- function(parameters,
                            age_group = c(0, 10, 100, 200, 250),
                            incidence = NULL,
                            clinical_incidence = c(0, 1825),
                            severe_incidence = NULL,
                            prevalence = c(730, 3650),
                            ica = NULL,
                            icm = NULL,
                            iva = NULL,
                            ivm = NULL,
                            id = NULL,
                            ib = NULL
){
  
  input <- list(
    age_group = age_group,
    incidence = incidence,
    clinical_incidence = clinical_incidence,
    severe_incidence = severe_incidence,
    prevalence = prevalence,
    ica = ica,
    icm = icm,
    iva = iva,
    ivm = ivm,
    id = id,
    ib = ib
  )
  input <- input[!sapply(input, is.null)]
  
  for(i in seq_along(input)){
    name <- names(input)[i]
    ages <- input[[i]]
    min_ages <- ages[-length(ages)]
    max_ages <- ages[-1] - 1
    parameters[[paste0(name, "_rendering_min_ages")]] <- min_ages
    parameters[[paste0(name, "_rendering_max_ages")]] <- max_ages
  }
  
  return(parameters)
}

Copy link
Member Author

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.

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))}))]

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is obsolete now since we don't use formals!

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}))
Copy link
Member

Choose a reason for hiding this comment

The 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 function(x){x[-length(x)]} and function(x){x[-1]-1}, and/or unwrapping an unlist/lapply into a for loop.

Copy link
Member Author

@RJSheppard RJSheppard May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing - happy to put this into a function for readability.

… and immunity. Discrete age groups are set using a list, where vectors within the list result in contiguous age group rendering.

Also correcting spelling of prevalence in create_prevalence_renderer function.

This is a correction attempting to remove issues around line endings.
…will be rendered now). Made the code more easier to read.
Merge branch 'new_epi_outputs_function' of https://github.com/mrc-ide/malariasimulation into new_epi_outputs_function

# Conflicts:
#	R/output_parameters.R
#	man/set_epi_outputs.Rd
#	tests/testthat/test-output.R
@RJSheppard
Copy link
Member Author

Was there anything extra on this branch that either of you wanted changing (@pwinskill @giovannic)?

@RJSheppard RJSheppard requested review from pwinskill and giovannic May 24, 2024 08:49
Copy link
Member

@giovannic giovannic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does reduce the functionality as you said, it doesn't support modelling discontiguous age ranges. But it is more readable. Approving since it is likely helpful for some users regardless

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))}))]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is obsolete now since we don't use formals!

Copy link
Member

@pwinskill pwinskill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

… and immunity. Discrete age groups are set using a list, where vectors within the list result in contiguous age group rendering.

Also correcting spelling of prevalence in create_prevalence_renderer function.

This is a correction attempting to remove issues around line endings.
…will be rendered now). Made the code more easier to read.
… and immunity. Discrete age groups are set using a list, where vectors within the list result in contiguous age group rendering.

Also correcting spelling of prevalence in create_prevalence_renderer function.
…simulation into new_epi_outputs_function

# Conflicts:
#	tests/testthat/test-output.R
@RJSheppard RJSheppard merged commit 36c4301 into dev Jul 16, 2024
4 checks passed
@RJSheppard RJSheppard deleted the new_epi_outputs_function branch July 16, 2024 14:31
@giovannic giovannic mentioned this pull request Sep 11, 2024
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants