0

组合由预测函数生成的 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) 中的错误:不一致的数组

知道为什么会这样吗?

4

1 回答 1

0

您正在尝试将 2 个向量转换为 2 个不同的矩阵,然后添加 - 这是抛出错误,因为两个向量的长度不同 - 所以矩阵将具有不同的维度。您想创建一个 2 列数据框吗?干得好 -

Y <- cbind(pred.hw.add , pred.arima.boxcox)
于 2018-06-28T11:33:11.003 回答