ankargren / mfbvar

R package for Mixed-Frequency Bayesian VARs
https://ankargren.github.io/mfbvar
38 stars 21 forks source link

Exogenous Variables #13

Closed adibiasi closed 3 years ago

adibiasi commented 5 years ago

Hi,

I am trying to use exogenous variables. However, whenever I specify the parameter block_exo, R returns "Error in is.data.frame(x) : object 'obj' not found". Below you find a reproducible example using the example from you paper.

Furthermore, I wonder, is it possible to use exogenous variables also for predictions?

variables <- c("CPIAUCSL", "UNRATE", "GDPC1")
out <- map(variables, get_alfred_series,
           observation_start = "1980-01-01",
           observation_end = "2018-11-01",
           realtime_start = "2018-12-10",
           realtime_end = "2018-12-10")

out[[3]]$date <- out[[3]]$date + months(2)

log_diff <- function(x, lag = 1) {
  c(rep(NA, lag), 1200/lag * diff(log(x), lag = lag))
}

mf_df <- reduce(out, full_join, by = c("date", "realtime_period")) %>%
  mutate(CPIAUCSL = log_diff(CPIAUCSL),
         GDPC1 = log_diff(GDPC1, lag = 3)) %>%
  filter(date >= "1980-04-01") %>%
  select(-realtime_period)

# note that I now include the argument "block_exo = c("UNRATE")"
prior <- set_prior(Y = mf_df, block_exo = c("UNRATE"),
                   freq = c("m","m","q"),
                   n_lags = 4, n_burnin = 1000, n_reps = 1000)
ankargren commented 5 years ago

I recently noticed this bug myself, and I will submit a bug fix to CRAN soon. In the meantime, what you can do is add the following after your code:

# The block_exo call is now removed
prior <- set_prior(Y = mf_df, 
                   freq = c("m","m","q"),
                   n_lags = 4, n_burnin = 1000, n_reps = 1000)
# Now adding it ourselves 
prior$block_exo <- 2 

By doing so, we're saying that the variable in position two should be block exogenous. (The way the code is supposed to work is that your argument block_exo = "UNRATE" is translated into a position, but there is a naming error in the code.)

Thanks for sharing your discovery of the bug!

adibiasi commented 5 years ago

Thanks a lot. This works.

I still have a follow-up question: Is it possible to use exogenous variables in the forecasting period. Lets assume I have forecasting model for a small open economy and I want to add US GDP to the model as a proxy for the world business cycle. In this case I would declare this variable as exogenous. Lets further assume I would obtain some good forecast of US GDP elsewhere and I want to condition my forecasts of my small open economy on the forecast of the US GDP. Can I do that?

ankargren commented 5 years ago

Sorry, I forgot to answer your question. In principle, you should be able to just expand your data matrix taking your forecasts as data. So if your data looks like

      DGDP   FGDP
19Q2     X      X
19Q3    NA      X
19Q4    NA      X 

where DGDP is domestic GDP, FGDP is foreign GDP, and X means an observation. The two X for 19Q3 and 19Q4 are really (external) forecasts, but for the purpose of estimation you should be able to treat them as regular data (although in mfbvar only if you use a mixed-frequency model). One issue though that might occur is that estimation breaks down, because you are missing a lot of data at the end, which could potentially lead to numerical problems. Another way to do it is to estimate the model with US GDP as a block exogenous variable and obtaining the model's endogenous forecasts for it. Once you have those, you could reweight the predictive distribution using entropic tilting, see e.g. this paper by Krúger, Clark and Ravazzolo.

adibiasi commented 5 years ago

Perfect! As I do have a mixed-frequency model, I will stick to first option for now. One day maybe Ill start experimenting with entropic tilting.

Cheers!