# NOT RUN {
data(FoReco_data)
# Cross-sectional reconciliation for all temporal aggregation levels
# (monthly, bi-monthly, ..., annual)
K <- c(1,2,3,4,6,12)
hts_recf <- NULL
for(i in 1:length(K)){
# base forecasts
id <- which(simplify2array(strsplit(colnames(FoReco_data$base),
split = "_"))[1, ] == paste("k", K[i], sep=""))
mbase <- t(FoReco_data$base[, id])
# residuals
id <- which(simplify2array(strsplit(colnames(FoReco_data$res),
split = "_"))[1, ] == paste("k", K[i], sep=""))
mres <- t(FoReco_data$res[, id])
hts_recf[[i]] <- htsrec(mbase, C = FoReco_data$C, comb = "shr",
res = mres, keep = "recf")
}
names(hts_recf) <- paste("k", K, sep="")
# Forecast reconciliation through temporal hierarchies for all time series
# comb = "acov"
n <- NROW(FoReco_data$base)
thf_recf <- matrix(NA, n, NCOL(FoReco_data$base))
dimnames(thf_recf) <- dimnames(FoReco_data$base)
for(i in 1:n){
# ts base forecasts ([lowest_freq' ... highest_freq']')
tsbase <- FoReco_data$base[i, ]
# ts residuals ([lowest_freq' ... highest_freq']')
tsres <- FoReco_data$res[i, ]
thf_recf[i,] <- thfrec(tsbase, m = 12, comb = "acov",
res = tsres, keep = "recf")
}
# Iterative cross-temporal reconciliation
# Each iteration: t-acov + cs-shr
ite_recf <- iterec(FoReco_data$base, note=FALSE,
m = 12, C = FoReco_data$C,
thf_comb = "acov", hts_comb = "shr",
res = FoReco_data$res, start_rec = "thf")$recf
# Heuristic first-cross-sectional-then-temporal cross-temporal reconciliation
# cs-shr + t-acov
cst_recf <- cstrec(FoReco_data$base, m = 12, C = FoReco_data$C,
thf_comb = "acov", hts_comb = "shr",
res = FoReco_data$res)$recf
# Heuristic first-temporal-then-cross-sectional cross-temporal reconciliation
# t-acov + cs-shr
tcs_recf <- tcsrec(FoReco_data$base, m = 12, C = FoReco_data$C,
thf_comb = "acov", hts_comb = "shr",
res = FoReco_data$res)$recf
# Optimal cross-temporal reconciliation
# comb = "bdshr"
oct_recf <- octrec(FoReco_data$base, m = 12, C = FoReco_data$C,
comb = "bdshr", res = FoReco_data$res, keep = "recf")
# }
Run the code above in your browser using DataLab