View source: R/aggregate_moments.R
aggregate_moments | R Documentation |
Aggregation of stationary or predictive moments as calculated using
stationary_moments
or predictive_moments
.
aggregate_moments(momentsObj, aggregation_matrix, by_timepoint = FALSE)
momentsObj |
an object of class |
aggregation_matrix |
an aggregation matrix with either
|
by_timepoint |
logical: is aggregation only across units while preserving
the temporal structure? Note that the new |
An object of class moments_hhh4
representing the new prediction.
# load data:
data("noroBL")
########
# fit a bivariate model:
controlBL <- list(end = list(f = addSeason2formula(~ -1 + fe(1, unitSpecific = TRUE))),
ar = list(f = ~ -1 + fe(1, unitSpecific = TRUE)),
ne = list(f = ~ -1 + fe(1, unitSpecific = TRUE)),
family = "NegBinM", subset = 2:260) # not a very parsimonious parametrization, but feasible
fitBL <- hhh4(noroBL, control = controlBL)
pred_mom <- predictive_moments(fitBL, t_condition = 260, lgt = 52, return_Sigma = TRUE)
# Sigma is required in order to aggregate predictions.
#########
# plot predictions for two regions:
par(mfrow = 1:2)
fanplot_prediction(pred_mom, unit = 1, main = "Bremen")
fanplot_prediction(pred_mom, unit = 2, main = "Lower Saxony")
#########
# aggregation 1: combine the two regions
aggr_matr_pool <- matrix(1, ncol = 2)
# specify by_timepoint = TRUE to keep the temporal structure and aggregate only
# counts from the same week:
pred_mom_pooled <- aggregate_moments(pred_mom, aggr_matr_pool, by_timepoint = TRUE)
fanplot_prediction(pred_mom_pooled, unit = 1, ylim = c(0, 500), main = "Aggregation over regions")
#########
# aggregation 2: total burden in the two regions
aggr_matr_total_burden <- matrix(rep(c(1, 0, 0, 1), 52), nrow = 2,
dimnames = list(c("Bremen", "Lower Saxony"),
NULL))
pred_mom_total_burden <- aggregate_moments(pred_mom, aggr_matr_total_burden)
plot_moments_by_unit(pred_mom_total_burden, main = "Total burdens")
#########
# works also with stationary moments:
stat_mom <- stationary_moments(fitBL, return_Sigma = TRUE)
stat_mom_pooled <- aggregate_moments(stat_mom, aggr_matr_pool, by_timepoint = TRUE)
stat_mom_total_burden <- aggregate_moments(stat_mom, aggr_matr_total_burden, by_timepoint = FALSE)
fanplot_stationary(stat_mom_pooled)
plot_moments_by_unit(stat_mom_total_burden, main = "Total burdens")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.