MatthieuStigler / tsDyn

tsDyn
tsdyn.googlecode.com
34 stars 20 forks source link

GIRF and IRF plots for VAR model #56

Closed taranpreetk closed 2 months ago

taranpreetk commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

Hi Mattieu,

Thanks for the quick response.

I will go through the links you have shared and will respond with my thoughts latest by tomorrow’s end of the day.

Thanks and Regards. Taranpreet

On Thu, 11 Apr 2024 at 9:00 PM, Matthieu @.***> wrote:

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 https://www.sciencedirect.com/science/article/pii/S0169716119300355#s0020 (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

— Reply to this email directly, view it on GitHub https://github.com/MatthieuStigler/tsDyn/issues/56#issuecomment-2049967954, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMGULQXKBQKEGUE67LNERGLY42UCJAVCNFSM6AAAAABGCOW6VOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBZHE3DOOJVGQ . You are receiving this because you authored the thread.Message ID: @.***>

MatthieuStigler commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

A few remarks:

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 commented 2 months ago

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