Here's my interpretation of your question (please correct me if this isn't right):
- You're estimating multiple models. Let's say
ETS() and ARIMA() as an example, but any model can be used here.
- You evaluate model performance using
accuracy().
- Looking at the accuracy output, you see that sometimes
ETS() is better, other times ARIMA() is preferred. Because of this, you want to create a new mable that uses whichever model is best.
One approach would be to use if_else() to select between models in a mable based on some condition. The example code below demonstrates how this could be done.
library(fpp3)
fit <- tourism %>%
group_by(State) %>%
summarise(Trips = sum(Trips)) %>%
model(
ets = ETS(Trips),
arima = ARIMA(Trips)
)
fit_accuracy <- accuracy(fit, measures = lst(MASE)) %>%
pivot_wider(names_from = .model, values_from = MASE) %>%
select(-.type)
fit_accuracy
#> # A tibble: 8 x 3
#> State ets arima
#> <chr> <dbl> <dbl>
#> 1 ACT 0.709 0.725
#> 2 New South Wales 0.731 0.719
#> 3 Northern Territory 0.769 0.776
#> 4 Queensland 0.714 0.797
#> 5 South Australia 0.714 0.723
#> 6 Tasmania 0.746 0.778
#> 7 Victoria 0.712 0.728
#> 8 Western Australia 0.619 0.650
best_fit <- fit %>%
transmute(
State, # Need to keep key variables for a valid mable
best_fit = if_else(fit_accuracy$ets < fit_accuracy$arima, ets, arima)
)
best_fit
#> # A mable: 8 x 2
#> # Key: State [8]
#> State best_fit
#> <chr> <model>
#> 1 ACT <ETS(M,A,N)>
#> 2 New South Wales <ARIMA(0,1,1)(0,1,1)[4]>
#> 3 Northern Territory <ETS(M,N,M)>
#> 4 Queensland <ETS(A,N,A)>
#> 5 South Australia <ETS(M,N,A)>
#> 6 Tasmania <ETS(M,N,M)>
#> 7 Victoria <ETS(M,N,M)>
#> 8 Western Australia <ETS(M,N,M)>
Created on 2020-02-25 by the reprex package (v0.3.0)