
Tidy a fitted nowcast into one standard table
tidy.nowcast.RdEvery nowcasting package returns its answer in its own shape – a matrix of
posterior draws, an stsNC object, a Stan fit, an INLA summary, a bare list.
tidy() turns any of them into the same table, so downstream code
(plotting, scoring, comparison) does not care which engine produced it.
Usage
tidy(x, ...)
# S3 method for class 'baselinenowcast_df'
tidy(x, probs = NULL, ...)
# S3 method for class 'epinowcast'
tidy(x, probs = NULL, ...)
# S3 method for class 'stsNC'
tidy(x, probs = NULL, ...)
# S3 method for class 'estimate_infections'
tidy(x, probs = NULL, ...)
# S3 method for class 'epinow'
tidy(x, probs = NULL, ...)
# S3 method for class 'estimate_truncation'
tidy(x, probs = NULL, ...)
# S3 method for class 'list'
tidy(x, probs = NULL, engine = NULL, level = NULL, ...)Arguments
- x
A fitted nowcast. See Supported objects.
- ...
Passed to methods.
- probs
Optional numeric vector of probabilities in
[0, 1]. Adds aq*column per probability. Only available for engines that expose draws.- engine
Optional string naming the engine. Needed only for the shapes that arrive as an unclassed list – a NobBS fit, an
EpiNow2::regional_epinow()result, or a per-stratum list of baselinenowcast orsurveillance::nowcast()fits – which are otherwise recognised by their structure.- level
Interval width to report for an engine that does not say what it produced. Only used by the NobBS branch (see
levelunder Value);NULL, the default, reportsNA.
Value
A tibble with one row per event date (per stratum, where the fit carries strata) and these columns:
event_dateDate. The event/reference date, on the engine's own grid.tidy()deliberately does not re-grid: some packages bin onto week starts of their own choosing, and silently snapping them would hide a real difference. Align afterwards if you need to.stratumcharacter. One label per stratum the fit reports, and"all"when the fit is unstratified. Several stratifying columns are pasted" | "-separated, matching thetriangle_listnaming oftbl_now_to_baselinenowcast().(stratum, event_date)is therefore a unique key.estimatenumeric. The point nowcast – the posterior median where the engine provides draws or a median, otherwise its point estimate.conf.low,conf.highnumeric. Interval bounds, following broom's naming.NAwhen the engine returns no interval.levelnumeric. The width the interval actually has, e.g.0.95. Engines differ – epinowcast reports a 90% band by default while others report 95% – and without this column those get compared as if they were the same thing.NAwhenever the width cannot be established: because the engine returned no interval (a baselinenowcast fit made withoutput_type = "point"), or because it returned one without saying how wide it is. NobBS is the latter case – itslower/uppercome fromspecs$conf, andNobBS()does not returnspecs– so passlevelyourself if you need it filled in. A guessed default is worse thanNAin the one column that exists to stop widths being compared blindly.enginecharacter. Which package produced the fit.
When probs is supplied, one extra column per requested quantile is appended,
named q5, q50, q95 and so on (the probability times 100, so 0.025
becomes q2.5).
Which engines can honour probs
Only the engines that expose draws can compute an arbitrary quantile: diseasenowcasting, baselinenowcast and epinowcast. The others report a fixed set of summaries and nothing else, so asking them for a quantile they did not compute is an error rather than a silent approximation.
Supported objects
nowcast_prediction(S7) fromdiseasenowcasting::predict()baselinenowcast_dffrombaselinenowcast::baselinenowcast()epinowcastfitsstsNCfromsurveillance::nowcast()the list returned by
NobBS::NobBS()or byNobBS::NobBS.strat()(the stratified variant is recognised by itsstratumcolumn)a list of
baselinenowcast_dffits, one per stratum – whatlapply()-ing over a tbl_now_triangle_list produces. Each element is tidied and labelled with its list name, giving the same one-block-per-stratum table the natively stratified engines return.
See also
run_nowcast() and tidy(), which give you this shape
without needing to call the modelling package yourself;
tidy() for a backtest;
tidy.epidist_fit() and tidy.estimate_dist() for fitted delay
distributions rather than case counts;
score_nowcast() to score the result. The
One dataset, many nowcasts article
shows each engine's native output next to this one.
Examples
data(denguedat)
# A few years of data and a small number of draws, to keep the example quick.
dengue <- tbl_now(denguedat[1:10000, ],
event_date = "onset_week", report_date = "report_week", verbose = FALSE
)
triangle <- suppressWarnings(
tbl_now_to_baselinenowcast(dengue, verbose = FALSE)
)
#> ℹ Using max_delay = 15 from data
fit <- baselinenowcast::baselinenowcast(
triangle, output_type = "samples", draws = 25
)
#> ℹ 0.5 reference times were specified for delay estimation but 0.489 of reference times used for delay estimation.
#> ℹ `prop_delay` not identical to the proportion of reference times used for delay estimation due to rounding.
tidy(fit)
#> # A tibble: 191 × 7
#> event_date stratum estimate conf.low conf.high level engine
#> <date> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 1990-01-01 all 61 61 61 0.95 baselinenowcast
#> 2 1990-01-08 all 50 50 50 0.95 baselinenowcast
#> 3 1990-01-15 all 44 44 44 0.95 baselinenowcast
#> 4 1990-01-22 all 46 46 46 0.95 baselinenowcast
#> 5 1990-01-29 all 39 39 39 0.95 baselinenowcast
#> 6 1990-02-05 all 34 34 34 0.95 baselinenowcast
#> 7 1990-02-12 all 24 24 24 0.95 baselinenowcast
#> 8 1990-02-19 all 17 17 17 0.95 baselinenowcast
#> 9 1990-02-26 all 17 17 17 0.95 baselinenowcast
#> 10 1990-03-05 all 16 16 16 0.95 baselinenowcast
#> # ℹ 181 more rows