我正在将fpp2软件包中的数据集与预测软件包中的tslm函数组合在一起进行聚类。为了获得更准确的投影,我将数据集分为训练集和测试集。这里的主要思想是使用Training set进行线性回归估计,并使用Test set应用该模型。
# CODE
library(fpp2) # required for the data
library(dplyr)
library(forecast)
data_set<-uschange[,1:4]
# Training set
Training_set = window(data_set, start=1970, end=c(2010,4))
# Test set
Test_set = window(data_set, start=2011, end=c(2016,3))
# Built own function
FORECASTING_FUNCTION_LM <- function(Z, hrz = 16) {
timeseries <- msts(Z, start = 1970, seasonal.periods = 4)
forecast <- tslm(timeseries~trend+season,biasadj=TRUE)
}
# Whole set-data_set
FORECASTING_LIST_LM <- lapply(X = data_set, FORECASTING_FUNCTION_LM)
# Training set
FORECASTING_LIST_LM_Training_set<-lapply(Training_set, FORECASTING_FUNCTION_LM)
# Test_set
FORECASTING_LIST_LM_Test_set <- lapply(1:length(FORECASTING_LIST_LM_Training_set),function(i) {tslm(Test_set[,i], model = FORECASTING_LIST_LM_Training_set[[i]])})
但是当我想将此代码应用于Test_set时出现问题。当我尝试运行此代码的最后一行时。
那么有人可以帮助我如何修复此行代码FORECASTING_LIST_LM_Test_set以便像之前的示例一样获得估算值吗?