Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GIRF and IRF plots for VAR model #56

Closed
taranpreetk opened this issue Apr 11, 2024 · 7 comments
Closed

GIRF and IRF plots for VAR model #56

taranpreetk opened this issue Apr 11, 2024 · 7 comments

Comments

@taranpreetk
Copy link

Dear Team,
I have recently conducted a VAR analysis using the tsDyn::linevar for a model comprising 10 variables. Subsequently, I intend to employ the GIRF (Generalized Impulse Response Function) function within the tsDyn package to further analyze the dynamics of the model.

Upon reviewing the output generated by the GIRF function, I've noted that it yields only 10 plots. In contrast, when utilizing the IRF (Impulse Response Function) function in the same library, we typically obtain 10 plots per impulse variable, resulting in a total of 10*10 plots, where "n" represents the number of variables in the dataset/model.

Given this distinction, I am interested in exploring methods to extract specific responses from the GIRF dataframe. Specifically, I aim to extract responses based on both the impulse and response variables, mirroring the functionality available in IRF plots.

Your expertise and guidance on this matter would be greatly appreciated as we strive to enhance our analytical capabilities within the tsDyn framework.

Thank you for your attention to this request.

@MatthieuStigler
Copy link
Owner

Hi Taranpreet

Thanks for writing on github, that's the right way to do it, better than the google group list. Do not hesitate to add a reproducible example when posting.

It's been a while since I looked at the GIRF, I would need a refresher. I see that the output of plot(GIRF(...)) produces a single plot for multivariate series, which is not the intended output. I should correct that.

Now I am not sure that GIRF should produce a matrix of N x N plots, noting that the shock from variable x is not clearly defined as all shocks are randomly drawn? Have a look at the discussion I had in Chapter 7 - Nonlinear time series in R: Threshold cointegration with tsDyn (don't hesitate to cite if useful) and if you can write down what you have exactly in mind, it might help you figure out how to use the output of GIRF()

best

@taranpreetk
Copy link
Author

taranpreetk commented Apr 11, 2024 via email

@MatthieuStigler
Copy link
Owner

great let me know how that goes

and about what I said:

I see that the output of plot(GIRF(...)) produces a single plot for multivariate series, which is not the intended output. I should correct that.

that's not really correct, I had forgotten about the var argument, that allows you to make a plot for another variable.

@MatthieuStigler
Copy link
Owner

also, to see the link between a GIRF and an IRF, you can simply set all histories to zero, and all but one shock to 1 (iteratively) in a GIRF to get the IRF, see below.

Remember that a GIRF takes random realizations for both histories and shocks. In that case, how would you define an impulse variable?

library(tsDyn)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

var <- lineVar(zeroyld, lag = 1)

girf_var <- GIRF(var, n.hist = 10, n.shock = 10)

GIRF_shock_1 <- GIRF(object = var, 
                     shock_li = list(matrix(c(1, 0), nrow = 1)),
                     hist_li = list(matrix(c(0, 0), nrow = 1)),
                     n.ahead=5)
GIRF_shock_2 <- GIRF(object = var, 
                     shock_li = list(matrix(c(0, 1), nrow = 1)),
                     hist_li = list(matrix(c(0, 0), nrow = 1)),
                     n.ahead=5)
ir <- irf( var, ortho = FALSE, runs=1, n.ahead=5)

all.equal(ir$irf$short.run[,"short.run"],
          subset(GIRF_shock_1, var =="short.run")$girf)
#> [1] TRUE

all.equal(ir$irf$short.run[,"long.run"],
          subset(GIRF_shock_1, var =="long.run")$girf)
#> [1] TRUE


all.equal(ir$irf$long.run[,"short.run"],
          subset(GIRF_shock_2, var =="short.run")$girf)
#> [1] TRUE

all.equal(ir$irf$long.run[,"long.run"],
          subset(GIRF_shock_2, var =="long.run")$girf)
#> [1] TRUE

Created on 2024-04-11 with reprex v2.1.0

@taranpreetk
Copy link
Author

Hi Mattieu,

I ran the example and went through the link you shared. However, this is not what I'm looking for.

I got to know that we can extract the GIRF based on impulse variable. However, I'm not able to do it with respect to response variable as well. Please have a look at these plots I found in this paper.

I want to achieve the same using this package, plotting GIRFs from impulse variables with respect to response variables.

library(tsDyn)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

var <- lineVar(zeroyld, lag = 1)

ir <- irf( var, ortho = FALSE, runs=1, n.ahead=5)

plot(ir)

girf_var <- GIRF(var, n.hist = 1, n.shock = 1, n.ahead = 5)

long.run <- subset(girf_var, var =="long.run")$girf 


short.run <- subset(girf_var, var =="short.run")$girf
n.ahead <- c(0:5)

# Plot long.run
plot(n.ahead, long.run, type = "l", col = "blue", xlab = "n.ahead", ylab = "GIRF",
     main = "GIRF Plot from long.run")
# Add a legend
legend("topright", legend = c("long.run"), col = c("blue"), lty = 1)

# Plot short.run
plot(n.ahead, short.run, type = "l", col = "red", xlab = "n.ahead", ylab = "GIRF",
     main = "GIRF Plot from short.run")

# Add a legend
legend("topright", legend = c("short.run"), col = c("red"), lty = 1)

Created on 2024-04-12 with reprex v2.0.2

If you follow through with the example, I have GIRF from 'long.run' and 'short.run' variables but that's only 2 plots. Whereas in IRFs, I get 4.

Let me know if there is a provision to get plots like IRFs or at least a way in which we can extract the GIRFs based on both impulse and response variables in the output data frame from GIRF function.

@MatthieuStigler
Copy link
Owner

A few remarks:

  • After all, why do you want GIRF if you use a linear model?
  • Same question for the paper you cited, I would suspect they got confused between IRF and GIRF. I don't think that in the original papers of Koop et al they showed such plot?
  • The plots you are making are the opposite, they are showing "by outcome variable" I believe, not "by impulse"

Again, look at the formulas (or my code) to understand the link between a IRF and GIRF: an IRF is a GIRF with hist=0 and shock=[1,0] (as well as shock=[0,1]). You did not specify shock_li as in my code, so got a random draw of two simultaneous shocks (set seed=123 to see which ones)...
with these two simultaneous shocks, which variable is impulsing which one? If you had used (0,1) or (1,0) shocks, the answer would be clear, here it is not (at least to me)?

@MatthieuStigler
Copy link
Owner

As the issue has been inactive for three weeks, I will close it. Feel free to reopen in case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants