在组合由预测函数生成的2个时间序列对象时出现错误。
这是源代码
# For Time Series Analysis
library(timeSeries)
library(forecast)
library(fpp)
library(tseries)
library(TSA)
# For manipulating data
library(magrittr)
library(forcats)
library(dplyr)
library(tidyr)
library(readr)
library(lubridate)
AP <-AirPassengers
splitTrainXvat <- function(tser, perc_train){
ntrain <- floor(length(as.vector(tser)) * perc_train)
nval <- length(as.vector(tser)) - ntrain
ttrain <- ts(as.vector(tser[1:ntrain]), start = start(tser), frequency = frequency(tser))
tval <- ts(as.vector(tser[ntrain + 1:nval]), start = end(ttrain) + deltat(tser),
frequency = frequency(tser))
stopifnot(length(ttrain) == ntrain)
stopifnot(length(tval) == nval)
list(ttrain, tval)
}
ts_all <- AP
data <- splitTrainXvat(ts_all, 0.95)
data
ts_train <- data[[1]]
ts_val <- data[[2]]
# preparing model with training set
mod.hw.add <- HoltWinters(ts_train, seasonal = "add")
mod.arima <- auto.arima(ts_train, max.p = 2, max.q = 2, max.d = 2,
max.P = 2, max.Q = 2, max.D = 2, allowdrift = T,
stepwise = F, approximation = F)
mod.arima.boxcox <- auto.arima(ts_train, max.p=2, max.q=2,
max.P=2, max.Q=2, max.d=2, max.D=2, allowdrift = T,
stepwise = F, approximation = F, lambda = 0)
# Preparing model with testing set and forecasting
pred.hw.add <- forecast(mod.hw.add, h = length(ts_val)+8)$mean
pred.arima <- forecast(mod.arima, h = (length(ts_val)+8))$mean
pred.arima.boxcox <- forecast(mod.arima.boxcox, h = length(ts_val)+4)$mean
# Combining the two forecasts
Y <- (as.matrix(pred.hw.add) + as.matrix(pred.arima.boxcox))
执行此操作时生成的错误是
Y <- (as.matrix(pred.hw.add) + as.matrix(pred.arima.boxcox))
as.matrix(pred.hw.add)+ as.matrix(pred.arima.boxcox)中的错误: 不整合数组
知道为什么会发生吗?
答案 0 :(得分:0)
您正在尝试将2个向量转换为2个不同的矩阵,然后添加-由于两个向量的长度不同,因此会引发错误-因此矩阵将具有不同的尺寸。是否要创建2列数据框?在这里-
Y <- cbind(pred.hw.add , pred.arima.boxcox)