# hts example
if (FALSE) {
h <- 12
ally <- aggts(htseg1)
n <- nrow(ally)
p <- ncol(ally)
allf <- matrix(NA, nrow = h, ncol = p)
res <- matrix(NA, nrow = n, ncol = p)
for(i in 1:p)
{
fit <- auto.arima(ally[, i])
allf[, i] <- forecast(fit, h = h)$mean
res[, i] <- na.omit(ally[, i] - fitted(fit))
}
allf <- ts(allf, start = 51)
y.f <- MinT(allf, get_nodes(htseg1), residual = res, covariance = "shr",
keep = "gts", algorithms = "lu")
plot(y.f)
y.f_cg <- MinT(allf, get_nodes(htseg1), residual = res, covariance = "shr",
keep = "all", algorithms = "cg")
}
if (FALSE) {
h <- 12
ally <- abs(aggts(htseg2))
allf <- matrix(NA, nrow = h, ncol = ncol(ally))
res <- matrix(NA, nrow = nrow(ally), ncol = ncol(ally))
for(i in 1:ncol(ally)) {
fit <- auto.arima(ally[, i], lambda = 0, biasadj = TRUE)
allf[,i] <- forecast(fit, h = h)$mean
res[,i] <- na.omit(ally[, i] - fitted(fit))
}
b.f <- MinT(allf, get_nodes(htseg2), residual = res, covariance = "shr",
keep = "bottom", algorithms = "lu")
b.nnf <- MinT(allf, get_nodes(htseg2), residual = res, covariance = "shr",
keep = "bottom", algorithms = "lu", nonnegative = TRUE, parallel = TRUE)
}
# gts example
if (FALSE) {
abc <- ts(5 + matrix(sort(rnorm(200)), ncol = 4, nrow = 50))
g <- rbind(c(1,1,2,2), c(1,2,1,2))
y <- gts(abc, groups = g)
h <- 12
ally <- aggts(y)
n <- nrow(ally)
p <- ncol(ally)
allf <- matrix(NA,nrow = h,ncol = ncol(ally))
res <- matrix(NA, nrow = n, ncol = p)
for(i in 1:p)
{
fit <- auto.arima(ally[, i])
allf[, i] <- forecast(fit, h = h)$mean
res[, i] <- na.omit(ally[, i] - fitted(fit))
}
allf <- ts(allf, start = 51)
y.f <- MinT(allf, groups = get_groups(y), residual = res, covariance = "shr",
keep = "gts", algorithms = "lu")
plot(y.f)
}
Run the code above in your browser using DataLab