分层时间序列 [英] Hierarchical Time Series

查看:60
本文介绍了分层时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R中的hts软件包将HTS模型拟合到火车数据上,并使用"arima"选项来预测和计算保持/测试数据的准确性. 这是我的代码:

I used the hts package in R to fit an HTS model on train data, used "arima" option to forecast and computed the accuracy on the holdout/test data. Here is my code:

library(hts)
data<-read.csv("C:/TS.csv")
ts_train <- ts(data[,-1],frequency=12, start=c(2000,1))
hts_train <- hts(ts_train, nodes=list(2, c(4, 2)))
data.test<-read.csv("C:/TStest.csv")
ts_test <- ts(data.test[,-1],frequency=12, start=c(2003,1))
hts_test <- hts(ts_test, nodes=list(2, c(4, 2)))
forecast <- forecast(hts_train, h=15, method="bu", fmethod="arima", keep.fitted = TRUE, keep.resid = TRUE)
accuracy<-accuracy.gts(forecast, hts_test)

现在,让我们假设我对保持样本的准确性感到满意,并且我想将测试数据与火车数据放在一起,并使用完整的数据集进行重新预测.

Now, let's suppose I'm happy with the accuracy on the holdout sample and I'd like to lump the test data back with the train data and re-forecast using the full set.

我尝试使用此代码:

data.full<-read.csv("C:/TS_full.csv")
ts_full <- ts(data.full[,-1],frequency=12, start=c(2000,1))
hts_full <- hts(ts_full, nodes=list(2, c(4, 2)))
forecast.full <- forecast(hts_full, h=15, method="bu", fmethod="arima", keep.fitted = TRUE, keep.resid = TRUE)

现在,我不确定这样做是否正确,因为我不知道用于估算火车数据的ARIMA模型是否与我现在用于预测的ARIMA模型相同完整的数据集(我假设fmethod ="arima"使用auto.arima).我希望它们保持相同的模型,否则我的样本外准确性评估所评估的模型与我用于最终预测的模型不同.

Now, I'm not sure that this is really the right way to do it as I don't know if ARIMA models that were used to estimate my train data are the same ARIMA models that I'm now using to forecast the full data set (I presume fmethod="arima" utilizes auto.arima) . I'd like them to remain the same models, otherwise the models evaluated by my out of sample accuracy measures are different from the models I used for the final forecast.

我看到有一个FUN参数,它表示一个用户定义的函数,该函数返回可以传递给预测函数的对象".也许可以在我的代码的最后一行中以某种方式使用该参数,以确保将我适合火车数据的模型用于预测整个数据集?

I see there is a FUN argument that represents "a user-defined function that returns an object which can be passed to the forecast function". Perhaps that argument can be used in the last line of my code somehow to make sure the models I fit on the train data are used to forecast the full data set?

任何有关哪种R代码都将有所帮助的建议将不胜感激.

Any suggestions on what sort of R code would help would be much appreciated.

推荐答案

未设置该功能.但是,做您想做的事并不难.这是一些示例代码

The functions are not set up to do that. However, it is not too difficult to do what you want. Here is some sample code

library(hts)
data <- htseg2

# Split data into training and test sets
hts_train <- window(data, end=2004)
hts_test <- window(data, start=2005)

# Fit models and compute forecasts on all nodes using training data
train <- aggts(hts_train)
fmodels <- list()
fc <- matrix(0, ncol=ncol(train), nrow=3)
for(i in 1:ncol(train))
{
  fmodels[[i]] <- auto.arima(train[,i])
  fc[,i] <- forecast(fmodels[[i]],h=3)$mean
}
forecast <- combinef(fc, nodes=data$nodes)
accuracy <- accuracy.gts(forecast, hts_test)

# Forecast on full data set without re-estimating parameters
full <- aggts(data)
fcfull <- matrix(0, ncol=ncol(full), nrow=15)
for(i in 1:ncol(full))
{
  fcfull[,i] <- forecast(Arima(full[,i], model=fmodels[[i]]),
                         h=15)$mean
}
forecast.full <- combinef(fcfull, nodes=data$nodes)

# Forecast on full data set with same models but re-estimated parameters
full <- aggts(data)
fcfull <- matrix(0, ncol=ncol(full), nrow=15)
for(i in 1:ncol(full))
{
  fcfull[,i] <- forecast(Arima(full[,i], 
                       order=fmodels[[i]]$arma[c(1,6,2)],
                       seasonal=fmodels[[i]]$arma[c(3,7,4)]),
                       h=15)$mean
}
forecast.full <- combinef(fcfull, nodes=data$nodes)

这篇关于分层时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆