tidyverts / tidyverts/fabletools

Adding new lst_mdl to existing mable produces unexpected behavior

Open
#402 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
98
Forks
37
PR merge metrics
No merged PRs in 30d

Description

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:

#build both models
models <- tourism %>%
  dplyr::filter(Region == "Melbourne"
         , Purpose == "Business") %>% 
  fabletools::model(
    ARIMA(as.formula(fmlas[1]))
, ARIMA(as.formula(fmlas[2]))
  )
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!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two examples using fabletools::model(), as_mable documentation, and the [[ assignment, then inspect how the resulting object is represented before glance() is called. Done means programmatically adding the second model preserves both model entries and produces the same two-row glance() output as fitting them together.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.