Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fable from distribution to confidence interval

I manage to use fable for the forecast then get the result enter image description here

could I have some guidance on how to change this distribution to 80% 95% confidence interval? thank you!

you can use the sample code here to get the distribution

result <–USAccDeaths %>% as_tsibble %>% 
  model(arima = ARIMA(log(value) ~ pdq(0,1,1) + PDQ(0,1,1)))%>%
  forecast(h=12)
like image 456
CloverCeline Avatar asked Mar 20 '26 19:03

CloverCeline


1 Answers

The hilo() function allows you to extract confidence intervals from a forecast distribution. It can be used on either the distribution vector, or the fable itself.

library(tidyverse)
library(fable)
result <- as_tsibble(USAccDeaths) %>%
  model(arima = ARIMA(log(value) ~ pdq(0,1,1) + PDQ(0,1,1)))%>%
  forecast(h=12)

result %>% 
  mutate(`80%` = hilo(value, 80))
#> # A fable: 12 x 5 [1M]
#> # Key:     .model [1]
#>    .model    index             value  .mean                    `80%`
#>    <chr>     <mth>            <dist>  <dbl>                   <hilo>
#>  1 arima  1979 Jan   t(N(9, 0.0014))  8290. [ 7899.082,  8689.169]80
#>  2 arima  1979 Feb t(N(8.9, 0.0018))  7453. [ 7055.860,  7859.100]80
#>  3 arima  1979 Mar   t(N(9, 0.0022))  8276. [ 7789.719,  8774.054]80
#>  4 arima  1979 Apr t(N(9.1, 0.0025))  8584. [ 8036.304,  9144.752]80
#>  5 arima  1979 May t(N(9.2, 0.0029))  9499. [ 8849.860, 10166.302]80
#>  6 arima  1979 Jun t(N(9.2, 0.0033))  9900. [ 9180.375, 10639.833]80
#>  7 arima  1979 Jul t(N(9.3, 0.0037)) 10988. [10145.473, 11857.038]80
#>  8 arima  1979 Aug t(N(9.2, 0.0041)) 10132. [ 9315.840, 10974.140]80
#>  9 arima  1979 Sep t(N(9.1, 0.0045))  9138. [ 8368.585,  9933.124]80
#> 10 arima  1979 Oct t(N(9.1, 0.0049))  9391. [ 8567.874, 10243.615]80
#> 11 arima  1979 Nov t(N(9.1, 0.0052))  8863. [ 8056.754,  9699.824]80
#> 12 arima  1979 Dec t(N(9.1, 0.0056))  9356. [ 8474.732, 10271.739]80

result %>% 
  hilo(level = c(80, 95))
#> # A tsibble: 12 x 6 [1M]
#> # Key:       .model [1]
#>    .model    index             value  .mean                    `80%`
#>    <chr>     <mth>            <dist>  <dbl>                   <hilo>
#>  1 arima  1979 Jan   t(N(9, 0.0014))  8290. [ 7899.082,  8689.169]80
#>  2 arima  1979 Feb t(N(8.9, 0.0018))  7453. [ 7055.860,  7859.100]80
#>  3 arima  1979 Mar   t(N(9, 0.0022))  8276. [ 7789.719,  8774.054]80
#>  4 arima  1979 Apr t(N(9.1, 0.0025))  8584. [ 8036.304,  9144.752]80
#>  5 arima  1979 May t(N(9.2, 0.0029))  9499. [ 8849.860, 10166.302]80
#>  6 arima  1979 Jun t(N(9.2, 0.0033))  9900. [ 9180.375, 10639.833]80
#>  7 arima  1979 Jul t(N(9.3, 0.0037)) 10988. [10145.473, 11857.038]80
#>  8 arima  1979 Aug t(N(9.2, 0.0041)) 10132. [ 9315.840, 10974.140]80
#>  9 arima  1979 Sep t(N(9.1, 0.0045))  9138. [ 8368.585,  9933.124]80
#> 10 arima  1979 Oct t(N(9.1, 0.0049))  9391. [ 8567.874, 10243.615]80
#> 11 arima  1979 Nov t(N(9.1, 0.0052))  8863. [ 8056.754,  9699.824]80
#> 12 arima  1979 Dec t(N(9.1, 0.0056))  9356. [ 8474.732, 10271.739]80
#> # … with 1 more variable: `95%` <hilo>

To extract the numerical values from a <hilo> object, you can use the unpack_hilo() function, or obtain each part using <hilo>$lower, <hilo>$upper and <hilo>$level.

result %>% 
  hilo(level = c(80, 95)) %>% 
  unpack_hilo("80%")
#> # A tsibble: 12 x 7 [1M]
#> # Key:       .model [1]
#>    .model    index             value  .mean `80%_lower` `80%_upper`
#>    <chr>     <mth>            <dist>  <dbl>       <dbl>       <dbl>
#>  1 arima  1979 Jan   t(N(9, 0.0014))  8290.       7899.       8689.
#>  2 arima  1979 Feb t(N(8.9, 0.0018))  7453.       7056.       7859.
#>  3 arima  1979 Mar   t(N(9, 0.0022))  8276.       7790.       8774.
#>  4 arima  1979 Apr t(N(9.1, 0.0025))  8584.       8036.       9145.
#>  5 arima  1979 May t(N(9.2, 0.0029))  9499.       8850.      10166.
#>  6 arima  1979 Jun t(N(9.2, 0.0033))  9900.       9180.      10640.
#>  7 arima  1979 Jul t(N(9.3, 0.0037)) 10988.      10145.      11857.
#>  8 arima  1979 Aug t(N(9.2, 0.0041)) 10132.       9316.      10974.
#>  9 arima  1979 Sep t(N(9.1, 0.0045))  9138.       8369.       9933.
#> 10 arima  1979 Oct t(N(9.1, 0.0049))  9391.       8568.      10244.
#> 11 arima  1979 Nov t(N(9.1, 0.0052))  8863.       8057.       9700.
#> 12 arima  1979 Dec t(N(9.1, 0.0056))  9356.       8475.      10272.
#> # … with 1 more variable: `95%` <hilo>

Created on 2020-04-08 by the reprex package (v0.3.0)

like image 198
Mitchell O'Hara-Wild Avatar answered Mar 22 '26 11:03

Mitchell O'Hara-Wild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!