tidyverts / fable

Tidy time series forecasting
https://fable.tidyverts.org
GNU General Public License v3.0
564 stars 66 forks source link

Impulse response function for nested VAR models #363

Open IcarusAE opened 2 years ago

IcarusAE commented 2 years ago

Hi there,

I would like to know whether it is possible --after fitting a VAR model across different keys in a tsibble--to display the impulse response functions for each of the keys?

Here's a small example

weather <- nycflights13::weather %>% 
  dplyr::select(origin, time_hour, temp, humid, precip)

weather_ts <- as_tsibble(weather, key = origin, index = time_hour)

weather_day <- weather_ts %>%
    group_by(origin) %>%
    index_by(day = as.Date(time_hour)) %>%
    summarise(
      mean_temp = mean(temp),
      mean_humid = mean(humid),
      mean_precip = mean(precip)
    ) %>%
    update_tsibble(index = day)

fit <- weather_day %>%
  model(
    VAR(vars(mean_precip, mean_humid) ~ AR(1)))

fit_coef <- fit %>% 
  tidy(.) 

The example shows lagged effects, e.g., from precip to humidity, for three airports (origin). Now it would be nice to have a way to create a IRF plot for the three keys and scale it to many more keys (although this would surely be messy at some point).

Thanks for suggestions, Holger

mitchelloharawild commented 2 years ago

At this stage there is no functionality for calculating impulse responses for VAR models, but fundamentally there is nothing preventing that functionality from being added. The complexity with adding this to the package is choosing how general to make the functionality. I can see calculating impulse responses being useful for more than just VAR models, so I suspect adding a generic to fabletools would be the way to go.

However more generally, there can be very model specific analysis/results, and I'm not yet sure how best to handle these in terms of design for the package's interface (#216).

IcarusAE commented 2 years ago

Hi Mitch,

thank you for chiming in. I roughly thought about whether it could be possible to use the irf() function from the traditional vars package and map it across the tibble with the key's data sets but how to do that is beyond my competence. As far as I see, the irf() function is applied on a vars model. Any idea whether going for that strategy is reasonable? In my field, the IRFs are the main basis for model interpretation so without them, an important piece would be missing....

All the best, Holger

mitchelloharawild commented 2 years ago

The implementation of VAR in this package does not use the implementation from the vars package, so you won't be able to use vars::irf() on the mable to get the results. I would need to make a fable::irf() method/function of some sort, which is what I was discussing above (where exactly that function should belong).

IcarusAE commented 2 years ago

Ok, I see. Then, my strategy would be to run fable for the overall VAR models and then pick a handful of interesting keys (e.g. those with largest lagged effects or representing the spectrum from positive to negative effects, and repeat the VARs procedure plus irf for those. Not ideal but doable. Thanks for your comments!

All the best Holger