tidyverts / fabletools

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

[FR] Support for automatic box_cox transformation while specifying models #322

Closed raneameya closed 4 months ago

raneameya commented 2 years ago

While dealing with keyed tsibbles that contain many time series, it is useful to compare the forecast (and distributional) accuracy across model specifications relying on untransformed vs transformed time series.

Consider the example below -

library(tsibble)
library(fable)
m <- list()
m[['Untransformed']] <- tourism %>% # This works
  model(
    E = ETS(Trips), 
    A = ARIMA(Trips), 
    T = THETA(Trips)
  )
m[['Transformed']] <- tourism %>% # Can this be made to work?
  model(
    E = ETS(box_cox(Trips, lambda = 'auto')), 
    A = ARIMA(box_cox(Trips, lambda = 'auto')), 
    T = THETA(box_cox(Trips, lambda = 'auto'))
  )

Can m[['Transformed]] be made to work?

adgustaf commented 1 year ago

I'd also find this quite useful. Now I have to run a for loop through each series which defeats the utility of the fable(tools) library

mitchelloharawild commented 4 months ago

Yes, this is now possible since length 1 variables used in the transformation of the response variable are now cached. So you can use feasts::guerrero(resp) to calculate the optimal box-cox transformation parameter.

library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
library(fable)
#> Loading required package: fabletools
library(feasts)

UKLungDeaths <- as_tsibble(cbind(mdeaths, fdeaths))

m <- list()
m[['Untransformed']] <- UKLungDeaths %>% # This works
  model(
    E = ETS(value), 
    A = ARIMA(value), 
    T = THETA(value)
  )
m[['Transformed']] <- UKLungDeaths %>% # Can this be made to work?
  model(
    E = ETS(box_cox(value, lambda = guerrero(value))), 
    A = ARIMA(box_cox(value, lambda = guerrero(value))), 
    T = THETA(box_cox(value, lambda = guerrero(value)))
  )

m
#> $Untransformed
#> # A mable: 2 x 4
#> # Key:     key [2]
#>   key                E                                  A       T
#>   <chr>        <model>                            <model> <model>
#> 1 fdeaths <ETS(M,N,M)>          <ARIMA(0,0,0)(1,1,1)[12]> <THETA>
#> 2 mdeaths <ETS(M,A,A)> <ARIMA(2,0,0)(2,1,0)[12] w/ drift> <THETA>
#> 
#> $Transformed
#> # A mable: 2 x 4
#> # Key:     key [2]
#>   key                E                                  A       T
#>   <chr>        <model>                            <model> <model>
#> 1 fdeaths <ETS(A,N,A)> <ARIMA(0,0,1)(0,1,1)[12] w/ drift> <THETA>
#> 2 mdeaths <ETS(A,N,A)> <ARIMA(0,0,1)(1,1,1)[12] w/ drift> <THETA>

Created on 2024-03-02 with reprex v2.0.2