tidyverts / fabletools

General fable features useful for extension packages
http://fabletools.tidyverts.org/
89 stars 31 forks source link

Add tidyselect to specify multiple responses #361

Open FinYang opened 2 years ago

FinYang commented 2 years ago

I also added across to use when applying transformation(s) to multiple responses, even though transformation is not yet implemented in multivariate forecasting.

To tell tidyselect syntax from transformation functions, I try tidyselect first and pass on whatever failed. Later in transformation, transformation functions will be picked up and bad tidyselect will be caught as "No supported inverse for the %s transformation."

https://github.com/tidyverts/fabletools/blob/b5917f3ade4f7b7c0f4e5f442d013b08b40fd588/R/parse.R#L266-L267

Also included a few bug fixes.

Usage

library(fpp3)
#> ── Attaching packages ─────────────────────────────────────── fpp3 0.4.0.9000 ──
#> ✔ tibble      3.1.7          ✔ tsibble     1.1.1     
#> ✔ dplyr       1.0.9          ✔ tsibbledata 0.4.0.9000
#> ✔ tidyr       1.2.0          ✔ feasts      0.2.2.9000
#> ✔ lubridate   1.8.0          ✔ fable       0.3.1.9000
#> ✔ ggplot2     3.3.6          ✔ fabletools  0.3.2.9000
#> ── 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()
lung_deaths <- cbind(mdeaths, fdeaths) %>%
  as_tsibble(pivot_longer = FALSE)
head(lung_deaths)
#> # A tsibble: 6 x 3 [1M]
#>      index mdeaths fdeaths
#>      <mth>   <dbl>   <dbl>
#> 1 1974 Jan    2134     901
#> 2 1974 Feb    1863     689
#> 3 1974 Mar    1877     827
#> 4 1974 Apr    1877     677
#> 5 1974 May    1492     522
#> 6 1974 Jun    1249     406

# Original behaviour
lung_deaths %>%
  model(VAR(vars(mdeaths, fdeaths) ~ AR(3))) %>% 
  response_vars()
#> [1] "mdeaths" "fdeaths"

# tidyselect syntax
lung_deaths %>%
  model(VAR(c(mdeaths, fdeaths) ~ AR(3))) %>% 
  response_vars()
#> [1] "mdeaths" "fdeaths"
lung_deaths %>%
  model(VAR(mdeaths:fdeaths ~ AR(3))) %>% 
  response_vars()
#> [1] "mdeaths" "fdeaths"
lung_deaths %>%
  model(VAR(ends_with("deaths") ~ AR(3))) %>% 
  response_vars()
#> [1] "mdeaths" "fdeaths"
lung_deaths %>%
  model(VAR(everything() ~ AR(3))) %>% 
  response_vars()
#> [1] "mdeaths" "fdeaths"
lung_deaths %>%
  model(VAR(!fdeaths ~ AR(3))) %>% 
  response_vars()
#> [1] "mdeaths"
lung_deaths %>%
  model(VAR(2 ~ AR(3))) %>% 
  response_vars()
#> [1] "fdeaths"

# Transformation using across
fit <- lung_deaths %>%
  model(VAR(across(c(mdeaths, fdeaths), log) ~ AR(3))) 
fit <- lung_deaths %>%
  model(VAR(across(everything(), list(log, sqrt)) ~ AR(3))) 

# Combination
fit <- lung_deaths %>%
  model(VAR(c(mdeaths, across(c(mdeaths, fdeaths), log)) ~ AR(3))) 

Created on 2022-06-17 by the reprex package (v2.0.1)