MatthieuStigler / tsDyn

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

VECM including exogenous variables restricted to the cointegration relation #49

Closed cbracco85 closed 1 year ago

cbracco85 commented 1 year ago

Hi all,

I've been trying to estimate a VECM that includes strong/strict exogenous variables in the cointegration relation (by using the LRinclude argument and 2OLS estimation), but kept having problems.

I was wondering if anyone could help me with this matter.

I'd also appreciate some guidance in making predictions in such setting (i.e using certain assumptions about the future values of the strictly exogenous variables and using them as input for forecasts of endogenous ones).

Thanks in advance,

Claudia

MatthieuStigler commented 1 year ago

Can you please give a reproducible code how-to-make-a-great-r-reproducible-example describing your problem?

cbracco85 commented 1 year ago

Hi Matthieu! Of course, I transcribe it below.

This is my first time doing this, so I hope I do it right. Otherwise, please let me know and I´ll give it another try.

Thank you in advance!

Claudia

var1 endog1 endog2 endog3 [1,] 0.06208165 5.038500 -0.0111729457 [2,] 0.07440535 5.579000 -0.0131176949 [3,] 0.05267939 5.296190 -0.0143168567 [4,] 0.06117652 4.275909 -0.0125211544 [5,] 0.07360165 3.098500 -0.0057797893 [6,] 0.05104581 3.124091 0.0051450052 [7,] 0.06667806 2.413182 0.0162031872 [8,] 0.06953104 2.708000 0.0238534267 [9,] 0.06192961 3.907143 0.0251258911 [10,] 0.06465544 3.840909 0.0201515126 [11,] 0.05897749 3.817619 0.0142195024 [12,] 0.07285310 3.250556 0.0106630826 [13,] 0.05397882 3.740870 0.0093429803 [14,] 0.05719960 2.907647 0.0094172614 [15,] 0.07418257 3.544762 0.0088808658 [16,] 0.06836292 4.932857 0.0034313621 [17,] 0.06765013 4.766000 -0.0073641438 [18,] 0.06925147 5.060909 -0.0200183807 [19,] 0.07240278 5.308571 -0.0291878453 [20,] 0.06604394 3.754762 -0.0308854005 [21,] 0.08052013 3.905238 -0.0245577900 [22,] 0.08342519 3.561500 -0.0107073895 [23,] 0.09621872 3.230455 0.0064340713 [24,] 0.08157318 2.935556 0.0247361851 [25,] 0.08709977 3.877273 0.0451376565 [26,] 0.10073198 5.701667 -0.1439437158 [27,] 0.08488368 5.306667 -0.1200752210 [28,] 0.06770073 6.287000 -0.0909003883 [29,] 0.08350173 8.180476 -0.0591974108 [30,] 0.09920760 7.683913 -0.0284721109 [31,] 0.07503566 7.888500 -0.0004979773 [32,] 0.06791372 9.020000 0.0232764078 [33,] 0.06430450 8.913333 0.0428462419 [34,] 0.07101207 8.718421 0.0577822446 [35,] 0.06157487 8.745655 0.0670999053 [36,] 0.08503051 8.483826 0.0717300614 [37,] 0.08743165 7.947642 0.0713559046 [38,] 0.07442848 7.859600 0.0675479080 [39,] 0.08703516 7.521476 0.0624702895 [40,] 0.09506786 7.835305 0.0561860337 [41,] 0.06783243 9.137814 0.0500865668 [42,] 0.06977354 9.642265 0.0449390978 [43,] 0.08116920 10.141636 0.0382509692 [44,] 0.08197801 14.045317 0.0296375550 [45,] 0.08373061 18.990990 0.0188404267 [46,] 0.08976888 16.609962 0.0054572051 [47,] 0.06362006 12.521290 -0.0087434712 [48,] 0.04624995 11.307450 -0.0215608383 [49,] 0.06193370 10.392723 -0.0307807296 [50,] 0.05807567 10.044165 -0.0340677496 [51,] 0.06569116 10.752979 -0.0315257001 [52,] 0.08050852 9.781290 -0.0255263859 [53,] 0.07192613 9.186030 -0.0179856708 [54,] 0.07361813 8.909210 -0.0110710580 [55,] 0.06162315 8.721800 -0.0069227105 [56,] 0.05493922 9.220343 -0.0062046791 [57,] 0.06433064 9.010865 -0.0066352644 [58,] 0.06642638 8.952233 -0.0060589521 [59,] 0.06126255 8.486968 -0.0033783011 [60,] 0.07179281 8.955556 0.0001364097 [61,] 0.06777534 8.201336 0.0013992394 [62,] 0.06627950 7.795579 -0.0012400047 [63,] 0.06571990 8.029645 -0.0086068468 [64,] 0.06368950 7.786059 -0.0187235179 [65,] 0.08121936 8.066732 -0.0267490052 [66,] 0.08335579 7.329086 -0.0283721463 [67,] 0.07285303 6.679891 -0.0204157750 [68,] 0.08490092 7.076020 -0.0040958399 [69,] 0.07187800 7.185710 0.0144588717 [70,] 0.08003461 7.394595 0.0295340343 [71,] 0.08712781 6.987070 0.0382819202 [72,] 0.09065735 6.552704 0.0395043439 [73,] 0.09545767 7.017857 0.0354373686 [74,] 0.07914614 7.834228 0.0279189988 [75,] 0.08015329 7.546833 0.0173711054 [76,] 0.07867020 8.252105 0.0038593690 [77,] 0.08060408 8.640660 -0.0142927171 [78,] 0.06791170 9.262273 -0.0373671496 [79,] 0.07037963 8.632741 -0.0603930866 [80,] 0.07817280 8.386010 -0.0770346021 [81,] 0.08915288 8.424443 -0.0835551228 [82,] 0.10075717 8.693395 -0.0778649817 [83,] 0.05613544 9.655486 -0.0621947380 [84,] 0.07026631 8.996142 -0.0411371097 [85,] 0.07976858 8.871323 -0.0191748266 [86,] 0.08980755 8.725806 0.0003668167 [87,] 0.08339595 9.190605 0.0156751831 [88,] 0.07853824 9.168760 0.0269214517 [89,] 0.06893596 9.146333 0.0335702800 [90,] 0.08393886 8.705139 0.0361388537 [91,] 0.09673966 8.857760 0.0351228511 [92,] 0.09712952 9.429695 0.0301560033 [93,] 0.08400103 9.557710 0.0244725715 [94,] 0.06660435 9.669026 0.0216424644 [95,] 0.09400490 9.714132 0.0217034653 [96,] 0.08509237 9.821628 0.0227234783 [97,] 0.08276166 9.821568 0.0233087715 [98,] 0.08905761 9.564867 0.0205262942 [99,] 0.08416134 9.478545 0.0146219834 [100,] 0.09287913 9.382021 0.0085612123 [101,] 0.09552359 7.725977 0.0036714065 [102,] 0.09614896 10.102618 0.0006391362 [103,] 0.09859389 13.580695 -0.0015786271 [104,] 0.09310272 14.235304 -0.0050466598 [105,] 0.08981710 14.761519 -0.0120353445 [106,] 0.08206721 14.783505 -0.0238622826 [107,] 0.09838169 15.033257 -0.0371738299 [108,] 0.10566044 15.225050 -0.0464995126 [109,] 0.08330255 15.811595 -0.0496352682 [110,] 0.07118451 15.922415 -0.0454157108 [111,] 0.08110948 15.818652 -0.0324520075 [112,] 0.07780733 14.900820 -0.0149387763 [113,] 0.07923589 12.668514 0.0041140295 [114,] 0.07478822 11.010150 0.0207731078 [115,] 0.08565748 13.463386 0.0297188798 [116,] 0.07988070 13.411374 0.0298435328 [117,] 0.08306359 13.150225 0.0248864176 [118,] 0.08470248 14.113476 0.0180516235 [119,] 0.08610773 14.095970 0.0130605974 [120,] 0.08276542 14.150594 0.0136858540

var2 exog1 exog2 [1,] 4.166673e-03 0.0246516361 [2,] 3.631651e-03 0.0259562042 [3,] 3.102381e-03 0.0273836227 [4,] -5.163956e-04 0.0295956948 [5,] 5.163956e-04 0.0299781318 [6,] 6.176036e-03 0.0319152469 [7,] 6.138127e-03 0.0342961072 [8,] 1.367456e-02 0.0357715022 [9,] 1.507917e-03 0.0369344478 [10,] -5.035257e-03 0.0392014822 [11,] 0.000000e+00 0.0407445515 [12,] 6.039274e-03 0.0421011760 [13,] 5.016303e-04 0.0439259722 [14,] 1.503383e-03 0.0449112222 [15,] 4.995015e-03 0.0465925130 [16,] 2.985077e-03 0.0481404293 [17,] 2.480775e-03 0.0487682592 [18,] 5.436139e-03 0.0511682866 [19,] 4.425874e-03 0.0511312313 [20,] -4.918849e-03 0.0511920393 [21,] -4.447746e-03 0.0511549848 [22,] 4.951721e-04 0.0511777877 [23,] 5.430771e-03 0.0510447635 [24,] 1.657906e-03 0.0512499935 [25,] 3.870849e-03 0.0513278938 [26,] 5.186647e-03 0.0512718442 [27,] 2.996169e-03 0.0511046266 [28,] 4.124477e-03 0.0511635360 [29,] 2.314072e-03 0.0512043904 [30,] 1.779012e-03 0.0512908444 [31,] 3.082332e-04 0.0491253461 [32,] 4.228600e-03 0.0484691603 [33,] 3.078494e-03 0.0463930076 [34,] 7.828164e-03 0.0439833920 [35,] 2.893823e-03 0.0413242684 [36,] 3.441775e-03 0.0385608801 [37,] 2.414909e-03 0.0291946583 [38,] 3.571642e-03 0.0256960115 [39,] 2.311707e-03 0.0224236991 [40,] 5.900052e-03 0.0197369389 [41,] 1.042372e-02 0.0198163527 [42,] 7.116065e-03 0.0199898647 [43,] -1.489585e-03 0.0196673240 [44,] 8.547263e-04 0.0184713494 [45,] -8.635616e-03 0.0101216033 [46,] -1.786409e-02 0.0038645231 [47,] -8.267605e-03 0.0017075413 [48,] 2.527574e-03 0.0015687688 [49,] 3.636042e-03 0.0022345016 [50,] -9.877705e-04 0.0018253331 [51,] 1.006576e-03 0.0015308277 [52,] 1.470412e-03 0.0017434793 [53,] 8.265360e-03 0.0020748460 [54,] -2.980099e-04 0.0015687688 [55,] 3.342860e-03 0.0016037134 [56,] 1.929025e-03 0.0014609323 [57,] 2.997435e-03 0.0011753091 [58,] 3.342997e-03 0.0011992806 [59,] 5.200412e-04 0.0011852973 [60,] 6.485219e-04 0.0011673184 [61,] -9.522299e-04 0.0012622031 [62,] 3.313132e-04 0.0016156941 [63,] 2.300141e-04 0.0019750483 [64,] -5.199072e-04 0.0020229525 [65,] -4.188829e-04 0.0017624460 [66,] 1.867509e-03 0.0018173476 [67,] 1.460297e-03 0.0018932068 [68,] 1.613946e-03 0.0019361245 [69,] 3.475799e-03 0.0019031878 [70,] 2.530637e-03 0.0019481012 [71,] 4.008531e-03 0.0018333184 [72,] 3.237795e-03 0.0017285053 [73,] 3.209319e-03 0.0015777547 [74,] 5.160211e-03 0.0013820445 [75,] 4.683116e-03 0.0009565424 [76,] 3.176664e-03 0.0009325650 [77,] 0.000000e+00 0.0009085871 [78,] 2.616611e-03 0.0006797689 [79,] 3.149497e-03 0.0009775221 [80,] 2.169193e-03 0.0008376491 [81,] 6.749797e-04 0.0007197409 [82,] 1.846145e-03 0.0008196640 [83,] 2.376802e-04 0.0007137452 [84,] 2.720492e-03 0.0008446432 [85,] 2.135165e-03 0.0010494491 [86,] 2.091282e-03 0.0012721904 [87,] 1.659411e-03 0.0014509469 [88,] -2.070322e-03 0.0015627782 [89,] -8.267048e-04 0.0016176908 [90,] 2.887682e-04 0.0015847436 [91,] 5.792718e-03 0.0013211269 [92,] 4.759920e-03 0.0014938836 [93,] 2.693165e-03 0.0015897357 [94,] -1.680756e-03 0.0016087053 [95,] -1.210889e-04 0.0016286730 [96,] 1.978830e-03 0.0014419599 [97,] 5.415238e-03 0.0014199913 [98,] -2.815880e-03 0.0014609323 [99,] -2.090162e-03 0.0014489498 [100,] 4.140698e-04 0.0010854107 [101,] 2.377580e-03 0.0009545443 [102,] 1.955539e-03 0.0009225743 [103,] 2.384446e-03 0.0008266582 [104,] 3.768737e-04 0.0008096721 [105,] 5.350879e-04 0.0008546347 [106,] 1.842790e-03 0.0008466415 [107,] 2.640679e-03 0.0008616287 [108,] 2.421242e-03 0.0007137452 [109,] 1.100173e-03 0.0006737730 [110,] 2.039973e-03 0.0007896881 [111,] 1.862450e-03 0.0009045907 [112,] 1.901197e-03 0.0008706209 [113,] 1.320260e-03 0.0009565424 [114,] 1.124852e-03 0.0009135826 [115,] -1.600141e-04 0.0008896042 [116,] 7.158844e-05 0.0008856077 [117,] -1.979335e-04 0.0008856077 [118,] -1.884435e-03 0.0009495490 [119,] -3.089377e-03 0.0012132637 [120,] -6.390693e-03 0.0011643219

just to check whether var2 meets the condition defined in the lineVar

function to consider it as a matrix with exogeneous regressors to include in the long-term relationship

inherits(var2, c("matrix", "numeric")) [1] TRUE

lags<-2 rank<-1

vecm<-VECM(data=var1,lag=lags,r=rank,LRinclude=var2,exogen=NULL, estim="2OLS")

Error in match.arg(LRinclude) : 'arg' must be NULL or a character vector

El jue, 16 mar 2023 a las 5:42, Matthieu @.***>) escribió:

Can you please give a reproducible code how-to-make-a-great-r-reproducible-example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example describing your problem?

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

MatthieuStigler commented 1 year ago

Dear Claudia

This is not really reproducible, but well, find one below as example. And you indeed found a problem, the documentation says that LRinclude can be a matrix but the code doesn't work:

library(tsDyn)

# use data from package
data(barry)
endo <- barry[,1:2]
exo <- barry[,3, drop=FALSE]

## now run
vecm <- VECM(data=endo,lag=2,r=1,LRinclude = exo, estim="2OLS")
#> Error in match.arg(LRinclude): 'arg' must be NULL or a character vector
vecm
#> Error in eval(expr, envir, enclos): object 'vecm' not found

Created on 2023-03-18 with reprex v2.0.2

MatthieuStigler commented 1 year ago

Ok, I initially thought that I could fix this easily, but I realized after this would need many many changes in the package. So I decided this option would not be supported, and I removed the mention from the help file.

If you really wanted to have it, you can use the development version before I reverted it. As you will see, it "works", yet methods like fevd, irf etc won't work

I suggest you to try with packages urca/vars, maybe it is possible there.

remotes::install_github("MatthieuStigler/tsDyn/tsDyn@3fe577fea10")
#> Downloading GitHub repo MatthieuStigler/tsDyn@3fe577fea10
#> vctrs  (0.5.2 -> 0.6.0) [CRAN]
#> rlang  (1.0.6 -> 1.1.0) [CRAN]
#> gtable (0.3.1 -> 0.3.2) [CRAN]
#> Installing 3 packages: vctrs, rlang, gtable
#> Installing packages into '/home/matifou/R/x86_64-pc-linux-gnu-library/4.2'
#> (as 'lib' is unspecified)
#> ── R CMD build ─────────────────────────────────────────────────────────────────
#> * checking for file ‘/tmp/Rtmpb24emy/remotes3f2bd3764f0dc/MatthieuStigler-tsDyn-3fe577f/tsDyn/DESCRIPTION’ ... OK
#> * preparing ‘tsDyn’:
#> * checking DESCRIPTION meta-information ... OK
#> * cleaning src
#> * checking for LF line-endings in source and make files and shell scripts
#> * checking for empty or unneeded directories
#> * building ‘tsDyn_11.0.4.tar.gz’
#> Installing package into '/home/matifou/R/x86_64-pc-linux-gnu-library/4.2'
#> (as 'lib' is unspecified)

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

# use data from package
data(barry)
endo <- barry[,1:2]
exo <- barry[,3, drop=FALSE]

names(exo)
#> NULL
## now run
vecm <- VECM(data=endo,lag=2,r=1,LRinclude = exo, estim="2OLS")
summary(vecm)
#> #############
#> ###Model VECM 
#> #############
#> Full sample size: 324    End sample size: 321
#> Number of variables: 2   Number of estimated slope parameters 12
#> AIC -3964.154    BIC -3915.126   SSR 7.945383
#> Cointegrating vector (estimated by 2OLS):
#>    dolcan      cpiUSA       exo
#> r1      1 -0.05710189 0.0410196
#> 
#> 
#>                 ECT                 Intercept         dolcan -1         
#> Equation dolcan -0.0026(0.0027)     0.0026(0.0014).   0.1629(0.0562)**  
#> Equation cpiUSA 0.0535(0.0330)      0.1283(0.0175)*** 0.6915(0.6917)    
#>                 cpiUSA -1           dolcan -2           cpiUSA -2          
#> Equation dolcan -0.0018(0.0046)     -0.0312(0.0563)     -0.0023(0.0046)    
#> Equation cpiUSA 0.4583(0.0562)***   -0.6524(0.6927)     0.0181(0.0563)

VARrep(vecm)
#> Error in if (LRinclude != "none") betas <- betas[1:k, , drop = FALSE]: the condition has length > 1
fevd(vecm)
#> Error in if (LRinc == "both" | inc == "none" & LRinc == "none") warning("Not guaranted to work with this specification of deterministic terms."): the condition has length > 1
irf(vecm)
#> Error in if (x$inputArgs$LRinclude != "none") stop("irf not available for VECM with LRinclude != 'none'"): the condition has length > 1

Created on 2023-03-18 with reprex v2.0.2

cbracco85 commented 1 year ago

Dear Matthieu, thank you very much!

Tried it with the development version, it works indeed!

I´m sorry to bother you with one further question. I would like to make forecasts from this estimation, using as an input some assumptions about the exogenous variables.

I noticed the function predict uses the "exoPred" argument, but I´m not sure if this is intended for both short and long term exogenous, or just the former. I would appreciate if you could give me some guidance with this. For example, I tried the following code, but I´m getting an error:

exog_pred<-as.data.frame(c(107.00,107.25,107.75,108.00)) names(exog_pred)<-"exo" vecm_pred <- predict(vecm,exoPred = exog_pred,n.ahead=nrow(exog_pred))

Error in Pi_all[, "trend", drop = FALSE] : subscript out of bounds In addition: Warning messages: 1: In if (LRinclude != "none") betas <- betas[1:k, , drop = FALSE] : the condition has length > 1 and only the first element will be used 2: In if (LRinclude != "none") { : the condition has length > 1 and only the first element will be used

Thank you in advance,

Claudia

El sáb, 18 mar 2023 a las 8:32, Matthieu @.***>) escribió:

Ok, I initially thought that I could fix this easily, but I realized after this would need many many changes in the package. So I decided this option would not be supported, and I removed the mention from the help file.

If you really wanted to have it, you can use the development version before I reverted it. As you will see, it "works", yet methods like fevd, irf etc won't work

I suggest you to try with packages urca/vars, maybe it is possible there.

@.")#> Downloading GitHub repo @.#> vctrs (0.5.2 -> 0.6.0) [CRAN]#> rlang (1.0.6 -> 1.1.0) [CRAN]#> gtable (0.3.1 -> 0.3.2) [CRAN]#> Installing 3 packages: vctrs, rlang, gtable#> Installing packages into '/home/matifou/R/x86_64-pc-linux-gnu-library/4.2'#> (as 'lib' is unspecified)#> ── R CMD build ─────────────────────────────────────────────────────────────────#> checking for file ‘/tmp/Rtmpb24emy/remotes3f2bd3764f0dc/MatthieuStigler-tsDyn-3fe577f/tsDyn/DESCRIPTION’ ... OK#> preparing ‘tsDyn’:#> checking DESCRIPTION meta-information ... OK#> cleaning src#> checking for LF line-endings in source and make files and shell scripts#> checking for empty or unneeded directories#> * building ‘tsDyn_11.0.4.tar.gz’#> Installing package into '/home/matifou/R/x86_64-pc-linux-gnu-library/4.2'#> (as 'lib' is unspecified)

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

use data from package

data(barry)endo <- barry[,1:2]exo <- barry[,3, drop=FALSE]

names(exo)#> NULL## now runvecm <- VECM(data=endo,lag=2,r=1,LRinclude = exo, estim="2OLS") summary(vecm)#> ##############> ###Model VECM #> ##############> Full sample size: 324 End sample size: 321#> Number of variables: 2 Number of estimated slope parameters 12#> AIC -3964.154 BIC -3915.126 SSR 7.945383#> Cointegrating vector (estimated by 2OLS):#> dolcan cpiUSA exo#> r1 1 -0.05710189 0.0410196#> #> #> ECT Intercept dolcan -1 #> Equation dolcan -0.0026(0.0027) 0.0026(0.0014). 0.1629(0.0562) #> Equation cpiUSA 0.0535(0.0330) 0.1283(0.0175)** 0.6915(0.6917) #> cpiUSA -1 dolcan -2 cpiUSA -2 #> Equation dolcan -0.0018(0.0046) -0.0312(0.0563) -0.0023(0.0046) #> Equation cpiUSA 0.4583(0.0562) -0.6524(0.6927) 0.0181(0.0563)

VARrep(vecm)#> Error in if (LRinclude != "none") betas <- betas[1:k, , drop = FALSE]: the condition has length > 1 fevd(vecm)#> Error in if (LRinc == "both" | inc == "none" & LRinc == "none") warning("Not guaranted to work with this specification of deterministic terms."): the condition has length > 1 irf(vecm)#> Error in if (x$inputArgs$LRinclude != "none") stop("irf not available for VECM with LRinclude != 'none'"): the condition has length > 1

Created on 2023-03-18 with reprex v2.0.2 https://reprex.tidyverse.org

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

MatthieuStigler commented 1 year ago

Hi Claudia

Yes, this is what I was fearing: the other functions are not going to work with that. Anyway, exoPred was meant to be as exogeneous regressor in the SR parameter, not the long-term ones. So I am afraid tsDyn cannot fulfill your needs.