我有一些代码可以借鉴R中预测库中的auto.arima函数,并且可以正常工作。
library(forecast)
# read data
proj_path = "P:/R"
Macro <- read.csv("P:/Earnest/Old/R/InputFull.csv")
output <- list()
len <- nrow(Macro)
for(z in rev(1:6)) {
x <- Macro[1:(len-z),5:5]
x <- ts((x), start = c(2011, 4), frequency = 4)
y <- Macro[1:(len-z),2:2]
y <- ts((y), start = c(2011, 4), frequency = 4)
t <- Macro[(len+1-z):(len+1-z),5:5]
t <- ts((t), start = c(2019, 2), frequency = 4)
#fit model
fit <-auto.arima(y,xreg=x,seasonal=TRUE)
fit
output[[z]] <- forecast(fit,xreg=t)
}
我的问题是调用output
时(请参阅下文)。理想情况下,我只想提取“点数预测”值。当我输出(输出)时-参见下文-我听到了很多我并不特别感兴趣的噪音,我猜是auto.arima的输出。仅提取Point Forecast有帮助吗?
[[1]]
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2019 Q3 953.5374 930.5549 976.5199 918.3887 988.6861
[[2]]
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2019 Q2 967.6486 942.6151 992.6821 929.3632 1005.934
[[3]]
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2019 Q1 839.4326 813.0172 865.8481 799.0337 879.8316
[[4]]
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2018 Q4 1065.612 1039.34 1091.884 1025.433 1105.791
[[5]]
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2018 Q3 888.7737 861.9055 915.6419 847.6823 929.8651
[[6]]
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2018 Q2 876.959 849.4451 904.473 834.8801 919.038
每个评论中,我包括输出[[1]]-不幸的是,这仅返回第一个预测:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2019 Q3 953.5374 930.5549 976.5199 918.3887 988.6861
让dput尝试:
dput(output[[1]])
structure(list(method = "Regression with ARIMA(3,0,0)(0,1,0)[4] errors",
model = structure(list(coef = c(ar1 = 0.344388033807951,
ar2 = 0.47045405649059, ar3 = -0.372353082067685, drift = 4.6170060691926,
xreg = 156.483453676652), sigma2 = 321.603998685109, var.coef = structure(c(0.0309368420597048,
-0.0067604404772564, -0.0114273841518184, -0.00765269741973596,
0.31298209820046, -0.0067604404772564, 0.0324039295478909,
-0.0102797363340369, -0.0506064432119486, 0.925918002517119,
-0.0114273841518184, -0.0102797363340368, 0.0347678170710133,
0.0679537317102984, -0.858293796397855, -0.00765269741973596,
-0.0506064432119486, 0.0679537317102984, 3.76612081659896,
-31.7616552609842, 0.31298209820046, 0.925918002517119, -0.858293796397855,
-31.7616552609842, 551.415541351487), .Dim = c(5L, 5L), .Dimnames = list(
c("ar1", "ar2", "ar3", "drift", "xreg"), c("ar1", "ar2",
"ar3", "drift", "xreg"))), mask = c(TRUE, TRUE, TRUE,
TRUE, TRUE), loglik = -113.937105368339, aic = 239.874210736678,
arma = c(3L, 0L, 0L, 0L, 4L, 0L, 1L), residuals = structure(c(0.417031652697368,
0.362938085616478, 0.379239503857839, 0.410930375294051,
12.7724173992322, -25.6234869707515, 1.02702692377506,
-9.64209472954486, 11.5796225208562, 4.61901034117763,
-8.92694799644059, 28.5644217958362, 2.07860701481547,
-11.9341928827785, 22.4326382789151, -10.9283721852124,
19.5278764536355, -7.43202365481068, -40.2650002316025,
-21.9019776478344, -18.4813717021279, 9.92148855978593,
-10.8829746290488, 17.0416890149748, 6.80627461017923,
-17.9109071037545, 5.46600608260712, 5.91334435343634,
20.1404704527817, 0.687340748186216, 10.7718395995369
), .Tsp = c(2011.75, 2019.25, 4), class = "ts"), call = auto.arima(y = y,
seasonal = TRUE, xreg = structure(c(1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1.576786643,
0.970367212, 1.380910896, 1.153820861, 1.755251012,
1.298564786, 1.675546579, 1.734566088, 2.360872229,
1.71432468, 2.047891667, 1.984148637, 2.46407588,
1.826639578, 2.13179517, 2.175771867, 2.852571892,
2.110268848, 2.55556188, 2.421748178, 3.031113582,
2.247203602, 2.713249789, 2.421141924, 2.95679417,
2.10855795, 2.458458681, 2.468671075, 3.070054762,
2.535768803, 2.957307786), .Dim = c(31L, 2L), .Dimnames = list(
NULL, c("drift", "xreg")), .Tsp = c(2011.75,
2019.25, 4), class = c("mts", "ts", "matrix")), x = structure(list(
x = structure(c(668.39, 524.019, 609.181, 609.953,
730.648, 568.93, 676.269, 692.894, 856.832, 648.177,
758.524, 774.049, 905.858, 686.31, 811.253, 814.47,
1011.044, 739.01, 867.46, 825.258, 1013.406,
762.577, 890.568, 862.491, 1030.2, 761.2, 872.93,
892.77, 1089.12, 855.69, 992.454), .Tsp = c(2011.75,
2019.25, 4), class = "ts")), class = "data.frame", row.names = c(NA,
-31L))), series = "y", code = 0L, n.cond = 0L, nobs = 27L,
model = list(phi = c(0.344388033807951, 0.47045405649059,
-0.372353082067685), theta = c(0, 0), Delta = c(0, 0,
0, 1), Z = c(1, 0, 0, 0, 0, 0, 1), a = c(22.994344909323,
-4.14873145388895, -3.41468106646512, 320.373957905273,
474.814051859203, 377.187654254954, 363.562731007573),
P = structure(c(0, -0.00000000000000000436177749699274,
0.00000000000000000404307017557023, -0.0000000000000000108581622397725,
-0.00000000000000000200478795716124, -0.00000000000000000148990964033995,
0.0000000000000000229762775720554, -0.00000000000000000436178192960162,
-0.0000000000000000223111786692371, 0.00000000000000000882938329769209,
0.00000000000000000501982339400321, 0.0000000000000000326509077057451,
0.0000000000000000166443833211233, 0.00000000000000000434082364118481,
0.00000000000000000404307603382513, 0.000000000000000008829378546185,
-0.000000000000000000000000000000000530498471379299,
-0.0000000000000000040196930292904, -0.0000000000000000237123820679965,
0.000000000000000021038875301895, -0.00000000000000000403285498933536,
-0.000000000000000010960435057365, 0.00000000000000000513624867377796,
-0.00000000000000000406521741615706, 0.0000000000000000109176413784004,
-0.000000000000000000000000000000000303309385794285,
0.00000000000000000000000000000000112569713072961,
0.000000000000000000000000000000000277245367023131,
-0.00000000000000000209500758560436, 0.000000000000000032758390362318,
-0.000000000000000023757723671398, 0.0000000000000000638042890352103,
-0.00000000000000000736237699170413, 0.00000000000000000000000000000000299132815068903,
0.000000000000000000000000000000000712790153614343,
-0.00000000000000000159885091144409, 0.0000000000000000167687290601845,
0.0000000000000000209914682136184, -0.0000000000000000563751697637422,
-0.0000000000000001162623822542, 0.000000000000000010635149410813,
0.000000000000000000000000000000000824725300499317,
0.000000000000000022961977899447, 0.00000000000000000436177749699274,
-0.00000000000000000404307017557023, 0.0000000000000000108581622397725,
0.00000000000000000200478795716124, 0.00000000000000000148990964033995,
-0.0000000000000000229762775720554), .Dim = c(7L,
7L)), T = structure(c(0.344388033807951, 0.47045405649059,
-0.372353082067685, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 0), .Dim = c(7L, 7L)), V = structure(c(1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(7L,
7L)), h = 0, Pn = structure(c(1, -0.000000000000000000269186010724848,
-0.0000000000000000189894822530969, 0.00000000000000000386217786572014,
0.0000000000000000185999931566332, -0.000000000000000077962132609535,
0.00000000000000000414210899739849, -0.000000000000000000269190443333734,
-0.0000000000000000223111786692371, 0.00000000000000000882938329769209,
0.00000000000000000501982339400321, 0.0000000000000000326509077057451,
0.0000000000000000166443833211233, 0.00000000000000000434082364118481,
-0.000000000000000018989476394842, 0.000000000000000008829378546185,
0, -0.0000000000000000040196930292904, -0.0000000000000000237123820679965,
0.000000000000000021038875301895, -0.00000000000000000403285498933536,
0.00000000000000000375990504812762, 0.00000000000000000513624867377796,
-0.00000000000000000406521741615706, 0.0000000000000000109176413784004,
0, 0, 0, 0.0000000000000000185097735281901, 0.000000000000000032758390362318,
-0.000000000000000023757723671398, 0.0000000000000000638042890352103,
-0.00000000000000000736237699170413, 0.00000000000000000000000000000000141563473512348,
0.000000000000000000000000000000000324716232673231,
-0.0000000000000000780710738806391, 0.0000000000000000167687290601845,
0.0000000000000000209914682136184, -0.0000000000000000563751697637422,
-0.0000000000000001162623822542, 0.000000000000000010635149410813,
0.00000000000000000000000000000000226501603917989,
0.00000000000000000412780932479003, 0.00000000000000000436177749699274,
-0.00000000000000000404307017557023, 0.0000000000000000108581622397725,
0.00000000000000000200478795716124, 0.00000000000000000148990964033995,
-0.0000000000000000229762775720554), .Dim = c(7L,
7L))), xreg = structure(c(1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 1.576786643, 0.970367212,
1.380910896, 1.153820861, 1.755251012, 1.298564786, 1.675546579,
1.734566088, 2.360872229, 1.71432468, 2.047891667, 1.984148637,
2.46407588, 1.826639578, 2.13179517, 2.175771867, 2.852571892,
2.110268848, 2.55556188, 2.421748178, 3.031113582, 2.247203602,
2.713249789, 2.421141924, 2.95679417, 2.10855795, 2.458458681,
2.468671075, 3.070054762, 2.535768803, 2.957307786), .Dim = c(31L,
2L), .Dimnames = list(NULL, c("drift", "xreg")), .Tsp = c(2011.75,
2019.25, 4), class = c("mts", "ts", "matrix")), bic = 247.649231932704,
aicc = 244.074210736678, x = structure(c(668.39, 524.019,
609.181, 609.953, 730.648, 568.93, 676.269, 692.894,
856.832, 648.177, 758.524, 774.049, 905.858, 686.31,
811.253, 814.47, 1011.044, 739.01, 867.46, 825.258, 1013.406,
762.577, 890.568, 862.491, 1030.2, 761.2, 872.93, 892.77,
1089.12, 855.69, 992.454), .Tsp = c(2011.75, 2019.25,
4), class = "ts"), fitted = structure(c(667.972968347303,
523.656061914383, 608.801760496142, 609.542069624706,
717.875582600768, 594.553486970751, 675.241973076225,
702.536094729545, 845.252377479144, 643.557989658822,
767.450947996441, 745.484578204164, 903.779392985184,
698.244192882778, 788.820361721085, 825.398372185212,
991.516123546364, 746.442023654811, 907.725000231603,
847.159977647834, 1031.88737170213, 752.655511440214,
901.450974629049, 845.449310985025, 1023.39372538982,
779.110907103755, 867.463993917393, 886.856655646564,
1068.97952954722, 855.002659251814, 981.682160400463), .Tsp = c(2011.75,
2019.25, 4), class = "ts")), class = c("ARIMA", "Arima"
)), level = c(80, 95), mean = structure(953.537403030249, .Tsp = c(2019.5,
2019.5, 4), class = "ts"), lower = structure(c(930.554927548911,
918.388739933064), .Dim = 1:2, .Dimnames = list(NULL, c("80%",
"95%")), .Tsp = c(2019.5, 2019.5, 4), class = c("mts", "ts",
"matrix")), upper = structure(c(976.519878511587, 988.686066127434
), .Dim = 1:2, .Dimnames = list(NULL, c("80%", "95%")), .Tsp = c(2019.5,
2019.5, 4), class = c("mts", "ts", "matrix")), x = structure(c(668.39,
524.019, 609.181, 609.953, 730.648, 568.93, 676.269, 692.894,
856.832, 648.177, 758.524, 774.049, 905.858, 686.31, 811.253,
814.47, 1011.044, 739.01, 867.46, 825.258, 1013.406, 762.577,
890.568, 862.491, 1030.2, 761.2, 872.93, 892.77, 1089.12,
855.69, 992.454), .Tsp = c(2011.75, 2019.25, 4), class = "ts"),
series = "y", fitted = structure(c(667.972968347303, 523.656061914383,
608.801760496142, 609.542069624706, 717.875582600768, 594.553486970751,
675.241973076225, 702.536094729545, 845.252377479144, 643.557989658822,
767.450947996441, 745.484578204164, 903.779392985184, 698.244192882778,
788.820361721085, 825.398372185212, 991.516123546364, 746.442023654811,
907.725000231603, 847.159977647834, 1031.88737170213, 752.655511440214,
901.450974629049, 845.449310985025, 1023.39372538982, 779.110907103755,
867.463993917393, 886.856655646564, 1068.97952954722, 855.002659251814,
981.682160400463), .Tsp = c(2011.75, 2019.25, 4), class = "ts"),
residuals = structure(c(0.417031652697368, 0.362938085616478,
0.379239503857839, 0.410930375294051, 12.7724173992322, -25.6234869707515,
1.02702692377506, -9.64209472954486, 11.5796225208562, 4.61901034117763,
-8.92694799644059, 28.5644217958362, 2.07860701481547, -11.9341928827785,
22.4326382789151, -10.9283721852124, 19.5278764536355, -7.43202365481068,
-40.2650002316025, -21.9019776478344, -18.4813717021279,
9.92148855978593, -10.8829746290488, 17.0416890149748, 6.80627461017923,
-17.9109071037545, 5.46600608260712, 5.91334435343634, 20.1404704527817,
0.687340748186216, 10.7718395995369), .Tsp = c(2011.75, 2019.25,
4), class = "ts")), class = "forecast")