From e833d64706f334c46309283db76789da82394030 Mon Sep 17 00:00:00 2001 From: "Carl A. B. Pearson" Date: Fri, 6 Dec 2024 15:05:56 -0500 Subject: [PATCH] fix issues with examples --- DESCRIPTION | 11 +++++------ R/aggregate.R | 42 +++++++++++++++++++++++++++--------------- man/alembic.Rd | 10 ++++++---- man/blend.Rd | 15 ++++++++++----- man/distill.Rd | 17 +++++++++++------ 5 files changed, 59 insertions(+), 36 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c6fbffe..06c5f5e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,12 +8,11 @@ Authors@R: comment = c(ORCID = "0000-0002-0380-1503")), person("Lucy", "Goodfellow", , "lucy.goodfellow@lshtm.ac.uk", role = c("aut"), comment = c(ORCID = "0009-0004-0434-5863"))) -Description: A convenient framework for aggregating - and disaggregating continuously varying parameters (for example, case fatality - ratio, with age) for proper parametrization of lower-resolution compartmental - models (for example, with broad age categories) and subsequent upscaling of - model outputs to high resolution (for example, as needed when calculating - age-sensitive measures like years-life-lost). +Description: A convenient framework for aggregating and disaggregating continuously + varying parameters (for example, case fatality ratio, with age) for proper + parametrization of lower-resolution compartmental models (for example, with + broad age categories) and subsequent upscaling of model outputs to high resolution + (for example, as needed when calculating age-sensitive measures like years-life-lost). License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) diff --git a/R/aggregate.R b/R/aggregate.R index 6770f0a..9533628 100644 --- a/R/aggregate.R +++ b/R/aggregate.R @@ -39,11 +39,13 @@ utils::globalVariables(c( #' ifr_levin <- function(age_in_years) { #' (10^(-3.27 + 0.0524 * age_in_years))/100 #' } -#' age_limits <- c(seq(0, 69, by = 5), 70, 80, 100) +#' age_limits <- c(seq(0, 69, by = 5), 70, 80, 101) #' age_pyramid <- data.frame( -#' from = 0:100, weight = ifelse(0:100 < 65, 1, .99^(0:100-64)) -#' ) # flat age distribution, then 1% annual deaths -#' ifr_alembic <- alembic(ifr_levin, age_pyramid, age_limits, 0:100) +#' from = 0:101, weight = ifelse(0:101 < 65, 1, .99^(0:101-64)) +#' ) +#' age_pyramid$weight[102] <- 0 +#' # flat age distribution, then 1% annual deaths, no one lives past 101 +#' ifr_alembic <- alembic(ifr_levin, age_pyramid, age_limits, 0:101) #' #' @importFrom utils head tail #' @importFrom stats integrate @@ -115,11 +117,15 @@ alembic <- function( #' ifr_levin <- function(age_in_years) { #' (10^(-3.27 + 0.0524 * age_in_years))/100 #' } -#' age_limits <- c(seq(0, 69, by = 5), 70, 80, 100) +#' +#' age_limits <- c(seq(0, 69, by = 5), 70, 80, 101) #' age_pyramid <- data.frame( -#' from = 0:99, weight = ifelse(0:99 < 65, 1, .99^(0:99-64)) -#' ) # flat age distribution, then 1% annual deaths -#' alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:100) +#' from = 0:101, weight = ifelse(0:101 < 65, 1, .99^(0:101-64)) +#' ) +#' age_pyramid$weight[102] <- 0 +#' # flat age distribution, then 1% annual deaths, no one lives past 101 +#' +#' alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:101) #' #' ifr_blend <- blend(alembic_dt) #' # the actual function @@ -140,7 +146,8 @@ alembic <- function( #' ) #' # properly aggregated, but not accounting for age distribution #' bad_alembic_dt <- alembic( -#' ifr_levin, within(age_pyramid, weight <- 1), age_limits, 0:100 +#' ifr_levin, +#' within(age_pyramid, weight <- c(rep(1, 101), 0)), age_limits, 0:101 #' ) #' ifr_unif <- blend(bad_alembic_dt) #' lines( @@ -188,13 +195,18 @@ blend <- function( #' ifr_levin <- function(age_in_years) { #' (10^(-3.27 + 0.0524 * age_in_years))/100 #' } -#' age_limits <- c(seq(0, 69, by = 5), 70, 80, 100) -#' age_pyramid <- data.table( -#' from = 0:99, weight = ifelse(0:99 < 65, 1, .99^(0:99-64)) -#' ) # flat age distribution, then 1% annual deaths -#' alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:100) #' -#' results <- data.table(model_partition = head(age_limits, -1))[, value := 10] +#' age_limits <- c(seq(0, 69, by = 5), 70, 80, 101) +#' age_pyramid <- data.frame( +#' from = 0:101, weight = ifelse(0:101 < 65, 1, .99^(0:101-64)) +#' ) +#' age_pyramid$weight[102] <- 0 +#' # flat age distribution, then 1% annual deaths, no one lives past 101 +#' +#' alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:101) +#' +#' results <- data.frame(model_partition = head(age_limits, -1)) +#' results$value <- 10 #' distill(alembic_dt, results) distill <- function( alembic_dt, outcomes_dt, groupcol = names(outcomes_dt)[1] diff --git a/man/alembic.Rd b/man/alembic.Rd index a5ca112..fd85361 100644 --- a/man/alembic.Rd +++ b/man/alembic.Rd @@ -54,10 +54,12 @@ Create the Blending and Distilling Object ifr_levin <- function(age_in_years) { (10^(-3.27 + 0.0524 * age_in_years))/100 } -age_limits <- c(seq(0, 69, by = 5), 70, 80, 100) +age_limits <- c(seq(0, 69, by = 5), 70, 80, 101) age_pyramid <- data.frame( - from = 0:100, weight = ifelse(0:100 < 65, 1, .99^(0:100-64)) -) # flat age distribution, then 1\% annual deaths -ifr_alembic <- alembic(ifr_levin, age_pyramid, age_limits, 0:100) + from = 0:101, weight = ifelse(0:101 < 65, 1, .99^(0:101-64)) +) +age_pyramid$weight[102] <- 0 +# flat age distribution, then 1\% annual deaths, no one lives past 101 +ifr_alembic <- alembic(ifr_levin, age_pyramid, age_limits, 0:101) } diff --git a/man/blend.Rd b/man/blend.Rd index 95585d4..46ea2df 100644 --- a/man/blend.Rd +++ b/man/blend.Rd @@ -20,11 +20,15 @@ bounds) and \code{value} (parameter values for those partitions) ifr_levin <- function(age_in_years) { (10^(-3.27 + 0.0524 * age_in_years))/100 } -age_limits <- c(seq(0, 69, by = 5), 70, 80, 100) + +age_limits <- c(seq(0, 69, by = 5), 70, 80, 101) age_pyramid <- data.frame( - from = 0:99, weight = ifelse(0:99 < 65, 1, .99^(0:99-64)) -) # flat age distribution, then 1\% annual deaths -alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:100) + from = 0:101, weight = ifelse(0:101 < 65, 1, .99^(0:101-64)) +) +age_pyramid$weight[102] <- 0 +# flat age distribution, then 1\% annual deaths, no one lives past 101 + +alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:101) ifr_blend <- blend(alembic_dt) # the actual function @@ -45,7 +49,8 @@ lines( ) # properly aggregated, but not accounting for age distribution bad_alembic_dt <- alembic( - ifr_levin, within(age_pyramid, weight <- 1), age_limits, 0:100 + ifr_levin, + within(age_pyramid, weight <- c(rep(1, 101), 0)), age_limits, 0:101 ) ifr_unif <- blend(bad_alembic_dt) lines( diff --git a/man/distill.Rd b/man/distill.Rd index 9a11166..eb9b85e 100644 --- a/man/distill.Rd +++ b/man/distill.Rd @@ -38,13 +38,18 @@ be done by external grouping then calling \code{distill()}. ifr_levin <- function(age_in_years) { (10^(-3.27 + 0.0524 * age_in_years))/100 } -age_limits <- c(seq(0, 69, by = 5), 70, 80, 100) -age_pyramid <- data.table( - from = 0:99, weight = ifelse(0:99 < 65, 1, .99^(0:99-64)) -) # flat age distribution, then 1\% annual deaths -alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:100) -results <- data.table(model_partition = head(age_limits, -1))[, value := 10] +age_limits <- c(seq(0, 69, by = 5), 70, 80, 101) +age_pyramid <- data.frame( + from = 0:101, weight = ifelse(0:101 < 65, 1, .99^(0:101-64)) +) +age_pyramid$weight[102] <- 0 +# flat age distribution, then 1\% annual deaths, no one lives past 101 + +alembic_dt <- alembic(ifr_levin, age_pyramid, age_limits, 0:101) + +results <- data.frame(model_partition = head(age_limits, -1)) +results$value <- 10 distill(alembic_dt, results) \dontshow{\}) # examplesIf} }