TL;DR
This is a longer article explaining the workings and the choices behind the GAM nowcasting model and its advantages and limitations.
Why nowcasting_mgcv()
nowcasting_mgcv() fits the same nowcasting model as
nowcasting_inla(), a negative binomial model for the delay
triangle with smooth effects for time and delay, but as a generalised
additive model (GAM) with mgcv
instead of INLA. mgcv is on CRAN and is
installed together with nowcaster, so this function works
out of the box, which makes it a good choice when INLA
cannot be installed or when a fast estimate is needed.
The model
Let Y_{t,d} be the number of cases with onset in week t that were reported with a delay of d weeks. The model is
Y_{t,d} \sim \text{NegBinom}(\lambda_{t,d}, \phi), \qquad \log(\lambda_{t,d}) = \alpha + f_1(t) + f_2(d),
where f_1 and f_2 are penalised thin plate regression
splines and \phi is the dispersion
parameter, all estimated by mgcv::gam(). The cells of the
delay triangle that are not observed yet (t +
d beyond the present week) are then predicted. The uncertainty is
propagated by sampling 1,000 sets of coefficients from their approximate
posterior distribution and, for each set, sampling the unobserved counts
from the negative binomial distribution. Summing the observed and
sampled counts over the delays gives 1,000 trajectories of the epidemic
curve, which are summarised by their median and 50% and 95%
intervals.
Non-structured nowcasting
We use the SARI data from Belo Horizonte shipped with the package. The function needs the data set and the names of the onset and report date columns.
now_mgcv <- nowcasting_mgcv(dataset = sragBH,
date_onset = DT_SIN_PRI,
date_report = DT_DIGITA,
data.by.week = TRUE)
names(now_mgcv)#> [1] "total" "data"
head(now_mgcv$total)#> # A tibble: 6 × 7
#> Time dt_event Median LI LS LIb LSb
#> <int> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 17 2021-12-19 625 621 633 623 627
#> 2 18 2021-12-26 695 687 707. 692 698
#> 3 19 2022-01-02 812 801 829 807 817
#> 4 20 2022-01-09 887 870 910. 881 893
#> 5 21 2022-01-16 817 800. 845 810 825.
#> 6 22 2022-01-23 629 608 661 622 639
total holds the estimate for each week of onset
(dt_event): the Median, the 50% interval
(LIb, LSb) and the 95% interval
(LI, LS). With
data.by.week = TRUE, data holds the delay
triangle used to fit the model, from which we get the curve as observed
at the time of the last record:
observed <- now_mgcv$data |>
group_by(dt_event) |>
summarise(observed = sum(Y, na.rm = TRUE))
ggplot(now_mgcv$total, aes(x = dt_event)) +
geom_ribbon(aes(ymin = LI, ymax = LS), fill = "grey70", alpha = 0.5) +
geom_ribbon(aes(ymin = LIb, ymax = LSb), fill = "grey50", alpha = 0.5) +
geom_line(aes(y = Median, colour = "Nowcasting")) +
geom_line(data = observed, aes(y = observed, colour = "Observed")) +
scale_colour_manual(values = c(Nowcasting = "black", Observed = "grey50"),
name = "") +
scale_x_date(date_breaks = "2 weeks", date_labels = "%V/%y") +
labs(x = "Week of onset of symptoms", y = "Number of cases") +
theme_bw() +
theme(legend.position = "bottom", axis.text.x = element_text(angle = 90))
Controlling the estimation window
Three arguments, all in weeks, control which part of the data enters the model:
-
Dmax: the maximum delay considered (default 15). Cases reported later thanDmaxweeks after onset are discarded. -
wdw: the length of the window, counted back from the last onset week, used to fit the model (default 30). -
trim.data: the number of most recent weeks to drop before fitting (default 0).
A shorter window makes the fit faster and more local:
now_short <- nowcasting_mgcv(dataset = sragBH,
date_onset = DT_SIN_PRI,
date_report = DT_DIGITA,
Dmax = 10,
wdw = 20)
tail(now_short$total)#> # A tibble: 6 × 7
#> Time dt_event Median LI LS LIb LSb
#> <int> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 16 2022-02-20 355 322 398. 342 370
#> 2 17 2022-02-27 344 302 402. 329 361
#> 3 18 2022-03-06 368 312. 433 348. 389
#> 4 19 2022-03-13 343 278. 429. 319. 371
#> 5 20 2022-03-20 320 226 435. 284 360
#> 6 21 2022-03-27 267 146 471. 217. 325
Keep Dmax at 9 weeks or more: the smooth for the delay
uses 10 basis functions and needs at least as many distinct delays.
Structured nowcasting, by age
Delays differ between age groups, so the model can be fitted with one
smooth for time and one for delay per age class. Pass the age column to
age_col and choose the age classes with
bins_age ("SI-PNI", "10 years",
"5 years" or a vector of break points).
method.group.gam selects how the smooths are shared
between age classes:
-
"fs"(default): a factor smooth,s(Time, age_class, bs = "fs"), where the curves of the age classes share one smoothing parameter; -
"by": one independent smooth per age class,s(Time, by = age_class), plus an intercept per age class.
now_age <- nowcasting_mgcv(dataset = sragBH,
date_onset = DT_SIN_PRI,
date_report = DT_DIGITA,
age_col = Idade,
bins_age = "10 years",
method.group.gam = "fs")
names(now_age)#> [1] "total" "age"
head(now_age$age)#> # A tibble: 6 × 9
#> Time dt_event fx_etaria fx_etaria.num Median LI LS LIb LSb
#> <int> <date> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 17 2021-12-19 1 1 123 122 126 122 124
#> 2 17 2021-12-19 2 2 15 15 17 15 15
#> 3 17 2021-12-19 3 3 20 20 22 20 20
#> 4 17 2021-12-19 4 4 30 30 31 30 30
#> 5 17 2021-12-19 5 5 29 29 31 29 30
#> 6 17 2021-12-19 6 6 68 68 70 68 68
The element age has the estimate for each age class,
while total sums the trajectories over the age classes. The
age classes are numbered in increasing order of age; with
bins_age = "10 years" class 1 is 0–9 years and class 10 is
90 years or more:
age_labels <- c(paste0(seq(0, 80, 10), "-", seq(9, 89, 10)), "90+")
now_age$age |>
mutate(age_class = factor(age_labels[fx_etaria.num], levels = age_labels)) |>
ggplot(aes(x = dt_event, y = Median)) +
geom_ribbon(aes(ymin = LI, ymax = LS), fill = "grey70", alpha = 0.6) +
geom_line() +
facet_wrap(~age_class, scales = "free_y") +
labs(x = "Week of onset of symptoms", y = "Number of cases") +
theme_bw()
Forecasting
Setting K to a positive number adds K weeks
beyond the last onset week to the delay triangle, so the model also
forecasts cases that have not started yet. Here we cut the data on 4
July 2020, as if we were nowcasting at that date, and forecast two weeks
ahead:
srag_2020 <- sragBH |>
filter(DT_DIGITA <= as.Date("2020-07-04"))
now_forecast <- nowcasting_mgcv(dataset = srag_2020,
date_onset = DT_SIN_PRI,
date_report = DT_DIGITA,
K = 2)
tail(now_forecast$total, 4)#> # A tibble: 4 × 7
#> Time dt_event Median LI LS LIb LSb
#> <int> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 25 2020-06-14 1104. 788. 1602. 988. 1247
#> 2 26 2020-06-21 1096. 562. 2256 856 1404.
#> 3 27 2020-06-28 1194. 418. 3350. 839 1743.
#> 4 28 2020-07-05 1293 314. 5517. 774. 2148
Because the data set holds every case reported up to 2022, we can compare the estimate with what was eventually observed:
eventually_observed <- sragBH |>
mutate(dt_event = DT_SIN_PRI - as.numeric(format(DT_SIN_PRI, "%w"))) |>
count(dt_event, name = "observed") |>
filter(dt_event >= min(now_forecast$total$dt_event),
dt_event <= max(now_forecast$total$dt_event))
now_forecast$total |>
mutate(part = ifelse(Time > max(Time) - 2, "Forecasting", "Nowcasting")) |>
ggplot(aes(x = dt_event)) +
geom_ribbon(aes(ymin = LI, ymax = LS, fill = part), alpha = 0.3) +
geom_line(aes(y = Median, colour = part)) +
geom_line(data = eventually_observed,
aes(y = observed, colour = "Eventually observed")) +
scale_colour_manual(values = c(Nowcasting = "black", Forecasting = "orange",
`Eventually observed` = "red"), name = "") +
scale_fill_manual(values = c(Nowcasting = "grey50", Forecasting = "orange"),
guide = "none") +
scale_x_date(date_breaks = "2 weeks", date_labels = "%V/%y") +
labs(x = "Week of onset of symptoms", y = "Number of cases") +
theme_bw() +
theme(legend.position = "bottom", axis.text.x = element_text(angle = 90))
The last two weeks are the forecast. Their intervals are much wider, as no case with onset in those weeks had been observed at the time.
Trajectories and trend
With trajectories = TRUE the function also returns the
1,000 sampled trajectories. They can be summarised in other ways, for
instance to classify the trend of the curve in the last weeks with
slope.estimate.quant() (which needs the lme4
package). It fits a line to each trajectory over the last
window weeks and returns 1 if the curve is
increasing, 0.5 if probably increasing, 0 if
stable or uncertain, -0.5 if probably decreasing and
-1 if decreasing:
now_traj <- nowcasting_mgcv(dataset = srag_2020,
date_onset = DT_SIN_PRI,
date_report = DT_DIGITA,
trajectories = TRUE)
head(now_traj$trajectories)#> # A tibble: 6 × 4
#> # Groups: Time, dt_event [6]
#> sample Time dt_event Y
#> <chr> <int> <date> <dbl>
#> 1 1 12 2020-03-15 349
#> 2 1 13 2020-03-22 351
#> 3 1 14 2020-03-29 298
#> 4 1 15 2020-04-05 267
#> 5 1 16 2020-04-12 278
#> 6 1 17 2020-04-19 303
slope.estimate.quant(trajectories = now_traj$trajectories, window = 3)#> [1] 0
See also
-
Get
Started, the same analysis with
nowcasting_inla(). - Nowcasting with INLA, for the details of the age-structured model.
- nowcasting_inla() or nowcasting_mgcv(), the same examples run with both functions side by side.