这是我正在处理的问题。
我从 R 中的时间序列模型获得了以下两个输出:
library(forecast)
library(dplyr)
set.seed(123)
dates <- seq(as.Date("2000-01-01"), by = "month", length.out = 100)
##################
values1 <- rnorm(100, mean = 100, sd = 100)
ts_data1 <- ts(values1, start = c(2000, 1), frequency = 12)
model1 <- auto.arima(ts_data1)
forecast_data1 <- forecast(model1, h = 12)
forecast_data1$class = 1
####################
values2 <- rnorm(100, mean = 200, sd = 100)
ts_data2 <- ts(values2, start = c(2000, 1), frequency = 12)
model2 <- auto.arima(ts_data2)
forecast_data2 <- forecast(model2, h = 12)
forecast_data2$class = 2
我正在尝试将这两个结合在一起,例如
rbind(data.frame(forecast_data1), data.frame(forecast_data2))
Point.Forecast Lo.80 Hi.80 Lo.95 Hi.95
May 2008 116.88772 2.03451679 231.7409 -58.765094 292.5405
Jun 2008 84.77524 -30.07795384 199.6284 -90.877564 260.4281
Jul 2008 88.15699 -26.69621184 203.0102 -87.495822 263.8098
Aug 2008 97.84920 -17.00400178 212.7024 -77.803612 273.5020
Sep 2008 104.59216 -10.26103639 219.4454 -71.060647 280.2450
Oct 2008 123.46321 8.61001633 238.3164 -52.189594 299.1160
Nov 2008 80.16232 -34.69087853 195.0155 -95.490489 255.8151
Dec 2008 122.86121 8.00801357 237.7144 -52.791597 298.5140
Jan 2009 62.16134 -52.69186110 177.0145 -113.491472 237.8141
Feb 2009 76.41793 -38.43527070 191.2711 -99.234881 252.0707
Mar 2009 114.92293 0.06973034 229.7761 -60.729880 290.5757
Apr 2009 132.14088 17.28768324 246.9941 -43.511927 307.7937
May 20081 173.02664 50.54760673 295.5057 -14.288876 360.3422
Jun 20081 196.17783 73.69880321 318.6569 8.862321 383.4933
Jul 20081 181.38489 58.90585584 303.8639 -5.930626 368.7004
Aug 20081 196.21547 73.73644398 318.6945 8.899962 383.5310
Sep 20081 189.46162 66.98258965 311.9407 2.146107 376.7771
Oct 20081 200.14378 77.66475146 322.6228 12.828269 387.4593
Nov 20081 214.71057 92.23154347 337.1896 27.395061 402.0261
Dec 20081 151.90221 29.42317702 274.3812 -35.413305 339.2177
Jan 20091 176.64285 54.16381860 299.1219 -10.672664 363.9584
Feb 20091 211.48637 89.00734228 333.9654 24.170860 398.8019
Mar 20091 196.08048 73.60144798 318.5595 8.764966 383.3960
Apr 20091 210.85820 88.37916960 333.3372 23.542687 398.1737
但由于某种原因,类变量(即 1 或 2)没有显示出来以表明预测来自哪个模型。
我该如何修复此问题?
当您将预测对象转换为数据框时,
class
插槽会丢失。您应该class
在转换后添加插槽。例如forecast
在添加列之前将对象转换为 data.frameclass
,例如:然后第二个也一样:
创建于 2024-10-29,使用reprex v2.1.0