sweep icon indicating copy to clipboard operation
sweep copied to clipboard

How to fit multiple models for each time series

Open ghost opened this issue 6 years ago • 0 comments

Hi,

I'm playing with sweep and I'm struggling in how to forecast multiple models for each time series in the tibble. I have read the vignettes and this topic it's treated separately and I haven't been able to merge both ideas.

Here is a reproducible example:

library(forecast)
library(tidyquant)
library(timetk)
library(sweep)

monthly_qty_by_cat2_ts <- bike_sales %>%
  mutate(order.month = as_date(as.yearmon(order.date))) %>%
  group_by(category.secondary, order.month) %>%
  summarise(total.qty = sum(quantity)) %>% 
  group_by(category.secondary) %>%
  nest(.key = "data.tbl") %>% 
  mutate(data.ts = map(.x       = data.tbl, 
                       .f       = tk_ts, 
                       select   = -order.month, 
                       start    = 2011,
                       freq     = 12))

# Time Series forecasting
monthly_qty_by_cat2_fit <- monthly_qty_by_cat2_ts %>%
  mutate(fit.ets = map(data.ts, ets))
monthly_qty_by_cat2_fit

# Output
# A tibble: 9 x 4
  category.secondary          data.tbl  data.ts   fit.ets
               <chr>            <list>   <list>    <list>
1 Cross Country Race <tibble [60 x 2]> <S3: ts> <S3: ets>
2         Cyclocross <tibble [60 x 2]> <S3: ts> <S3: ets>
3         Elite Road <tibble [60 x 2]> <S3: ts> <S3: ets>
4     Endurance Road <tibble [60 x 2]> <S3: ts> <S3: ets>
5           Fat Bike <tibble [58 x 2]> <S3: ts> <S3: ets>
6      Over Mountain <tibble [60 x 2]> <S3: ts> <S3: ets>
7              Sport <tibble [60 x 2]> <S3: ts> <S3: ets>
8              Trail <tibble [60 x 2]> <S3: ts> <S3: ets>
9         Triathalon <tibble [60 x 2]> <S3: ts> <S3: ets>

One solutions would be the following one but I belive that there exist a better way to do that

monthly_qty_by_cat2_fit <- monthly_qty_by_cat2_ts %>%
  mutate(fit.ets = map(data.ts, ets),
         fit.arima = map(data.ts, auto.arima),
         ...)

What I'm trying to do is forecast different models as it's done in the vignette SW02_Forecasting_Multiple_Models but here the list of models has the data "hardcoded" and then it's just executed with invoke_map.

How it could be modified to be able to forecast multiple models?

ghost avatar Sep 27 '17 15:09 ghost