R Fable:如何访问分发对象元素



我正在使用寓言程序包进行时间序列分析。

使用下面的代码,我可以生成一个类对象寓言形式的预测(my_forecast(。

我正在寻找一种访问此表第3列中元素的方法。例如,在第一行中,我希望能够访问0.58和0.41。

有办法做到这一点吗?

library(fable)
library(fabletools)
library(feasts)
library(tsibble)
library(dplyr)
library(lubridate)
library(fpp3)
fit <- us_change %>%
model(trend_model = TSLM(Consumption ~ trend()))
my_forecast <- fit %>%
forecast(h = "12 years")
my_forecast

您可以为此使用distributional包。

library(fpp3)
fit <- us_change %>%
model(trend_model = TSLM(Consumption ~ trend()))
my_forecast <- fit %>%
forecast(h = "12 years")
# Use package distributional
distributional::parameters(my_forecast$Consumption)
mu     sigma
1  0.5800046 0.6389760
2  0.5783716 0.6390728
3  0.5767387 0.6391705
4  0.5751058 0.6392693
5  0.5734728 0.6393689
6  0.5718399 0.6394695
7  0.5702069 0.6395711
8  0.5685740 0.6396736
9  0.5669411 0.6397771
10 0.5653081 0.6398815

或者通过purrr使用它,并将列添加到预测中

my_forecast %>% 
mutate(purrr::map_dfr(Consumption, distributional::parameters))
# A fable: 48 x 6 [1Q]
# Key:     .model [1]
.model      Quarter   Consumption .mean    mu sigma
<chr>         <qtr>        <dist> <dbl> <dbl> <dbl>
1 trend_model 2019 Q3 N(0.58, 0.41) 0.580 0.580 0.639
2 trend_model 2019 Q4 N(0.58, 0.41) 0.578 0.578 0.639
3 trend_model 2020 Q1 N(0.58, 0.41) 0.577 0.577 0.639
4 trend_model 2020 Q2 N(0.58, 0.41) 0.575 0.575 0.639
5 trend_model 2020 Q3 N(0.57, 0.41) 0.573 0.573 0.639
6 trend_model 2020 Q4 N(0.57, 0.41) 0.572 0.572 0.639
7 trend_model 2021 Q1 N(0.57, 0.41) 0.570 0.570 0.640
8 trend_model 2021 Q2 N(0.57, 0.41) 0.569 0.569 0.640
9 trend_model 2021 Q3 N(0.57, 0.41) 0.567 0.567 0.640
10 trend_model 2021 Q4 N(0.57, 0.41) 0.565 0.565 0.640
# … with 38 more rows
# ℹ Use `print(n = ...)` to see more rows

最新更新