
Convert between tbl_now and epinowcast
tbl_now_epinowcast.Rdepinowcast represents the same observations in several shapes:
the raw long input
data.frame(reference_date,report_dateand a cumulativeconfirmcolumn, plus optional grouping columns) consumed byepinowcast::enw_preprocess_data();the preprocessed object returned by
epinowcast::enw_preprocess_data()(a nesteddata.tableused downstream for modelling, summaries and plotting);a fitted
epinowcastobject (which extends the preprocessed object).
tbl_now_from_epinowcast() accepts any of these and converts the
cumulative observations into a tbl_now of data_type = "count-cumulative".
When given a preprocessed or fitted object, the grouping (by) columns are
detected automatically and the observations are those retained by
preprocessing (i.e. truncated at max_delay).
tbl_now_to_epinowcast() takes a tbl_now and, by default, builds the
preprocessed epinowcast::enw_preprocess_data() object (the representation
used for epinowcast's summaries and plots). With preprocess = FALSE it
returns the completed long observation data.table (the model input
format, as produced by epinowcast::enw_complete_dates()).
Usage
tbl_now_from_epinowcast(
data,
...,
reference_date = "reference_date",
report_date = "report_date",
confirm = "confirm",
strata = NULL,
verbose = TRUE
)
tbl_now_to_epinowcast(
x,
...,
max_delay = NULL,
missing_reference = FALSE,
preprocess = TRUE,
verbose = TRUE,
quiet = FALSE
)Arguments
- data
Source data: a raw long
data.frame/data.table, anenw_preprocess_dataobject, or a fittedepinowcastobject.- ...
Additional arguments forwarded to
as_tbl_now()(forfrom) or toepinowcast::enw_preprocess_data()(forto).- reference_date, report_date, confirm
Column names (raw input only; ignored for preprocessed/fitted objects).
- strata
Optional character vector of grouping columns. If
NULL(default) the grouping is taken from the preprocessed object'sby, or, for raw input, any column other thanreference_date,report_dateandconfirm.- verbose
Logical. Print the choices that were made.
- x
A
tbl_nowobject.- max_delay
Maximum delay (in
timesteps) to use when preprocessing. IfNULLit is inferred from the data asmax(.delay) + 1.- missing_reference
Passed to
epinowcast::enw_complete_dates(). Defaults toFALSE(unlike epinowcast's own default ofTRUE): atbl_nownever carries reports with a missingreference_date, so leaving thisTRUEwould synthesise NA-reference padding rows the data never had.- preprocess
If
TRUE(default) returns anenw_preprocess_dataobject; ifFALSEreturns the completed observationdata.table.- quiet
Logical. If
TRUE, suppress the lossy-conversion warning emitted bytbl_now_to_epinowcast()(see the Round-trip section).
Value
tbl_now_from_epinowcast() returns a tbl_now.
tbl_now_to_epinowcast() returns an enw_preprocess_data object or a
data.table.
Round-trip
The round-trip is not the identity, and tbl_now_to_epinowcast() warns
to that effect (silence it with quiet = TRUE). If you already have the data
in epinowcast's format, work from it directly rather than converting through
tbl_now and back.
tbl_now_from_epinowcast(tbl_now_to_epinowcast(x)) recovers x up to the
max_delay truncation that epinowcast applies during preprocessing: reports
with a delay beyond max_delay are dropped by
epinowcast::enw_preprocess_data() and so are absent from the result.
Conversely, tbl_now_to_epinowcast(tbl_now_from_epinowcast(pobs)) is not
identical to pobs, because a tbl_now does not retain everything an
enw_preprocess_data object carries:
Covariate columns that are neither the core
reference_date/report_date/confirm, a grouping (by) column, nor a materialised temporal-effect column are dropped. The temporal-effect columns (holidays, Fourier terms, calendar effects) are carried over: the lazytemporal_effects()spec is materialised withcompute_temporal_effects()and the resulting columns are passed through to the observations andmetareference/metareporttables.Grouping indices (
.group) are reassigned from the factor levels, so the row order of the nested tables can differ even though the underlying values match.NA-reference padding is not regenerated by default (see
missing_reference).max_confirm(and the derivedcum_prop_reported) will not match for reference dates whose reporting completes aftermax_delay. The modelledconfirm(the reporting triangle) is truncated atmax_delay, but epinowcast computesmax_confirmas the eventual final total from the untruncated history. Atbl_nowonly stores the truncated triangle, so reports arriving beyondmax_delayare gone: on the way backepinowcast::enw_preprocess_data()recomputesmax_confirmfrom the within-window data and obtains a smaller value. Theconfirmcounts themselves still round-trip exactly; only these truncation-derived summary columns differ. (For example, ingermany_covid19_hospthe 2021-04-06 / 00-04 cell reaches 7 by delay 40 but a final 11 only at delay 74, so itsmax_confirmis 11 inpobsand 7 after the round-trip.)
Examples
# Preprocessing epinowcast data is slow, so this is wrapped in \donttest{}.
# \donttest{
library(data.table)
#>
#> Attaching package: ‘data.table’
#> The following object is masked from ‘package:base’:
#>
#> %notin%
library(epinowcast)
#> ! `enw_cache_location` is not set.
#> ℹ Using `tempdir()` at /tmp/RtmpWlmMbZ for the epinowcast model cache location.
#> ℹ Set a specific cache location using `enw_set_cache` to control Stan
#> recompilation in this R session or across R sessions.
#> ℹ For example: `enw_set_cache(tools::R_user_dir(package = "epinowcast",
#> "cache"), type = c('session', 'persistent'))`.
#> ℹ See `?enw_set_cache` for details.
#> This message is displayed once per session.
#Read data from epinowcast
obs <- germany_covid19_hosp[location == "DE"]
#Remove unused column
obs <- obs[, location := NULL]
#Pre-process data
pobs <- epinowcast::enw_preprocess_data(obs, max_delay = 40, by = "age_group")
# From the data.table input format ...
nowobj <- tbl_now_from_epinowcast(obs, strata = c("age_group"))
#>
#> ── Converted epinowcast <data> into a <tbl_now>
#> • event_date: "reference_date"
#> • report_date: "report_date"
#> • data_type: "count-cumulative"
#> • now: "2021-10-20"
#> • event_units: "days"
#> • report_units: "days"
#> • strata: "age_group"
#> • case_count: "confirm"
# ... or from a preprocessed epinowcast object
tbl_epi <- tbl_now_from_epinowcast(pobs)
#>
#> ── Converted epinowcast <data> into a <tbl_now>
#> • event_date: "reference_date"
#> • report_date: "report_date"
#> • data_type: "count-cumulative"
#> • now: "2021-10-20"
#> • event_units: "days"
#> • report_units: "days"
#> • strata: "age_group"
#> • case_count: "confirm"
#You can also convert to epinowcast preprocess data format
preprocessed_tbl <- tbl_now_to_epinowcast(tbl_epi, quiet = TRUE)
#>
#> ── Converting <tbl_now> into an epinowcast object
#> • reference_date <- "reference_date"
#> • report_date <- "report_date"
#> • confirm <- "confirm"
#> • by: "age_group"
#> • max_delay: 40
#> • missing_reference: FALSE
#> • preprocess: TRUE
# }