MatthieuStigler / tsDyn

tsDyn
tsdyn.googlecode.com
GNU General Public License v2.0
34 stars 20 forks source link

GIRF with TVAR with external variable #44

Closed ninowski closed 2 years ago

ninowski commented 2 years ago

Hi, I am trying to estimate a monetary policy threshold var with an external threshold (recession/expansion defined from unemployment rate). I cannot get the state-dependent GIRFs and the relative confidence interval. Specifically, I would like to get confidence bound from the empirical distribution of simulated GIRFs, assuming normality.

Below there are the codes:

dataset_panel_g7 <- read_excel("Desktop/dataset panel g7.xlsx", sheet = "USA") usa <- data.frame(dataset_panel_g7) g1<-cbind(gdp,p,,i) tvv <- TVAR(g1, lag=2, nthresh=1, thDelay=1, trim=0.1,thVar=un,mTh=1, plot=FALSE) girfff<-GIRF(tvv) A<-plot(girfff, plot_type = c( "line"), n.ahead = c(1, 5, 10), var = unique(girfff$var)[1], n_simu = c(50))

I also tried the tvarGIRF command. In this case it is possible to obtain state-conditional GIRFs, but the TVAR must have no threshold external to the model, so it is not useful in my case.

MatthieuStigler commented 2 years ago

Hi, can you please make your example reproducible? See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

ninowski commented 2 years ago

library(vars) library(dplyr) library(readxl) library(svars) library(reshape2) library(VAR.etp) library(ggplot2) library(stats) library(TSstudio) library(tframePlus) library(writexl) library(forecast) library(devtools) library(readxl) library(tsDyn)

dataset_panel_g7 <- read_excel("Desktop/dataset panel g7.xlsx", sheet = "USA") usa <- data.frame(dataset_panel_g7)

i = ts((usa$i),start=c(1954,3),freq=4) un = ts((usa$un),start=c(1954,3),freq=4) gdp = ts(log(usa$gdp),start=c(1954,3),freq=4) p = ts(log(usa$p),start=c(1954,3),freq=4)

g1<-cbind(gdp,p,i) g3<-subset(g1,start=3) u<-subset(un,start=23) tvv <- TVAR(g1, lag=2, nthresh=1, thDelay=1, trim=0.1,thVar=u,mTh=1, plot=FALSE) girfff<-GIRF(tvv) A<-plot(girfff, plot_type = c( "line"), n.ahead = c(1, 5, 10), var = unique(girfff$var)[1], n_simu = c(50))

dataset_panel_g7.xlsx

MatthieuStigler commented 2 years ago

This is not reproducible :-( As per the link I indicated, you are failing the two first point:

A MRE consists of the following items:

  • a minimal dataset, necessary to demonstrate the problem
    • the minimal runnable code necessary to reproduce the issue, which can be run on the given dataset
ninowski commented 2 years ago

I can't upload the data so I will proceed with the DEMO-DATA. I want to estimate a TVAR with a threshold external to the model. I cannot get the state-dependent GIRFs and the relative confidence interval. Specifically, I would like to get confidence bound from the empirical distribution of simulated GIRFs, assuming normality.

I run the below code:

data(zeroyld) x=rnorm(482) xx=as.vector(x) xx=as.vector(x,mode="any") t5<-TVAR(zeroyld, lag=3, nthresh=1, thDelay=1, thVar=xx, gamma=0.248, trim=0.1, mTh=2, plot=FALSE) library(tvarGIRF)

c1=GIRF(t5, c(0,1),restrict.to=1)

I get an error: c1=GIRF(t5, c(0,1),restrict.to=1) Error: Simulations of models with external transition variables (argument thVar in TVAR) not supported

So i detach the package tvarGIRF and I calculate the GIRF with GIRF {tsDyn}. detach("package:tvarGIRF", unload = TRUE)

c2<-GIRF(t5) A<-plot(c2, plot_type = c( "line"), n.ahead = c(1, 5, 10), var = unique(c2$var)[1], n_simu = c(50))

This command works but I can not obtain the state-dependent GIRF and the relative confidence intervals.

Thank you in advance, Antonino

MatthieuStigler commented 2 years ago

Ok, so I believe your question is actually: how to obtain state-dependent GIRF and the relative confidence intervals, is that correct? This is independent of the external thVar argument, since tsDyn has no buit-in function for state-dependent GIRF.

ninowski commented 2 years ago

The point is that if I use an external threshold I cannot get state-dependent GIRF. But if I use the internal threshold it does

By sending the following codes I can get state-dependent GIRF, but the TVAR is estimated with internal variable-threshold

data(zeroyld) library(tsDyn) library(remotes) install_github("angusmoore/tvarGIRF") t5<-TVAR(zeroyld, lag=3, nthresh=1, thDelay=1, trim=0.1, mTh=2, plot=FALSE)

*just for example H=1 and R=1 c1=GIRF(t5, c(0,1),H=1, R=1, restrict.to=1) plot(c1) c2=GIRF(t5, c(0,1), H=1, R=1, restrict.to=2) plot(c2) * whereas if I use the external threshold I get an error as shown in the example above

many papers report state dependent GIRF and say they used the tsDyn package (For example:

MatthieuStigler commented 2 years ago

I think you are getting confused with tsDyn::GIRF and tvarGIRF::GIRF. tsDyn::GIRF does not provide a final function to plot regime-specific GIRF (even with internal threshold), but its output contains all elements to do so.

I had users asking about this in the past, and helped one user to do it, but haven't heard back from them. I will follow-up, but at this point you might be better-off processing yourself the output to obtain what you are after.

ninowski commented 2 years ago

Thank you for your help. Attached is the output of a GIRF function (resgirf.xlsx), where can I find regime specific girfs?

library(tsDyn) data(zeroyld) x<-rnorm(482) xx<-as.vector(x,mode="any") resTT <- TVAR(zeroyld, lag=2, nthresh=1,thVar=xx, thDelay=1, trim=0.1, mTh=1, plot=FALSE) resGIRF <- GIRF(resTT,n.hist=2,n.shock=2,R=5)

n_simu hist_x1_l1 hist_x1_l2 hist_x2_l1 hist_x2_l2 shock_var1 shock_var2 n.ahead 1 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 0 2 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 1 3 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 2 4 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 3 5 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 4 6 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 5 7 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 6 8 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 7 9 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 8 10 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 9 11 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 10 12 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 0 13 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 1 14 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 2 15 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 3 16 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 4 17 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 5 18 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 6 19 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 7 20 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 8 21 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 9 22 1 8.952 9.20 7.071 7.575 0.06361853 0.2314788 10 23 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 0 24 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 1 25 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 2 26 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 3 27 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 4 28 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 5 29 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 6 30 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 7 31 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 8 32 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 9 33 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 10 34 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 0 35 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 1 36 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 2 37 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 3 38 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 4 39 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 5 40 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 6 41 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 7 42 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 8 43 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 9 44 2 7.834 7.84 8.730 8.683 0.06361853 0.2314788 10 45 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 0 46 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 1 47 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 2 48 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 3 49 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 4 50 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 5 51 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 6 52 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 7 53 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 8 54 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 9 55 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 10 56 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 0 57 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 1 58 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 2 59 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 3 60 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 4 61 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 5 62 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 6 63 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 7 64 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 8 65 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 9 66 3 8.952 9.20 7.071 7.575 -0.17815691 -0.2793551 10 67 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 0 68 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 1 69 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 2 70 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 3 71 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 4 72 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 5 73 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 6 74 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 7 75 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 8 76 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 9 77 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 10 78 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 0 79 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 1 80 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 2 81 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 3 82 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 4 83 4 7.834 7.84 8.730 8.683 -0.17815691 -0.2793551 5 var sim_1 sim_2 girf 1 long.run 7.770371 7.538893 0.23147876 2 long.run 7.711066 7.492760 0.21830633 3 long.run 7.766501 7.564320 0.20218181 4 long.run 7.609066 7.419730 0.18933634 5 long.run 6.707326 6.529676 0.17765000 6 long.run 6.696976 6.529982 0.16699403 7 long.run 7.338583 7.181299 0.15728428 8 long.run 7.159638 7.011208 0.14843018 9 long.run 7.379328 7.238978 0.14034992 10 long.run 7.324958 7.191988 0.13296969 11 long.run 6.845996 6.719773 0.12622282 12 short.run 9.066574 9.002956 0.06361853 13 short.run 8.976962 8.915039 0.06192240 14 short.run 8.996429 8.933166 0.06326230 15 short.run 8.727945 8.663158 0.06478713 16 short.run 8.551809 8.485774 0.06603516 17 short.run 8.432895 8.365833 0.06706165 18 short.run 8.679379 8.611487 0.06789236 19 short.run 8.516662 8.448115 0.06854725 20 short.run 8.559999 8.490954 0.06904455 21 short.run 8.470923 8.401522 0.06940075 22 short.run 8.102483 8.032852 0.06963070 23 long.run 8.981743 8.750265 0.23147876 24 long.run 8.786869 8.568563 0.21830633 25 long.run 9.056627 8.854445 0.20218181 26 long.run 8.459322 8.269986 0.18933634 27 long.run 8.271479 8.093829 0.17765000 28 long.run 8.191547 8.024553 0.16699403 29 long.run 7.949274 7.791990 0.15728428 30 long.run 7.911926 7.763496 0.14843018 31 long.run 8.052772 7.912422 0.14034992 32 long.run 8.085206 7.952236 0.13296969 33 long.run 8.234273 8.108050 0.12622282 34 short.run 7.968317 7.904699 0.06361853 35 short.run 8.111286 8.049364 0.06192240 36 short.run 8.377738 8.314476 0.06326230 37 short.run 8.049732 7.984945 0.06478713 38 short.run 8.014863 7.948828 0.06603516 39 short.run 8.102645 8.035584 0.06706165 40 short.run 8.026490 7.958598 0.06789236 41 short.run 8.161476 8.092929 0.06854725 42 short.run 8.109390 8.040346 0.06904455 43 short.run 8.222246 8.152845 0.06940075 44 short.run 8.322731 8.253100 0.06963070 45 long.run 7.641486 7.920842 -0.27935510 46 long.run 7.609001 7.923603 -0.31460209 47 long.run 7.598735 7.895593 -0.29685828 48 long.run 7.614214 7.897333 -0.28311931 49 long.run 7.187378 7.458236 -0.27085740 50 long.run 7.348930 7.608490 -0.25956006 51 long.run 7.523152 7.772311 -0.24915926 52 long.run 7.612414 7.851988 -0.23957333 53 long.run 7.631270 7.861996 -0.23072568 54 long.run 7.560254 7.782802 -0.22254733 55 long.run 7.804825 8.019801 -0.21497595 56 short.run 9.270971 9.449128 -0.17815691 57 short.run 9.087854 9.271033 -0.18317933 58 short.run 9.196107 9.377836 -0.18172880 59 short.run 9.081498 9.263000 -0.18150112 60 short.run 8.862127 9.043220 -0.18109304 61 short.run 8.752055 8.932545 -0.18049023 62 short.run 8.906173 9.085898 -0.17972508 63 short.run 8.805124 8.983940 -0.17881630 64 short.run 8.765392 8.943172 -0.17778053 65 short.run 8.814580 8.991213 -0.17663286 66 short.run 9.025227 9.200614 -0.17538690 67 long.run 8.103978 8.383333 -0.27935510 68 long.run 7.817201 8.131803 -0.31460209 69 long.run 7.782379 8.079237 -0.29685828 70 long.run 7.675205 7.958324 -0.28311931 71 long.run 7.607583 7.878440 -0.27085740 72 long.run 7.551136 7.810696 -0.25956006 73 long.run 7.502918 7.752077 -0.24915926 74 long.run 7.552130 7.791703 -0.23957333 75 long.run 7.561916 7.792642 -0.23072568 76 long.run 7.652011 7.874558 -0.22254733 77 long.run 7.255811 7.470787 -0.21497595 78 short.run 7.625302 7.803459 -0.17815691 79 short.run 7.430327 7.613506 -0.18317933 80 short.run 7.428285 7.610014 -0.18172880 81 short.run 7.381683 7.563184 -0.18150112 82 short.run 7.382635 7.563728 -0.18109304 83 short.run 7.433504 7.613995 -0.18049023 resgirf.xlsx

MatthieuStigler commented 2 years ago

I am sorry if my previous message was not clear:

tsDyn::GIRF does not provide a final function to plot regime-specific GIRF (even with internal threshold), but its output contains all elements to do so.

Also, again, when providing minimal example, please not the first point:

a minimal dataset, necessary to demonstrate the problem

MatthieuStigler commented 2 years ago

Closing this as two separate issues were created:

https://github.com/MatthieuStigler/tsDyn/issues/46 and #45