0

I m using tsibble package tourism .

trying to do transformation and fit models to it.

library(tsibble)
library(feasts)
library(dplyr)

sum_tourism <- tourism %>% 
  group_by(Region) %>% 
  summarise(sum_trips=sum(Trips))
fit <- function(ts, col){
  lambda <- ts %>% 
    features(col, features=guerrero) %>% 
    pull(lambda_guerrero)
  mod <- model(ts, arima=ARIMA(box_cox(col, lambda=lambda)))
  return(mod)
}
models <- sum_tourism %>% 
  split(sum_tourism$Region) %>% 
  map(~fit(.x, .x$sum_trips))

it gives me error. pls help. thx

! object 'lambda' not found
Wael
  • 1,640
  • 1
  • 9
  • 20
Bob
  • 13
  • 3
  • 1
    lambda is not found because you don't call it. Change `pull(guerrero_lambda)` to `pull(guerrero$lambda)`. – TarJae Apr 16 '23 at 07:23

1 Answers1

1

To have the lambda object you need some modification in your code read this answer Using rlang's injection paradigm.

with this modification, the lambda vector is calculated and could be returned by the function.

I have some errors with your model please provide the r packages used in your code

library(tsibble)
library(feasts)
library(dplyr)

sum_tourism <- tourism %>% 
  group_by(Region) %>% 
  summarise(sum_trips=sum(Trips))

fit <- function(ts, col){
  lambda <- ts %>% 
    features(!!as.symbol(col), features=guerrero) %>% 
    pull(lambda_guerrero)
  return(lambda)
}
fit(sum_tourism,"sum_trips")
#>  [1]  1.999926773 -0.549847967 -0.168440573  0.666662207 -0.005751637
#>  [6]  0.398992355 -0.221036172 -0.899926773  0.033105893  0.026759753
#> [11] -0.552176875 -0.732231571  0.443440107 -0.657039779  0.677437056
#> [16]  0.788476241  0.838297770  0.147628493 -0.899926773  0.185737932
#> [21] -0.007981998  0.387056949 -0.491568669  0.443289198  0.376520326
#> [26] -0.899926773 -0.899926773 -0.872745709 -0.038044620  0.044313692
#> [31]  0.762098703  0.030736135 -0.899926773  0.455422071  1.160665179
#> [36]  0.096452846 -0.535855516  0.738756222  0.090007587  0.689406832
#> [41]  0.407197974 -0.584847430  0.049477387 -0.304122383 -0.769245111
#> [46]  0.121514057  0.803968558 -0.426019305 -0.899926773 -0.531133400
#> [51]  0.667325493  0.109044833 -0.195416339  1.096501192  0.457220688
#> [56]  1.565333102  0.166391665  1.999926773 -0.013414117 -0.221385671
#> [61]  0.218667327 -0.899926773 -0.306734388 -0.473628340  1.045258935
#> [66]  0.365400240  0.068291558  0.670362886  1.127973610  0.626529021
#> [71] -0.439209555 -0.116154482  0.493627942  0.450986664 -0.353838979
#> [76]  0.232408170

Created on 2023-04-16 with reprex v2.0.2

Wael
  • 1,640
  • 1
  • 9
  • 20
  • the package used is `library(fpp3)` – Bob Apr 17 '23 at 11:16
  • Bob, does these changes Unlock the error ``object 'lambda' not found`` that you have, is it ok ? you have another error ? I do not see ``model`` and ``ARIMA`` functions in the ffp3 package https://cran.r-project.org/web/packages/fpp3/fpp3.pdf – Wael Apr 17 '23 at 12:00
  • Wael, `model` is in `fabletools` package while `ARIMA` is in `fable` package. Both of them are attached packages of `library(fpp3)`. Speaking of the the rlang's in jection paradigm, I can repeat your answer above, but when trying my own code, it created another problem `! object '658.5539' not found` – Bob Apr 18 '23 at 10:44
  • I don't have much experience with forecasting try to reproduce your model for one case before implementing it in the pipe and function – Wael Apr 19 '23 at 14:52