## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE)
library(mudnester)

## ----data---------------------------------------------------------------------
set.seed(42)
n <- 400
diag <- data.frame(
  onset_date = as.Date("2024-01-01") + sample(0:59, n, replace = TRUE),
  age        = sample(0:90, n, replace = TRUE),
  stringsAsFactors = FALSE
)
diag <- preening(diag, age_col = "age", scheme = "flucan_sentinel")

cases_monthly <- roost(
  diag,
  date_col   = "onset_date",
  time_unit  = "month",
  group_cols = "age_group"
)

cases_monthly

## ----factor-table-------------------------------------------------------------
factors <- data.frame(
  age_group    = rep(c("0-4", "5-15", "16-49", "50-64", "65+"), each = 2),
  date_start   = rep(as.Date(c("2024-01-01", "2024-02-01")), 5),
  date_end     = rep(as.Date(c("2024-01-31", "2024-02-29")), 5),
  factor       = c(3.2, 2.8,  2.6, 2.3,  1.8, 1.6,  1.5, 1.4,  1.3, 1.2),
  factor_lower = c(2.4, 2.1,  2.0, 1.8,  1.4, 1.3,  1.2, 1.1,  1.1, 1.0),
  factor_upper = c(4.2, 3.6,  3.3, 2.9,  2.3, 2.0,  1.9, 1.7,  1.6, 1.5),
  source       = "Illustrative multiplier, SCPHU surveillance evaluation 2025"
)

knitr::kable(factors)

## ----apply-corncrake----------------------------------------------------------
cases_corrected <- corncrake(
  cases_monthly,
  factor_table = factors,
  group_by     = "age_group"
)

cases_corrected[, c("age_group", "month", "n", "ascertainment_factor",
                     "corrected_count", "corrected_count_lower", "corrected_count_upper")]

## ----ci-methods---------------------------------------------------------------
corncrake(cases_monthly, factor_table = factors, group_by = "age_group",
          ci_method = "propagate")[
  , c("age_group", "month", "n", "corrected_count",
      "corrected_count_lower", "corrected_count_upper")
]

## ----ratio-estimate-----------------------------------------------------------
lab_positive_monthly <- cases_monthly
lab_positive_monthly$n <- round(cases_monthly$n * runif(nrow(cases_monthly), 1.3, 2.5))

cases_corrected2 <- corncrake(
  cases_monthly,
  method              = "ratio_estimate",
  group_by            = "age_group",
  secondary_data      = lab_positive_monthly,
  secondary_count_col = "n"
)

cases_corrected2[, c("age_group", "month", "n", "ascertainment_factor", "corrected_count")]

## ----severity-anchor-worked---------------------------------------------------
disease_x <- data.frame(
  month    = as.Date("2024-01-01"),
  n_cases  = 5000L,
  n_deaths = 100L
)

disease_x_corrected <- corncrake(
  disease_x,
  count_col             = "n_cases",
  method                 = "severity_anchor",
  time_col               = "month",
  severity_count_col     = "n_deaths",
  reference_rate         = 0.01,
  reference_rate_lower   = 0.005,
  reference_rate_upper   = 0.020,
  reference_source       = "WHO Disease X planning scenario, IFR 1.0% (0.5-2.0%)"
)

disease_x_corrected[, c("n_cases", "n_deaths", "ascertainment_factor",
                         "ascertainment_factor_lower", "ascertainment_factor_upper",
                         "corrected_count")]

## ----severity-anchor-table----------------------------------------------------
disease_x_stratified <- data.frame(
  month     = as.Date(c("2024-01-01", "2024-01-01")),
  age_group = c("0-17", "18+"),
  n_cases   = c(1000L, 4000L),
  n_deaths  = c(1L, 99L)
)

reference_rates <- data.frame(
  age_group  = c("0-17", "18+"),
  date_start = as.Date(NA),  # open-ended: one reference rate per age group, all time
  date_end   = as.Date(NA),
  rate       = c(0.001, 0.02),
  source     = "Illustrative age-stratified reference IFR"
)

corncrake(
  disease_x_stratified,
  count_col           = "n_cases",
  method               = "severity_anchor",
  group_by             = "age_group",
  time_col             = "month",
  severity_count_col   = "n_deaths",
  reference_rate       = reference_rates
)[, c("age_group", "n_cases", "n_deaths", "ascertainment_factor")]

## ----rates--------------------------------------------------------------------
cases_with_pop <- cases_monthly
cases_with_pop$pop <- ifelse(cases_with_pop$age_group == "0-4", 8000,
                       ifelse(cases_with_pop$age_group == "5-15", 15000,
                       ifelse(cases_with_pop$age_group == "16-49", 45000,
                       ifelse(cases_with_pop$age_group == "50-64", 20000, 18000))))

corncrake(
  cases_with_pop, factor_table = factors, group_by = "age_group",
  denominator_col = "pop", rate_multiplier = 100000
)[, c("age_group", "month", "corrected_count", "corrected_rate")]

## ----missing-example, error=TRUE----------------------------------------------
try({
sparse_factors <- factors[factors$age_group != "0-4", ]
corncrake(cases_monthly, factor_table = sparse_factors, group_by = "age_group",
          on_missing = "error")
})

