Skip to content

Conversation

@FinYang
Copy link

@FinYang FinYang commented Jun 17, 2022

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."

fabletools/R/parse.R

Lines 266 to 267 in b5917f3

pos <- try(tidyselect::eval_select(lhs, data), silent = TRUE)
if(class(pos) == "try-error" || length(pos) == 0){

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant