
Last chance! 50% off unlimited learning
Sale ends in
### univariate example
data("salmAllOnset")
## fit a hhh4 model to the first 13 years
salmModel <- list(end = list(f = addSeason2formula(~1 + t)),
ar = list(f = ~1), family = "NegBin1", subset = 2:678)
salmFit <- hhh4(salmAllOnset, salmModel)
## simulate the next 20 weeks ahead
salmSims <- simulate(salmFit, nsim = 300, seed = 3, subset = 678 + seq_len(20),
y.start = observed(salmAllOnset)[678,])
## compare final size distribution to observed value
plot(salmSims)
## simulated time series
plot(salmSims, type = "time", main = "2-weeks-ahead simulation")
### multivariate example
data("measlesWeserEms")
## fit a hhh4 model to the first year
measlesModel <- list(
end = list(f = addSeason2formula(~1), offset = population(measlesWeserEms)),
ar = list(f = ~1),
ne = list(f = ~1 + log(pop),
weights = W_powerlaw(maxlag = 5, normalize = TRUE)),
family = "NegBin1", subset = 2:52,
data = list(pop = population(measlesWeserEms)))
measlesFit1 <- hhh4(measlesWeserEms, control = measlesModel)
measlesFit2 <- update(measlesFit1, family = "Poisson")
## simulate realizations from this model during the second year
measlesSims <- lapply(X = list(NegBin = measlesFit1, Poisson = measlesFit2),
FUN = simulate, nsim = 50, seed = 1, subset = 53:104,
y.start = observed(measlesWeserEms)[52,])
## final size of the first model
plot(measlesSims[[1]])
## stratified by groups of districts
plot(measlesSims[[1]], groups = factor(substr(colnames(measlesWeserEms), 4, 4)))
## a class and plot-method for a list of simulations from different models
measlesSims <- as.hhh4simslist(measlesSims)
plot(measlesSims)
## simulated time series
plot(measlesSims, type = "time", individual = TRUE, ylim = c(0, 80))
## compare proper scoring rules for a specific subset of the regions
## (CAVE: these are univariate scores for each time point and region,
## which do not account for dependence over time)
measlesScores5 <- scores(measlesSims, which = "rps",
units = substr(colnames(measlesWeserEms), 4, 4) == "5")
sapply(measlesScores5, mean)
Run the code above in your browser using DataLab