### univariate salmonella agona data
data("salmonella.agona")
## convert to sts class
salmonella <- disProg2sts(salmonella.agona)
## generate formula for temporal and seasonal trends
f.end <- addSeason2formula(f = ~1 + t, S=1, period=52)
model1 <- list(ar = list(f=~1), end = list(f=f.end), family = "NegBin1")
## fit the model
result <- hhh4(salmonella, model1)
## do sequential one-step-ahead predictions for the last 5 weeks
pred <- oneStepAhead(result, nrow(salmonella)-5, type="rolling",
which.start="final", verbose=FALSE)
## test equivalence of parallelized version
if (.Platform$OS.type == "unix" && isTRUE(parallel::detectCores() > 1))
stopifnot(identical(pred,
oneStepAhead(result, nrow(salmonella)-5, type="rolling",
which.start="final", verbose=FALSE, cores=2)))
## oneStepAhead(..., type="final") just means fitted values
stopifnot(identical(
unname(oneStepAhead(result, nrow(salmonella)-5,
type="final", verbose=FALSE)$pred),
unname(tail(fitted(result), 5))))
## compute scores
(sc <- scores(pred))
## compute mean scores
colMeans(sc)
## plot a (non-randomized) PIT histogram for the predictions
with(pred, pit(x = observed, pdistr = "pnbinom", mu = pred, size = exp(psi)))
######################################################################
# Do one-step-ahead predictions for the models from the Paul & Held
# (2011) paper for the influenza data from Bavaria and Baden-Wuerttemberg
# (this takes some time!)
######################################################################
## see ?hhh4 for a specification of the models
## do 1-step ahead predictions for the last two years
tp <- nrow(fluBYBW)-2*52
val_A0 <- oneStepAhead(res_A0,tp=tp)
val_B0 <- oneStepAhead(res_B0,tp=tp)
val_C0 <- oneStepAhead(res_C0,tp=tp)
val_A1 <- oneStepAhead(res_A1,tp=tp)
val_B1 <- oneStepAhead(res_B1,tp=tp)
val_C1 <- oneStepAhead(res_C1,tp=tp)
val_A2 <- oneStepAhead(res_A2,tp=tp)
val_B2 <- oneStepAhead(res_B2,tp=tp)
val_C2 <- oneStepAhead(res_C2,tp=tp)
val_D <- oneStepAhead(res_D,tp=tp)
##################################
## compute scores
################################
#scores
vals <- ls(pattern="val_")
nam <- substring(vals,first=5,last=6)
whichScores <- c("logs", "rps", "ses")
scores_i <- list()
meanScores <- NULL
for(i in seq(along.with=vals)){
sc <- scores(get(vals[i]), which=whichScores, individual=TRUE)
scores_i[[i]] <- sc
meanScores <- rbind(meanScores,colMeans(sc, dims=2))
}
names(scores_i) <- nam
rownames(meanScores) <- nam
##comparison with best model B2
compareWithBest <- function(best, whichModels, nPermut=9999, seed=1234){
set.seed(seed)
pVals <- NULL
for(score in seq_along(whichScores)){
p <- c()
for(model in whichModels){
if(model==best) p <- c(p,NA)
else p <- c(p,permutationTest(scores_i[[model]][,,score],scores_i[[best]][,,score],
plot=TRUE,nPermutation=nPermut, verbose=TRUE)$pVal.permut)
}
pVals <- cbind(pVals,p)
}
return(pVals)
}
pVals_flu <- compareWithBest(best=6, whichModels=1:10,seed=2059710987)
rownames(pVals_flu) <- nam
Run the code above in your browser using DataLab