tidyverts / fable

Tidy time series forecasting
https://fable.tidyverts.org
GNU General Public License v3.0
558 stars 65 forks source link

autoplot show_gap on fbl_ts errors #400

Closed r2evans closed 1 year ago

r2evans commented 1 year ago

(In the posit::conf 2023 workshop)

brick_fit <- aus_production |>
  filter(!is.na(Bricks)) |>
  model(
    `Seasonal_naïve` = SNAIVE(Bricks),
    `Naïve` = NAIVE(Bricks),
    Drift = RW(Bricks ~ drift()),
    Mean = MEAN(Bricks)
  )

brick_fit |>
  forecast(h = "5 years") |>
  autoplot(filter(aus_production, Quarter > yearquarter("2000 Q1")), level = 50)

This works, but adding show_gap=FALSE fails with

brick_fit |>
  forecast(h = "5 years") |>
  autoplot(filter(aus_production, Quarter > yearquarter("2000 Q1")), level = 50, show_gap = FALSE)
# Error in `retain_tsibble()`:
# ! The result is not a valid tsibble.
# ℹ Do you need `as_tibble()` to work with data frame?
# Run `rlang::last_trace()` to see where the error occurred.

Oddly enough, very similar models built on slightly different datasets do work:

hh_fit <- hh_budget |>
    model(
    `Naïve` = NAIVE(Wealth),
    Drift = RW(Wealth ~ drift()),
    Mean = MEAN(Wealth)
  )
hh_fit |>
  forecast(h = "5 years") |>
  autoplot(hh_budget, level = NULL, show_gap = FALSE)

aus_fit <- aus_retail |>
  filter(grepl("Clothing", Industry), grepl("South", State)) |>
  model(
    `Seasonal_naïve` = SNAIVE(Turnover),
    `Naïve` = NAIVE(Turnover),
    Drift = RW(Turnover ~ drift()),
    Mean = MEAN(Turnover)
  )
aus_fit |>
  forecast(h = "2 years") |>
  autoplot(
   filter(aus_retail, grepl("Clothing", Industry), grepl("South", State), Month > yearmonth("2010 Jan")),
   level = NULL, show_gap = FALSE)

I'm really enjoying the class, thank you!

sessioninfo::session_info() (Running in emacs/ess.) ```r sessioninfo::session_info() # ─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────── # setting value # version R version 4.3.1 (2023-06-16) # os Ubuntu 23.04 # system x86_64, linux-gnu # ui X11 # language (EN) # collate C.UTF-8 # ctype C.UTF-8 # tz America/New_York # date 2023-09-18 # pandoc 2.17.1.1 @ /usr/bin/ (via rmarkdown) # ─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────── # ! package * version date (UTC) lib source # anytime 0.3.9 2020-08-27 [1] RSPM (R 4.3.0) # askpass 1.1 2019-01-13 [1] RSPM (R 4.3.0) # backports 1.4.1 2021-12-13 [1] RSPM (R 4.3.0) # brio 1.1.3 2021-11-30 [1] RSPM (R 4.3.0) # broom 1.0.5 2023-06-09 [1] RSPM (R 4.3.1) # cachem 1.0.8 2023-05-01 [1] RSPM (R 4.3.0) # callr 3.7.3 2022-11-02 [1] RSPM (R 4.3.0) # cellranger 1.1.0 2016-07-27 [1] RSPM (R 4.3.0) # cli 3.6.1 2023-03-23 [1] RSPM (R 4.3.0) # clipr 0.8.0 2022-02-22 [1] RSPM (R 4.3.0) # colorspace 2.1-0 2023-01-23 [1] RSPM (R 4.3.0) # crayon 1.5.2 2022-09-29 [1] RSPM (R 4.3.0) # curl 5.0.2 2023-08-14 [1] RSPM (R 4.3.1) # data.table 1.14.8 2023-02-17 [1] RSPM (R 4.3.0) # desc 1.4.2 2022-09-08 [1] RSPM (R 4.3.0) # devtools 2.4.5 2022-10-11 [1] RSPM (R 4.3.0) # digest 0.6.33 2023-07-07 [1] RSPM (R 4.3.1) # distributional 0.3.2 2023-03-22 [1] RSPM (R 4.3.1) # dplyr * 1.1.2 2023-04-20 [1] RSPM (R 4.3.0) # ellipsis 0.3.2 2021-04-29 [1] RSPM (R 4.3.0) # evaluate 0.21 2023-05-05 [1] RSPM (R 4.3.1) # fable * 0.3.3 2023-03-22 [1] RSPM (R 4.3.1) # fabletools * 0.3.3 2023-04-04 [1] RSPM (R 4.3.1) # fansi 1.0.4 2023-01-22 [1] RSPM (R 4.3.0) # farver 2.1.1 2022-07-06 [1] RSPM (R 4.3.0) # fastmap 1.1.1 2023-02-24 [1] RSPM (R 4.3.0) # feasts * 0.3.1 2023-03-22 [1] RSPM (R 4.3.1) # forecast 8.21.1 2023-08-31 [1] RSPM (R 4.3.1) # fpp3 * 0.5 2023-02-01 [1] RSPM (R 4.3.1) # fracdiff 1.5-2 2022-10-31 [1] RSPM (R 4.3.1) # fs 1.6.3 2023-07-20 [1] RSPM (R 4.3.1) # generics 0.1.3 2022-07-05 [1] RSPM (R 4.3.0) # GGally 2.1.2 2021-06-21 [1] RSPM (R 4.3.1) # ggplot2 * 3.4.3 2023-08-14 [1] RSPM (R 4.3.1) # glue 1.6.2 2022-02-24 [1] RSPM (R 4.3.0) # gtable 0.3.4 2023-08-21 [1] RSPM (R 4.3.1) # hms 1.1.3 2023-03-21 [1] RSPM (R 4.3.0) # htmltools 0.5.6 2023-08-10 [1] RSPM (R 4.3.1) # htmlwidgets 1.6.2 2023-03-17 [1] RSPM (R 4.3.0) # httpuv 1.6.11 2023-05-11 [1] RSPM (R 4.3.1) # jsonlite 1.8.7 2023-06-29 [1] RSPM (R 4.3.1) # knitr 1.43 2023-05-25 [1] RSPM (R 4.3.1) # labeling 0.4.3 2023-08-29 [1] RSPM (R 4.3.1) # later 1.3.1 2023-05-02 [1] RSPM (R 4.3.0) # lattice 0.21-8 2023-04-05 [2] CRAN (R 4.3.1) # lifecycle 1.0.3 2022-10-07 [1] RSPM (R 4.3.0) # lmtest 0.9-40 2022-03-21 [1] RSPM (R 4.3.1) # lubridate * 1.9.2 2023-02-10 [1] RSPM (R 4.3.0) # magick 2.7.5 2023-08-07 [1] RSPM (R 4.3.1) # magrittr 2.0.3 2022-03-30 [1] RSPM (R 4.3.0) # memoise 2.0.1 2021-11-26 [1] RSPM (R 4.3.0) # mime 0.12 2021-09-28 [1] RSPM (R 4.3.0) # miniUI 0.1.1.1 2018-05-18 [1] RSPM (R 4.3.0) # munsell 0.5.0 2018-06-12 [1] RSPM (R 4.3.0) # nlme 3.1-163 2023-08-09 [1] RSPM (R 4.3.1) # nnet 7.3-19 2023-05-03 [2] CRAN (R 4.3.1) # pdftools 3.3.3 2023-02-07 [1] RSPM (R 4.3.0) # pillar 1.9.0 2023-03-22 [1] RSPM (R 4.3.0) # pkgbuild 1.4.2 2023-06-26 [1] RSPM (R 4.3.1) # pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.3.0) # pkgload 1.3.2.1 2023-07-08 [1] RSPM (R 4.3.1) # plyr 1.8.8 2022-11-11 [1] RSPM (R 4.3.0) # png 0.1-8 2022-11-29 [1] RSPM (R 4.3.0) # prettyunits 1.1.1 2020-01-24 [1] RSPM (R 4.3.0) # processx 3.8.2 2023-06-30 [1] RSPM (R 4.3.1) # profvis 0.3.8 2023-05-02 [1] RSPM (R 4.3.0) # progress 1.2.2 2019-05-16 [1] RSPM (R 4.3.0) # progressr 0.14.0 2023-08-10 [1] RSPM (R 4.3.1) # promises 1.2.1 2023-08-10 [1] RSPM (R 4.3.1) # ps 1.7.5 2023-04-18 [1] RSPM (R 4.3.0) # purrr 1.0.2 2023-08-10 [1] RSPM (R 4.3.1) # qpdf 1.3.2 2023-03-17 [1] RSPM (R 4.3.0) # quadprog 1.5-8 2019-11-20 [1] RSPM (R 4.3.1) # quantmod 0.4.25 2023-08-22 [1] RSPM (R 4.3.1) # R6 2.5.1 2021-08-19 [1] RSPM (R 4.3.0) # rappdirs 0.3.3 2021-01-31 [1] RSPM (R 4.3.0) # RColorBrewer 1.1-3 2022-04-03 [1] RSPM (R 4.3.0) # Rcpp 1.0.11 2023-07-06 [1] RSPM (R 4.3.1) # readxl 1.4.3 2023-07-06 [1] RSPM (R 4.3.1) # remotes 2.4.2.1 2023-07-18 [1] RSPM (R 4.3.1) # reshape 0.8.9 2022-04-12 [1] RSPM (R 4.3.1) # rlang 1.1.1 2023-04-28 [1] RSPM (R 4.3.0) # rmarkdown 2.24 2023-08-14 [1] RSPM (R 4.3.1) # R rmote * 0.3.4.9000 [?] # rprojroot 2.0.3 2022-04-02 [1] RSPM (R 4.3.0) # rstudioapi 0.15.0 2023-07-07 [1] RSPM (R 4.3.1) # scales 1.2.1 2022-08-20 [1] RSPM (R 4.3.0) # servr 0.27 2023-05-02 [1] RSPM (R 4.3.1) # sessioninfo 1.2.2 2021-12-06 [1] RSPM (R 4.3.0) # shiny 1.7.5 2023-08-12 [1] RSPM (R 4.3.1) # slider 0.3.0 2022-11-16 [1] RSPM (R 4.3.1) # stringi 1.7.12 2023-01-11 [1] RSPM (R 4.3.0) # stringr 1.5.0 2022-12-02 [1] RSPM (R 4.3.0) # sugrrants * 0.2.8 2020-10-05 [1] RSPM (R 4.3.1) # testthat * 3.1.10 2023-07-06 [1] RSPM (R 4.3.1) # tibble * 3.2.1 2023-03-20 [1] RSPM (R 4.3.0) # tidyr * 1.3.0 2023-01-24 [1] RSPM (R 4.3.0) # tidyselect 1.2.0 2022-10-10 [1] RSPM (R 4.3.0) # timechange 0.2.0 2023-01-11 [1] RSPM (R 4.3.0) # timeDate 4022.108 2023-01-07 [1] RSPM (R 4.3.0) # tseries 0.10-54 2023-05-02 [1] RSPM (R 4.3.1) # tsibble * 1.1.3 2022-10-09 [1] RSPM (R 4.3.1) # tsibbledata * 0.4.1 2022-09-01 [1] RSPM (R 4.3.1) # TTR 0.24.3 2021-12-12 [1] RSPM (R 4.3.1) # urca 1.3-3 2022-08-29 [1] RSPM (R 4.3.1) # urlchecker 1.0.1 2021-11-30 [1] RSPM (R 4.3.0) # usethis 2.2.2 2023-07-06 [1] RSPM (R 4.3.1) # utf8 1.2.3 2023-01-31 [1] RSPM (R 4.3.0) # vctrs 0.6.3 2023-06-14 [1] RSPM (R 4.3.1) # viridisLite 0.4.2 2023-05-02 [1] RSPM (R 4.3.0) # warp 0.2.0 2020-10-21 [1] RSPM (R 4.3.1) # webshot 0.5.5 2023-06-26 [1] RSPM (R 4.3.1) # withr 2.5.0 2022-03-03 [1] RSPM (R 4.3.0) # xfun 0.40 2023-08-09 [1] RSPM (R 4.3.1) # xtable 1.8-4 2019-04-21 [1] RSPM (R 4.3.0) # xts 0.13.1 2023-04-16 [1] RSPM (R 4.3.1) # zoo 1.8-12 2023-04-13 [1] RSPM (R 4.3.0) # [1] /home/r2/R/x86_64-pc-linux-gnu-library/4.3 # [2] /opt/R/4.3.1/lib/R/library # R ── Package was removed from disk. # ────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ```
DavisVaughan commented 1 year ago

The problem was that you did filter(!is.na(Bricks)) when fitting the models, but not when making the plot!

library(fable)
#> Loading required package: fabletools
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tsibbledata)
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union

aus_production <- aus_production |>
  filter(!is.na(Bricks))

brick_fit <- aus_production |>
  model(
    `Seasonal_naïve` = SNAIVE(Bricks),
    `Naïve` = NAIVE(Bricks),
    Drift = RW(Bricks ~ drift()),
    Mean = MEAN(Bricks)
  )

brick_fit |>
  forecast(h = "5 years") |>
  autoplot(
    filter(aus_production, Quarter > yearquarter("2000 Q1")),
    level = 50,
    show_gap = FALSE
  )

Created on 2023-09-18 with reprex v2.0.2