组合由预测函数生成的 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) 中的错误:不一致的数组
知道为什么会这样吗?