R - 几个非重叠时间序列的总和(预测)

R - sum of several non-overlapping time series (forecasts)

我有 24 个时间序列预测,我想要它们的累计总和。这里只有两个,因为它们会产生我遇到的错误:

forecast(arima(P1, c(0,0,0)), h = 6)$mean
      Jan      Feb      Mar      Apr      May Jun Jul Aug Sep Oct Nov      Dec    
2012                                                                     12.81818                                                     
2013 12.81818 12.81818 12.81818 12.81818 12.81818 

从 2012 年 12 月到 2013 年 5 月。

forecast(arima(P2, c(0,0,0)), h = 6)$mean
      Jan      Feb      Mar      Apr      May      Jun
2014 12.94118 12.94118 12.94118 12.94118 12.94118 12.94118

从 2014 年 1 月到 2014 年 6 月。

我尝试使用全为 0 的虚拟时间序列来添加它们以保存中间结果,但是当我将第一个添加到虚拟时间序列时,它在它的末尾被切断(对于 P1,即 May 2013),然后尝试添加第二个 returns 时间序列不相交的错误消息。

有什么办法吗?我希望这样的时间序列是我的结果:

          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug          Sep      Oct      Nov      Dec
2011  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000      0.00000  0.00000  0.00000  0.00000
2012  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000      0.00000  0.00000  0.00000 12.94118
2013 12.81818 12.81818 12.81818 12.81818 12.81818  0.00000  0.00000  0.00000      0.00000  0.00000  0.00000  0.00000
2014 12.94118 12.94118 12.94118 12.94118 12.94118 12.94118 

有些要添加的时间序列有重叠,有些则没有。

我知道 R 有用于分层时间序列预测的 hts 包,但出于说明目的,我需要使用 arima(0,0,0) 模型完成预测...

干杯!

好的,这就是答案。可能有更好的方法,但这行得通:

#create dummy time series ts0
ts0 <- ts(rep(0,42), start = c(2011, 1), frequency = 12)

#merge alle forecasts with ts0 using ts.union
ts.union(ts0, forecast(arima(P1, c(0,0,0)), h = 6)$mean, forecast(arima(P2, c(0,0,0)), h = 6)$mean, ..., forecast(arima(P24, c(0,0,0)), h = 6)$mean)

#get row sums using rowSums
ts0sums <- rowSums(ts0)

#make into time series again
ts1 <- ts(ts0sums, start = c(2011, 1), frequency = 12)

#change 0's to NAs for plotting with "holes"
ts1[ts1 == 0] <- NA

#ts1
            Jan        Feb        Mar        Apr        May        Jun            Jul        Aug        Sep        Oct        Nov        Dec
2011         NA         NA         NA         NA         NA         NA             NA         NA         NA         NA         NA   9.000000
2012   9.000000   9.000000   9.000000   9.000000   9.000000         NA             NA         NA  13.428571  13.428571  13.428571  76.774109
2013  78.631252  78.631252  65.202681  65.202681  65.202681   1.857143             NA         NA         NA         NA         NA  18.411765
2014 430.440131 430.440131 430.440131 430.440131 430.440131 412.028367