仅从 forecast() 中提取预测值
Extract only the forecasted values from forecast()
我有一个如下所示的数据框:
> head(forecasts)
$`1_1`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.370299 7.335176 7.405422 7.316583 7.424015
$`1_10`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.396656 7.359845 7.433467 7.340359 7.452953
$`1_2`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.780033 7.752462 7.807605 7.737866 7.822201
$`1_3`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.216894 7.178896 7.254892 7.158781 7.275007
$`1_4`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.501195 7.465049 7.537341 7.445915 7.556475
$`1_5`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.455131 7.424918 7.485345 7.408924 7.501339
我只想提取 Point Forecast
调用 str(forecasts)
returns 很多输出,这只是 'forecasts' 变量中 89 个列表之一的输出:
$ 9_9 :List of 10
..$ method : chr "ARIMA(0,0,0)(0,1,0)[12] with drift"
..$ model :List of 19
.. ..$ coef : Named num 0.00965
.. .. ..- attr(*, "names")= chr "drift"
.. ..$ sigma2 : num 0.0047
.. ..$ var.coef : num [1, 1] 1.24e-06
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr "drift"
.. .. .. ..$ : chr "drift"
.. ..$ mask : logi TRUE
.. ..$ loglik : num 33.4
.. ..$ aic : num -62.7
.. ..$ arma : int [1:7] 0 0 0 0 12 0 1
.. ..$ residuals: Time-Series [1:38] from 2014 to 2017: 0.00546 0.00583 0.006 0.00564 0.00563 ...
.. ..$ call : language .f(y = .x[[i]], x = list(x = c(5.4677292870219, 5.85045765518954, 6.02852764863892, 5.67941181324485, 5.67526620| __truncated__ ...
.. ..$ series : chr ".x[[i]]"
.. ..$ code : int 0
.. ..$ n.cond : int 0
.. ..$ nobs : int 26
.. ..$ model :List of 10
.. .. ..$ phi : num(0)
.. .. ..$ theta: num(0)
.. .. ..$ Delta: num [1:12] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ Z : num [1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ a : num [1:13] 0.0677 5.6916 5.7073 5.692 5.7108 ...
.. .. ..$ P : num [1:13, 1:13] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ T : num [1:13, 1:13] 0 1 0 0 0 0 0 0 0 0 ...
.. .. ..$ V : num [1:13, 1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ h : num 0
.. .. ..$ Pn : num [1:13, 1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. ..$ xreg : int [1:38, 1] 1 2 3 4 5 6 7 8 9 10 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr "drift"
.. ..$ bic : num -60.2
.. ..$ aicc : num -62.2
.. ..$ x : Time-Series [1:38] from 2014 to 2017: 5.47 5.85 6.03 5.68 5.68 ...
.. ..$ fitted : Time-Series [1:38] from 2014 to 2017: 5.46 5.84 6.02 5.67 5.67 ...
.. ..- attr(*, "class")= chr [1:2] "ARIMA" "Arima"
..$ level : num [1:2] 80 95
..$ mean : Time-Series [1:1] from 2017 to 2017: 6.32
..$ lower : Time-Series [1, 1:2] from 2017 to 2017: 6.23 6.18
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "80%" "95%"
..$ upper : Time-Series [1, 1:2] from 2017 to 2017: 6.4 6.45
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "80%" "95%"
..$ x : Time-Series [1:38] from 2014 to 2017: 5.47 5.85 6.03 5.68 5.68 ...
..$ series : chr ".x[[i]]"
..$ fitted : Time-Series [1:38] from 2014 to 2017: 5.46 5.84 6.02 5.67 5.67 ...
..$ residuals: Time-Series [1:38] from 2014 to 2017: 0.00546 0.00583 0.006 0.00564 0.00563 ...
..- attr(*, "class")= chr "forecast"
我们可以使用 lapply
从 list
中提取值
lapply(forecasts, function(x) as.numeric(x$mean, na.rm = TRUE)))
如果所有列表元素中的预测数量相同,则可以将其转换为matrix
或data.frame
sapply(forecasts, `[[`, "mean")
或使用tidyverse
library(tidyverse)
forecasts %>%
map_df(~ .x$mean %>%
as.numeric)
我有一个如下所示的数据框:
> head(forecasts)
$`1_1`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.370299 7.335176 7.405422 7.316583 7.424015
$`1_10`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.396656 7.359845 7.433467 7.340359 7.452953
$`1_2`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.780033 7.752462 7.807605 7.737866 7.822201
$`1_3`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.216894 7.178896 7.254892 7.158781 7.275007
$`1_4`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.501195 7.465049 7.537341 7.445915 7.556475
$`1_5`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.455131 7.424918 7.485345 7.408924 7.501339
我只想提取 Point Forecast
调用 str(forecasts)
returns 很多输出,这只是 'forecasts' 变量中 89 个列表之一的输出:
$ 9_9 :List of 10
..$ method : chr "ARIMA(0,0,0)(0,1,0)[12] with drift"
..$ model :List of 19
.. ..$ coef : Named num 0.00965
.. .. ..- attr(*, "names")= chr "drift"
.. ..$ sigma2 : num 0.0047
.. ..$ var.coef : num [1, 1] 1.24e-06
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr "drift"
.. .. .. ..$ : chr "drift"
.. ..$ mask : logi TRUE
.. ..$ loglik : num 33.4
.. ..$ aic : num -62.7
.. ..$ arma : int [1:7] 0 0 0 0 12 0 1
.. ..$ residuals: Time-Series [1:38] from 2014 to 2017: 0.00546 0.00583 0.006 0.00564 0.00563 ...
.. ..$ call : language .f(y = .x[[i]], x = list(x = c(5.4677292870219, 5.85045765518954, 6.02852764863892, 5.67941181324485, 5.67526620| __truncated__ ...
.. ..$ series : chr ".x[[i]]"
.. ..$ code : int 0
.. ..$ n.cond : int 0
.. ..$ nobs : int 26
.. ..$ model :List of 10
.. .. ..$ phi : num(0)
.. .. ..$ theta: num(0)
.. .. ..$ Delta: num [1:12] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ Z : num [1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ a : num [1:13] 0.0677 5.6916 5.7073 5.692 5.7108 ...
.. .. ..$ P : num [1:13, 1:13] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ T : num [1:13, 1:13] 0 1 0 0 0 0 0 0 0 0 ...
.. .. ..$ V : num [1:13, 1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ h : num 0
.. .. ..$ Pn : num [1:13, 1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. ..$ xreg : int [1:38, 1] 1 2 3 4 5 6 7 8 9 10 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr "drift"
.. ..$ bic : num -60.2
.. ..$ aicc : num -62.2
.. ..$ x : Time-Series [1:38] from 2014 to 2017: 5.47 5.85 6.03 5.68 5.68 ...
.. ..$ fitted : Time-Series [1:38] from 2014 to 2017: 5.46 5.84 6.02 5.67 5.67 ...
.. ..- attr(*, "class")= chr [1:2] "ARIMA" "Arima"
..$ level : num [1:2] 80 95
..$ mean : Time-Series [1:1] from 2017 to 2017: 6.32
..$ lower : Time-Series [1, 1:2] from 2017 to 2017: 6.23 6.18
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "80%" "95%"
..$ upper : Time-Series [1, 1:2] from 2017 to 2017: 6.4 6.45
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "80%" "95%"
..$ x : Time-Series [1:38] from 2014 to 2017: 5.47 5.85 6.03 5.68 5.68 ...
..$ series : chr ".x[[i]]"
..$ fitted : Time-Series [1:38] from 2014 to 2017: 5.46 5.84 6.02 5.67 5.67 ...
..$ residuals: Time-Series [1:38] from 2014 to 2017: 0.00546 0.00583 0.006 0.00564 0.00563 ...
..- attr(*, "class")= chr "forecast"
我们可以使用 lapply
list
中提取值
lapply(forecasts, function(x) as.numeric(x$mean, na.rm = TRUE)))
如果所有列表元素中的预测数量相同,则可以将其转换为matrix
或data.frame
sapply(forecasts, `[[`, "mean")
或使用tidyverse
library(tidyverse)
forecasts %>%
map_df(~ .x$mean %>%
as.numeric)