franzmohr / bvartools

Functions for Bayesian inference of vector autoregressive and vector error correction models
29 stars 11 forks source link

Error in plot(bvar_est_min) package bvartools #3

Closed dlalounta closed 1 year ago

dlalounta commented 1 year ago

hi all,

I run a simpe var(2) with 4 endogenous variables no exogenous variables only constant. After executing draw_posterior() command I use the plot(object,type="trace") in order to inspect posterior draws. when executing plot() I receive the error message "Error in plot.new() : figure margins too large" thanks in andvance Dionysios

franzmohr commented 1 year ago

Hi Dionysios, I usually get this error, when the plot window in RStudio is too small. Have you tried to increase the area on your screen, where the plot is shown? Best, Franz

dlalounta commented 1 year ago

DearFranz,

Ithank you a lot for your reply. I will try it without R studio and I will inform you in thefollowing days.

Iam writing to you because when estimating a bvar model usingbvartools package, I receive an unexpected error.

Precisely,after loading and transforming data so as to be stationary, Iconstruct two time series objects e2 e3 related to endogenous andexogenous regressors respectively.

NextI create the model using the following command :

model<- gen_var(e2, p = 2, deterministic = "const",iterations= 5000,exogen=e3,s=2, burnin = 1000)

Iadd a minnesota prior:

model_min1<- minnesota_prior(model_min,kappa0=2,kappa1=0.5,kappa2=5,kappa3=5,sigma="AR") model_with_min <-add_priors(model_min,minessota=model_min1)

andfinally I estimate the model :

bvar_est_min<- draw_posterior(model_with_min)

whenthe estimation procedure ends I receive the message : Warning message: In bvar(data = NULL, exogen = NULL, y = object[["data"]][["Y"]], : NAs introduced by coercion

Thisis message is a little strange, at least to me, as when I run thesame commands without the exogenous term, using the dataset e2 or e3, everything seems to be ok.

model<- gen_var(e2, p = 2, deterministic = "const",iterations= 5000, burnin = 1000)

model_min1<- minnesota_prior(model_min,kappa0=2,kappa1=0.5,kappa2=5,kappa3=5,sigma="AR") model_with_min <-add_priors(model_min,minessota=model_min1)

bvar_est_min<- draw_posterior(model_with_min)

Ienclose the script file as well as the data set.

Ithank you in advance for your reply.

Bestregards,

DionysioLalountas

alternativeemail: @.***

On Thursday, March 16, 2023 at 10:31:16 PM GMT+2, Franz Mohr ***@***.***> wrote:  

Hi Dionysios, I usually get this error, when the plot window in RStudio is too small. Have you tried to increase the area on your screen, where the plot is shown? Best, Franz

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

library(bvartools)

load data base

basevar <-read.table('database.csv',sep=",",header=TRUE)

load individual ts

corecpi =basevar$corecpi_sa hicp =basevar$hicp_all_sa nulc=basevar$nulc_sa Dwellings=basevar$Dwellings gfcf=basevar$gfcf gdp=basevar$gdp dollar=basevar$dollar oil=basevar$oil interest=basevar$interest ur=basevar$ur

create time series data time series transformations

corecpi <- ts(corecpi,start=c(1995,1),frequency = 4) hicp <- ts(hicp,start=c(1995,1),frequency = 4) nulc <- ts(nulc,start=c(1995,1),frequency = 4) Dwellings <- ts(Dwellings,start=c(1995,1),frequency = 4) gfcf <- ts(gfcf,start=c(1995,1),frequency = 4) gdp <- ts(gdp,start=c(1995,1),frequency = 4) dollar <- ts(dollar,start=c(1995,1),frequency = 4) oil <- ts(oil,start=c(1995,1),frequency = 4) interest <- ts(interest,start=c(1995,1),frequency = 4) ur <- ts(ur,start=c(1995,1),frequency = 4) businv <- ts(gfcf-Dwellings,start=c(1995,1),frequency = 4) hicp <- diff(log(hicp),lag=4) gdp <- diff(log(gdp),lag=4) businv <-diff(log(businv),lag=4) nulc <- diff(log(nulc),lag=4) dollar <- diff(log(dollar),lag=4) oil <- diff(log(oil),lag=4) interest <-diff(interest,lag=4)

create ts object

e2 <- ts.union(hicp,gdp,businv,nulc) # endogenous variables e2 <- window(e2,start= c(2000,1), end = c(2022, 4)) e3 <- ts.union(dollar,oil,interest) # exogenous variables e3 <- window(e3,start= c(2000,1), end = c(2021, 4)) #missing values for dollar/euro

define model , priors and estimation

model_min <- gen_var(e2, p = 2, deterministic = "const",exogen=e3,s=2,iterations = 5000, burnin = 1000) model_min1 <- minnesota_prior(model_min,kappa0=2,kappa1=0.5,kappa2=5,kappa3=5,sigma="AR") model_with_min <- add_priors(model_min,minessota=model_min1) bvar_est_min <- draw_posterior(model_with_min) summary(bvar_est_min) mean(bvar_pred$fcst$gdp[,2])

srivastava-prachi commented 1 year ago

Hey Franz,

Thanks to many of these codes, it helped me understand BVARS.

I have a few questions and points to mention:

  1. when I use this_posterios in bvartools it doesn't work instead its only works when we put: _bvar_est <- thin(bvarest, thin = 15)

I just wanted to check if it's the same thing and if it is a common issue.

  1. In the start of this data set, you use: data(e1)

    • You take log(diff), but the series is still not stationary as I checked it with adf test, is it fine to run Bayesian VAR with non-stationary series?
    • and why do you choose lag =2, because adf gives lags = 4
  2. Lastly, if one would like to do a bayesian time varying VAR do you have any suggestions? A million thanks in advance, I hope to hear back from you.

Best, Prachi PhD student

franzmohr commented 1 year ago

Hi Prachi,

Thanks, I am glad that the package is helpful.

Ad 1: Yes, thin() is indeed the right function now. thin_posterior is not in the package anymore.

Ad 2: a) Just to be sure, you get the log before taking differences. In your example, it seems that you obtained the differences first and then used log. b) I also tried an ADF and KPSS test and didn't find indications for unit roots for any of the three variables in e1. In general using integrated data in VARs is only a problem for test statistics. In case of BVARs this is less an issue, if the priors are right (-> see mean specifications for VARs with Minnesota priors). c) The model specification follows the illustration in Lütkepohl's textbook (New introduction to multiple timer series analysis) to show that the estimates are similar to OLS estimates. Also, the AIC-test should suggest 2 lags, if I remember correctly.

Ad3: The recent version bvartools also contains code for TVP-VAR models. Feel free to check it out. For TVP-VARs I really like the introduction of Chan, Koop, Poirier and Tobias (2020) Bayesian Econometric Methods. The methods described in the book are also implemented in bvartools. The book also has a website with Matlab code.

Hope this helps.

Best, Franz

Best, Franz

srivastava-prachi commented 1 year ago

Thanks for this answer. This is extremely helpful, and I really appreciate it.

  1. Just quick clarification, I can't run a TVP BVAR if I have non-stationary series, right?

  2. Can you give reference to these Minnesots priors? I could not find e(1) data as I m using the codes from a blog and not the git directly.

  3. The Matlab code is by Chan, right? Is this the code website - https://joshuachan.org/code/code_SMSS.html https://joshuachan.org/code/code_SMSS.html

Do you have any reference example for Ad3) where you use TVP -models?

I don't have large data, I have 90 observations of inflation, and I want to see TV Bayesian VAR for which the inflation sector has spillovers.

Again, thanks a lot for your reply, I really appreciate it.

Best,

On Wed, Jun 21, 2023 at 7:40 PM Franz Mohr @.***> wrote:

Closed #3 https://github.com/franzmohr/bvartools/issues/3 as not planned.

— Reply to this email directly, view it on GitHub https://github.com/franzmohr/bvartools/issues/3#event-9596876288, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO26H5R36RVJSHFSAEHLKSTXMMWXNANCNFSM6AAAAAAV3QQPVA . You are receiving this because you commented.Message ID: @.***>

-- Prachi Srivastava Ph.D. Economics University College Dublin

franzmohr commented 1 year ago

Hi Prachi,

1) Yes, you can run TVP BVARs with non-stationary data. I would suggest that you include an intercept and treat the respective coefficients as trends. But be careful with with prior specifications in this case! I would use a generous prior variance for the initial condition and I also recommend to set the prior mean of the initial condition to the value of the first observations of the respective series - so the intercept already starts at the right level.

2) I would suggest the literature on the Wikipedia page. There are two references to the Minnesota prior: https://en.wikipedia.org/wiki/Bayesian_vector_autoregression. It also contains a reference, which I find very helpful, also with respect to TVP-models http://personal.strath.ac.uk/gary.koop/kk3.pdf

3) This is the link I meant: https://web.ics.purdue.edu/~jltobias/bayesmethods.html VARs are discussed in chapter 20.

Best, Franz

srivastava-prachi commented 1 year ago

Hey Franz,

Again, many thanks for your reply. Your answers are very helpful in understanding both the theory and how to apply BVAR in practice.

I have just a couple of more questions:

  1. Can you share the notebook of this package with me? I ask because, in my analysis, I want to calculate the Generalized FEVD, but the estimates from using the commands could be clearer.

E.g., When I want to calculate GFEVD for RESPONSE on Inflation, then logically, the biggest component in this decomposition should come from its own, but in my case, this is not the case. So, I was thinking of solving it manually.

  1. Can we manually use the Variance - Covariance Matrix estimates from the command "draw_posterios(model_with_prioirs)" to find the GFEVD?

I really appreciate all the replies. Thankyou very much in advance,

Best,

On Sat, Jun 24, 2023 at 3:19 PM Franz Mohr @.***> wrote:

Hi Prachi,

1.

Yes, you can run TVP BVARs with non-stationary data. I would suggest that you include an intercept and treat the respective coefficients as trends. But be careful with with prior specifications in this case! I would use a generous prior variance for the initial condition and I also recommend to set the prior mean of the initial condition to the value of the first observations of the respective series - so the intercept already starts at the right level. 2.

I would suggest the literature on the Wikipedia page. There are two references to the Minnesota prior: https://en.wikipedia.org/wiki/Bayesian_vector_autoregression. It also contains a reference, which I find very helpful, also with respect to TVP-models http://personal.strath.ac.uk/gary.koop/kk3.pdf 3.

This is the link I meant: https://web.ics.purdue.edu/~jltobias/bayesmethods.html VARs are discussed in chapter 20.

Best, Franz

— Reply to this email directly, view it on GitHub https://github.com/franzmohr/bvartools/issues/3#issuecomment-1605489670, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO26H5Q7EEHJGT2LUNXWNJLXM3SMVANCNFSM6AAAAAAV3QQPVA . You are receiving this because you commented.Message ID: @.***>

-- Prachi Srivastava Ph.D. Economics University College Dublin

srivastava-prachi commented 1 year ago

Hey Franz,

I have a few more questions. I am trying to follow the documentation and I m not sure how we can add Minnesota priors in the estimation.

Part 1: I m trying to estimate a BVAR and following this:

data_c1 <- gen_var(ts_c1, p=4, deterministic = "const", iterations = 50000, burnin = 5000)

prior <- minnesota_prior(data_c1)

Adding priors

model_with_priors <- add_priors(data_c1, coef = list(minnestora= TRUE) , sigma =list(minnestora= TRUE) )

And this gives me an error.

Obtaining the posteriors

bvar_est1 <- draw_posterior(prior)summary(bvar_est1)

Part 2:

I m trying to calculate GFEVD on my own to see if the match, and I don't get the same answers.

The results I get from the function are different from my own calculation.

I take the estimate from the GIRF estimate, and then the 4th horizon takes the square of the estimate at the 4th horizon as the numerator and the sum of all shocks squared, and they should give G FEVD but they don't match.

I would be really grateful if we could discuss this and if you can help me resolve this.

Best,

On Tue, Jun 27, 2023 at 9:47 AM Prachi Srivastava @.***> wrote:

Hey Franz,

Again, many thanks for your reply. Your answers are very helpful in understanding both the theory and how to apply BVAR in practice.

I have just a couple of more questions:

  1. Can you share the notebook of this package with me? I ask because, in my analysis, I want to calculate the Generalized FEVD, but the estimates from using the commands could be clearer.

E.g., When I want to calculate GFEVD for RESPONSE on Inflation, then logically, the biggest component in this decomposition should come from its own, but in my case, this is not the case. So, I was thinking of solving it manually.

  1. Can we manually use the Variance - Covariance Matrix estimates from the command "draw_posterios(model_with_prioirs)" to find the GFEVD?

I really appreciate all the replies. Thankyou very much in advance,

Best,

On Sat, Jun 24, 2023 at 3:19 PM Franz Mohr @.***> wrote:

Hi Prachi,

1.

Yes, you can run TVP BVARs with non-stationary data. I would suggest that you include an intercept and treat the respective coefficients as trends. But be careful with with prior specifications in this case! I would use a generous prior variance for the initial condition and I also recommend to set the prior mean of the initial condition to the value of the first observations of the respective series - so the intercept already starts at the right level. 2.

I would suggest the literature on the Wikipedia page. There are two references to the Minnesota prior: https://en.wikipedia.org/wiki/Bayesian_vector_autoregression. It also contains a reference, which I find very helpful, also with respect to TVP-models http://personal.strath.ac.uk/gary.koop/kk3.pdf 3.

This is the link I meant: https://web.ics.purdue.edu/~jltobias/bayesmethods.html VARs are discussed in chapter 20.

Best, Franz

— Reply to this email directly, view it on GitHub https://github.com/franzmohr/bvartools/issues/3#issuecomment-1605489670, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO26H5Q7EEHJGT2LUNXWNJLXM3SMVANCNFSM6AAAAAAV3QQPVA . You are receiving this because you commented.Message ID: @.***>

-- Prachi Srivastava Ph.D. Economics University College Dublin

-- Prachi Srivastava Ph.D. Economics University College Dublin