You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apologies if the title is somewhat unclear; I wasn't quite sure how to phrase the issue, but hopefully the example below is clear.
I'm attempting to add a new lst_mdl to an existing mable object programatically. The following attempt stems from trying to define a number of formulas dynamically and then fit each of them in turn in the fable framework within some user-defined function--in other words, without the ability to use $:
#define some formulas
fmlas <- c("sqrt(Trips) ~ fourier(period = 4L, K = 1)"
, "log(Trips+1) ~ fourier(period = 4L, K = 1)")
#build models for each element of fmlas using the same dataset
model1 <- tourism %>%
dplyr::filter(Region == "Melbourne"
, Purpose == "Business") %>%
fabletools::model(
ARIMA(as.formula(fmlas[1]))
)
model2 <- tourism %>%
dplyr::filter(Region == "Melbourne"
, Purpose == "Business") %>%
fabletools::model(
ARIMA(as.formula(fmlas[2]))
)
tmp <- model1
#add mable from model2 to tmp
tmp[[names(model2)[length(names(model2))]]] <- model2[[names(model2)[length(names(model2))]]]
This produces an output I wasn't expecting (is it because the original tmp object only contains the model name stored in model1? I had trouble understanding the as_mable documentation and am not sure if it's relevant here, particularly the model argument to this function):
glance(tmp)
# A tibble: 1 × 12
Region State Purpose `ARIMA(as.formula(fmlas[2]))` .model sigma2 log_lik AIC AICc BIC ar_roots ma_roots
<chr> <chr> <chr> <model> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <list> <list>
1 Melbourne Victoria Business <LM w/ ARIMA(0,1,1) errors> ARIMA(as.formula(fmlas[1])) 1.91 -136. 281. 281. 290. <cpl [0]> <cpl [1]>
Compare this to building both models within the same call to fabletools::model, and then calling glance on the result:
glance(models3)
# A tibble: 2 × 11
Region State Purpose .model sigma2 log_lik AIC AICc BIC ar_roots ma_roots
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <list> <list>
1 Melbourne Victoria Business ARIMA(as.formula(fmlas[1])) 1.91 -136. 281. 281. 290. <cpl [0]> <cpl [1]>
2 Melbourne Victoria Business ARIMA(as.formula(fmlas[2])) 0.0168 50.4 -92.9 -92.3 -83.4 <cpl [0]> <cpl [1]>
As I mentioned above, I'm trying to build models dynamically without resorting to manually writing out each desired model within fabletools::model, so if there's an alternative approach to achieve this idea I'd be interested in learning about it!
The text was updated successfully, but these errors were encountered:
While the use of [[ fails in the first code block in my original post, you can append a new lst_mdl to an existing mable using the $ operator on both sides of an assignment. Referring back to model1 from the first code block above:
Obtaining a summary for both models in tmp now works:
glance(tmp)
# A tibble: 2 × 11
Region State Purpose .model sigma2 log_lik AIC AICc BIC ar_roots ma_roots
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <list> <list>
1 Melbourne Victoria Business ARIMA(as.formula(fmlas[1])) 1.91 -136. 281. 281. 290. <cpl [0]> <cpl [1]>
2 Melbourne Victoria Business ARIMA(as.formula(fmlas[2])) 0.0168 50.4 -92.9 -92.3 -83.4 <cpl [0]> <cpl [1]>
vincentmgeels
changed the title
Add new mable to existing mable produces unexpected behavior
Adding new lst_mdl to existing mable produces unexpected behavior
Apr 25, 2024
Apologies if the title is somewhat unclear; I wasn't quite sure how to phrase the issue, but hopefully the example below is clear.
I'm attempting to add a new lst_mdl to an existing mable object programatically. The following attempt stems from trying to define a number of formulas dynamically and then fit each of them in turn in the fable framework within some user-defined function--in other words, without the ability to use
$
:This produces an output I wasn't expecting (is it because the original
tmp
object only contains the model name stored inmodel1
? I had trouble understanding theas_mable
documentation and am not sure if it's relevant here, particularly themodel
argument to this function):Compare this to building both models within the same call to
fabletools::model
, and then callingglance
on the result:As I mentioned above, I'm trying to build models dynamically without resorting to manually writing out each desired model within
fabletools::model
, so if there's an alternative approach to achieve this idea I'd be interested in learning about it!The text was updated successfully, but these errors were encountered: