首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R寓言:如何访问分发对象元素

R寓言:如何访问分发对象元素
EN

Stack Overflow用户
提问于 2022-10-06 11:51:44
回答 1查看 55关注 0票数 1

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

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

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

有办法这样做吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-06 14:19:19

您可以为此使用包分发。

代码语言:javascript
复制
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使用它,并将列添加到预测中。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73973322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档