gg_tsresiduals() with a fit that has multiple models. How to subset 1*2 mable to a 1*1

I have a ts model fit that is a 'mable' with 2 models:

pacman::p_load(tidyverse, fable, fpp3, feasts)

# aus exports:
## Extract data of interest
oz_exports <- global_economy %>%
  filter(Country =='Australia') %>% 
  select(Exports)

## take a look
oz_exports %>% autoplot(Exports)
## looks like upwards trend with some potential seasonality - try Snaive with drift()

## Define and estimate a model
fit <- oz_exports %>% 
  model(NAIVE(Exports),
        RWDrift = RW(Exports ~ drift()))

I wanted to look at the residuals with gg_tsresiduals:

fit %>% gg_tsresiduals
Error: gg_tsresiduals() must be used with a mable containing only one model.

OK. I tried subsetting fit to get just one model to pass to gg_tsresiduals() at a time.

Here's what fit looks like:

fit
# A mable: 1 x 2
  `NAIVE(Exports)`       RWDrift
           <model>       <model>
1          <NAIVE> <RW w/ drift>

A 1*2 mable.

Tried:

fit$`NAIVE(Exports)`
<lst_mdl[1]>
[1] <NAIVE>

fit$`NAIVE(Exports)` %>% gg_tsresiduals()
Error: gg_tsresiduals() must be used with a mable containing only one model.

Not a mable, it's a list. I need a 1*1 mable to pass to gg_tsresiduals function.

Then tried

fit[1]
# A tibble: 1 × 1
  `NAIVE(Exports)`
           <model>
1          <NAIVE>
> fit[1] %>% gg_tsresiduals()
Error: gg_tsresiduals() must be used with a mable containing only one model.

How can I pass a single model within a fit that contains multiple models to gg_tsresiduals()?

Google-ing on your error message I encountered an earlier question in this forum:
https://forum.posit.co/t/extracting-best-models-from-a-mable/54243/2
Maybe this gives you some ideas?

The linked post does not seem to provide a way to subset a mable, at least as far as I could tell.

And this one : https://stackoverflow.com/questions/63044163/report-models-separately-from-a-mable ?

1 Like

The function from the linked SO post is select:

fit %>% select(<model>) returns a single mable which can be passed to `gg_tsresiduals()`

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.