I am using the fable package for time series analysis.
With the code below, I can produce a forecast in the form of an object of class fable (my_forecast).
I am looking for a way to access the elements in the 3rd column of this table. For example, in the first row, I would like to be able to access 0.58 and 0.41.
Is there a way to do this?
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
CodePudding user response:
You can use the package distributional for this.
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
Or using it via purrr and add the columns to the forecast
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
CodePudding user response:
suppressPackageStartupMessages({
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")
# Access elements in "Consumption" for the first row
my_forecast$Consumption$`1`$mu
#> [1] 0.5800046
my_forecast$Consumption$`1`$sigma
#> [1] 0.638976
# Same for second row
my_forecast$Consumption$`2`$mu
#> [1] 0.5783716
my_forecast$Consumption$`2`$sigma
#> [1] 0.6390728
Created on 2022-10-06 with reprex v2.0.2