Principles of forecasting: How to obtain the boostraped samples #95
nickcox896
started this conversation in
General
Replies: 1 comment 1 reply
-
library(fpp3)
#> ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.3 ──
#> ✔ tibble 3.3.1 ✔ tsibble 1.2.0.9000
#> ✔ dplyr 1.2.0 ✔ tsibbledata 0.4.1
#> ✔ tidyr 1.3.2 ✔ ggtime 0.2.0.9000
#> ✔ lubridate 1.9.5 ✔ feasts 0.5.0
#> ✔ ggplot2 4.0.2 ✔ fable 0.5.0
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> ✖ lubridate::date() masks base::date()
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ tsibble::intersect() masks base::intersect()
#> ✖ tsibble::interval() masks lubridate::interval()
#> ✖ dplyr::lag() masks stats::lag()
#> ✖ tsibble::setdiff() masks base::setdiff()
#> ✖ tsibble::union() masks base::union()
fit <- gafa_stock |>
filter(Symbol == "GOOG") |>
mutate(t = seq_along(Date)) |>
as_tsibble(index = t, regular = TRUE) |>
model(NAIVE(Close))The simplest way is to instead use fc_sim <- fit |>
generate(h = 30, bootstrap = TRUE)
fc_sim
#> # A tsibble: 30 x 5 [1]
#> # Key: Symbol, .model, .rep [1]
#> Symbol .model t .rep .sim
#> <chr> <chr> <dbl> <chr> <dbl>
#> 1 GOOG NAIVE(Close) 1259 1 1051.
#> 2 GOOG NAIVE(Close) 1260 1 1041.
#> 3 GOOG NAIVE(Close) 1261 1 1029.
#> 4 GOOG NAIVE(Close) 1262 1 1027.
#> 5 GOOG NAIVE(Close) 1263 1 1032.
#> 6 GOOG NAIVE(Close) 1264 1 1029.
#> 7 GOOG NAIVE(Close) 1265 1 1028.
#> 8 GOOG NAIVE(Close) 1266 1 1036.
#> 9 GOOG NAIVE(Close) 1267 1 1026.
#> 10 GOOG NAIVE(Close) 1268 1 1042.
#> # ℹ 20 more rowsAlternatively, you can use fc <- fit |>
forecast(h = 30, bootstrap = TRUE)
fc
#> # A fable: 30 x 5 [1]
#> # Key: Symbol, .model [1]
#> Symbol .model t Close .mean
#> <chr> <chr> <dbl> <dist> <dbl>
#> 1 GOOG NAIVE(Close) 1259 sample[5000] 1036.
#> 2 GOOG NAIVE(Close) 1260 sample[5000] 1036.
#> 3 GOOG NAIVE(Close) 1261 sample[5000] 1036.
#> 4 GOOG NAIVE(Close) 1262 sample[5000] 1036.
#> 5 GOOG NAIVE(Close) 1263 sample[5000] 1036.
#> 6 GOOG NAIVE(Close) 1264 sample[5000] 1036.
#> 7 GOOG NAIVE(Close) 1265 sample[5000] 1036.
#> 8 GOOG NAIVE(Close) 1266 sample[5000] 1036.
#> 9 GOOG NAIVE(Close) 1267 sample[5000] 1036.
#> 10 GOOG NAIVE(Close) 1268 sample[5000] 1036.
#> # ℹ 20 more rows
fc |>
as_tibble() |>
mutate(
# Extract the samples from the distribution
distributional::parameters(Close),
# Add an identifier for each replicate
.rep = list(seq_len(5000L))
) |>
# Unnest the list columns into a flat tibble
unnest(c(x, .rep))
#> # A tibble: 150,000 × 7
#> Symbol .model t Close .mean x .rep
#> <chr> <chr> <dbl> <dist> <dbl> <dbl> <int>
#> 1 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1043. 1
#> 2 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1044. 2
#> 3 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1035. 3
#> 4 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1041. 4
#> 5 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1035. 5
#> 6 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1048. 6
#> 7 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1045. 7
#> 8 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1037. 8
#> 9 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1023. 9
#> 10 GOOG NAIVE(Close) 1259 sample[5000] 1036. 1032. 10
#> # ℹ 149,990 more rowsCreated on 2026-03-02 with reprex v2.1.1 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In section 5.5 (third edition) we have
fc <- fit |> forecast(h = 30, bootstrap = TRUE)
fc
#> # A fable: 30 x 5 [1]
#> # Key: Symbol, .model [1]
#> Symbol .model day Close .mean
#>
#> 1 GOOG NAIVE(Close) 253 sample[5000] 759.
#> 2 GOOG NAIVE(Close) 254 sample[5000] 759.
#> 3 GOOG NAIVE(Close) 255 sample[5000] 758.
#> 4 GOOG NAIVE(Close) 256 sample[5000] 759.
#> 5 GOOG NAIVE(Close) 257 sample[5000] 759.
#> 6 GOOG NAIVE(Close) 258 sample[5000] 759.
#> 7 GOOG NAIVE(Close) 259 sample[5000] 759.
#> 8 GOOG NAIVE(Close) 260 sample[5000] 759.
#> 9 GOOG NAIVE(Close) 261 sample[5000] 759.
#> 10 GOOG NAIVE(Close) 262 sample[5000] 759.
#> # ℹ 20 more rows
How can I see the (5000) bootstraped values for each forecasting period so that I can extract them as a data frame (where the columns will be the number of simulations and the rows will be the forecast dates)?
Beta Was this translation helpful? Give feedback.
All reactions