## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE)
library(mudnester)

## ----data---------------------------------------------------------------------
set.seed(7)
n <- 500
linked <- data.frame(
  onset_date       = as.Date("2024-01-01") + sample(0:89, n, replace = TRUE),
  age               = sample(0:90, n, replace = TRUE),
  stringsAsFactors  = FALSE
)
linked <- preening(linked, age_col = "age", scheme = "flucan_sentinel")

# Simulate a care pathway: ~8% of cases are admitted, ~15% of those reach
# ICU, ~5% of those die -- each downstream event's date lags the previous
admitted <- sample(seq_len(n), size = round(n * 0.08))
linked$admission_date <- as.Date(NA)
linked$admission_date[admitted] <- linked$onset_date[admitted] + sample(1:5, length(admitted), TRUE)

icu <- sample(admitted, size = round(length(admitted) * 0.15))
linked$icu_date <- as.Date(NA)
linked$icu_date[icu] <- linked$admission_date[icu] + sample(1:3, length(icu), TRUE)

died <- sample(icu, size = max(1, round(length(icu) * 0.30)))
linked$fatality_date <- as.Date(NA)
linked$fatality_date[died] <- linked$icu_date[died] + sample(1:7, length(died), TRUE)

head(linked[, c("onset_date", "age_group", "admission_date", "icu_date", "fatality_date")])

## ----flyway-------------------------------------------------------------------
linked_monthly <- flyway(
  linked,
  events = c(
    cases            = "onset_date",
    hospitalisations = "admission_date",
    icu              = "icu_date",
    deaths           = "fatality_date"
  ),
  time_unit  = "month",
  group_cols = "age_group"
)

linked_monthly

## ----rates--------------------------------------------------------------------
linked_monthly$chr_obs <- linked_monthly$n_hospitalisations / linked_monthly$n_cases
linked_monthly$cfr_obs <- linked_monthly$n_deaths / linked_monthly$n_cases

linked_monthly[, c("age_group", "month", "n_cases", "n_hospitalisations", "n_deaths",
                    "chr_obs", "cfr_obs")]

## ----na-check-----------------------------------------------------------------
# No NAs in any count column, even though the underlying events have very
# different calendar coverage
colSums(is.na(linked_monthly[, c("n_cases", "n_hospitalisations", "n_icu", "n_deaths")]))

## ----corncrake-integration----------------------------------------------------
corncrake(
  linked_monthly,
  count_col           = "n_cases",
  method               = "severity_anchor",
  group_by             = "age_group",
  severity_count_col   = "n_deaths",
  reference_rate       = 0.01,
  reference_rate_lower = 0.005,
  reference_rate_upper = 0.02,
  reference_source     = "Illustrative reference IFR"
)[, c("age_group", "month", "n_cases", "n_deaths", "ascertainment_factor", "corrected_count")]

## ----unnamed, error=TRUE------------------------------------------------------
try({
flyway(linked, events = c("onset_date", "admission_date"))
})

