MatthieuStigler / tsDyn

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

TVECM does not work with two threshold values #42

Open Justinxi opened 2 years ago

Justinxi commented 2 years ago

Hi, Matthieu

Recently I am trying to use TVECM in my paper. When I write "nthresh=2" in Rstudio, an error happens and it is listed below.

library(tsDyn)

> Registered S3 method overwritten by 'quantmod'

> method from

> as.zoo.data.frame zoo

packageVersion("tsDyn")

> [1]11.0.2

price<-read_excel("E:/spot and futures price.xlsx") tvec<-TVECM(price,nthresh=2,lag=2,ngridBeta=300,ngridTh=300,plot=TRUE,trim=0.05,common="All")

> error in allgammas[seq(from=wh.thresh + ninter, to=length(allgammas) - :only 0's may be mixed with negative subscripts

However, R runs smoothly if nthresh is set to 1. Could you please fix the bug?

Best Xi

MatthieuStigler commented 2 years ago

Sorry, your code is not reproducible. See How to make a great R reproducible example

Here is a minimal reproducible code using reprex(), where the code works fine:

library(tsDyn)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
out <- TVECM(zeroyld,nthresh=2,lag=2,ngridBeta=300,ngridTh=300,plot=TRUE,trim=0.05,common="All")
#> 1233 (1.4%) points of the grid lead to regimes with percentage of observations < trim and were not computed

#> Best threshold from first search -1.312 
#> Best cointegrating value 1.045747 
#> There were  3  thresholds values which minimize the SSR in the conditional step, the first one was taken
#> Second best (conditionnal on the first one) -1.312 -0.529     SSR 150.3263 
#> There were  12  thresholds values which minimize the SSR in the iterative step, the first one was taken
#> Second step best thresholds -1.414 -0.529             SSR 149.0925

Created on 2022-05-27 by the reprex package (v2.0.1)

Justinxi commented 2 years ago
library(tsDyn)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
packageVersion("tsDyn")
#> [1] '11.0.2'
price <- structure(list(`futures price` = c(6.4, 6.23, 6.17, 
    6.49, 6.3, 6.05, 5.89, 5.75, 5.74, 5.91, 5.5, 5.47, 4.98, 
    4.83, 5.32, 4.53, 4.23, 4.01, 4.08, 3.89, 3.7, 3.33, 4.23, 
    4.21, 4.3, 4.07, 4.09, 4.44, 4.37, 4.47, 5.11, 5.13, 5.07, 
    4.99, 4.57, 4.86, 5.18, 5.07, 4.47, 4.2, 4.37, 4.8, 4.57, 
    4.53, 4.14, 4.19, 4.24, 4.18, 3.88, 3.66, 3.45, 3.63, 3.71, 
    3.48, 3.47, 3.93, 4.38, 4.11, 4.44, 4.57, 4.9, 4.76, 4.9, 
    4.87, 4.99, 5.13, 5.14, 4.73, 4.25, 4.29, 4.71, 4.7, 3.05, 
    2.72, 3.04, 3.13, 2.85, 3.05, 2.98, 2.92, 3.06, 3.27, 3.08, 
    3.04, 3.73, 3.86, 3.69, 3.33, 3.22, 3.47, 3.65, 3.49, 3.51, 
    3.36, 3.51, 3.45, 3.6, 3.77, 3.91, 3.87, 3.91, 3.95, 4.07, 
    4, 4.16, 4.38, 4.74, 4.55, 4.67, 4.4, 4.35, 4.09, 4.27, 4.42, 
    4.2, 4.23, 4.28, 4.54, 4.28, 4.23, 4.16, 4.08, 4.03, 4.04, 
    4.12, 4.12, 4.18, 4.2, 4.25, 4.29, 4.32, 4.32, 4.27, 4.33, 
    4.38, 4.41, 4.5, 4.51, 4.44, 4.31, 4.29, 4.35, 4.32, 4.34, 
    4.51, 4.42, 4.53, 4.58, 4.55, 4.49, 4.4, 4.95, 5.32, 5.3, 
    5.12, 5.61, 5.39, 5.46, 5.66, 5.35, 5.35, 5.41, 5.37, 5.46, 
    5.39, 5.02, 5.15, 5.11, 4.97, 4.87, 4.77, 4.62, 4.82, 4.96, 
    5.18, 5.13, 4.59, 4.71, 4.67, 5.01, 4.83, 5.09, 4.81, 4.62, 
    4.56, 4.8, 4.8, 4.49, 4.61, 4.65, 4.59, 4.5, 4.35, 4.42, 
    4.41, 4.43, 4.4, 4.49, 4.43, 4.36, 4.5, 4.48, 4.64, 4.79, 
    4.9, 5.02, 4.9, 4.8, 4.67, 4.91, 4.69, 4.58, 4.51, 4.53, 
    4.63, 4.83, 5.07, 5.07, 4.93, 5.04, 5.03, 5.26, 5.37, 5.52, 
    5.71, 5.52, 5.84, 5.82, 6.07, 6.44, 6.42, 6.4, 6.24, 6.38, 
    6.58, 6.8, 6.74, 6.98, 7.13, 7, 6.21, 6.5, 7.06, 6.61, 6.81, 
    6.77, 6.8, 6.9, 6.89, 6.82, 6.43, 6.29, 5.87, 5.75, 6.01, 
    6.18, 5.86, 5.85, 5.17, 4.34, 4.64, 5.06, 4.87, 4.7, 4.99, 
    4.87, 5.13, 5.33, 5.25, 5.55, 5.56, 5.69, 5.73, 5.08, 5.22, 
    5.43, 5.2, 5.23, 5.24, 5.15, 5.25, 5.31, 5.3, 4.78, 4.8, 
    4.7, 4.84, 5.16, 5.12, 5.09, 5.17, 5.05, 5.14, 5.43, 5.54, 
    5.44, 5.49, 5.55, 5.66, 5.63, 5.7, 5.54, 5.65, 5.79, 5.74, 
    5.7, 5.76, 5.81, 6.03, 6.01, 5.67, 5.56, 5.75, 5.68, 5.74, 
    5.88, 5.94, 6.15, 6, 6.06, 6.17, 6.05, 6.13, 6.19, 6.03, 
    6.21, 6.27, 6.23, 6.2, 5.94, 5.97, 6.13, 6.11, 6.23, 6.38, 
    6.34, 6.43, 6.34, 6.35, 6.32, 6.29, 6.43, 6.38, 6.39, 6.38, 
    6.11, 6.26, 6.27, 6.12, 6.1, 6.04, 5.91, 5.79, 5.97, 6.01, 
    5.98, 5.65, 5.8, 5.91, 5.75, 5.84, 5.69, 5.73, 6.12, 6.04, 
    6.06, 6.08, 6.21, 6.13, 6.14, 6.18, 6.34, 6.41, 6.29, 6.16, 
    6.42, 6.35, 6.58, 6.49, 6.63, 6.76, 6.72, 6.8, 6.79, 6.62, 
    6.9, 7.02, 6.96, 7, 7.05, 7.12, 7.09, 7.04, 7.07, 6.85, 6.84, 
    6.66, 6.66, 6.74, 6.74, 6.66, 6.89, 6.93, 6.84, 6.74, 6.74, 
    7.32, 7.15, 7.16, 7.24, 7.19, 6.78, 6.82, 6.87, 6.85, 6.96, 
    7.07, 7.09, 7.14, 7.07, 7.01, 6.96, 6.93, 7.12, 7.41, 7.65, 
    7.66, 7.48, 7.35, 7.33, 7.71, 7.47, 7.06, 7.1, 6.95, 6.73, 
    6.75, 6.8, 6.68, 6.82, 6.41, 6.46, 6.49, 6.73, 6.67, 7.04, 
    7.04, 7.05, 6.99, 6.97, 6.78, 6.9, 6.94, 7.17, 7.13, 7.03, 
    6.96, 6.81, 6.81, 6.88, 6.85, 7.14, 7.09, 7.27, 7.3, 7.17, 
    7.36, 7.4, 7.56, 7.44, 7.54, 7.63, 7.6, 7.59, 7.6, 7.37, 
    7.35, 7.3, 7.3, 7.22, 7.18, 7.32, 7.24, 7.44, 7.4, 7.44, 
    7.56, 7.5, 7.61, 7.51, 7.41, 7.45, 7.41, 7.46, 7.48, 7.58, 
    7.52, 7.33, 7.44, 7.44), `spot price` = c(6.47, 6.18, 6.18, 
    6.46, 6.27, 6.05, 5.89, 5.73, 5.55, 5.88, 5.46, 5.4, 4.94, 
    4.8, 5.25, 4.55, 4.25, 4.01, 4.06, 3.88, 3.7, 3.37, 4.16, 
    4.49, 4.35, 4.06, 4.1, 4.42, 4.37, 4.49, 5.11, 5.15, 5.07, 
    5.02, 4.58, 4.86, 5.2, 5.07, 4.45, 4.2, 4.36, 4.79, 4.57, 
    4.53, 4.12, 4.17, 4.22, 4.18, 3.88, 3.65, 3.46, 3.65, 3.71, 
    3.49, 3.44, 3.9, 4.4, 4.1, 4.43, 4.56, 4.89, 4.82, 4.87, 
    4.85, 4.95, 5.11, 5.13, 4.73, 4.29, 4.26, 4.67, 4.72, 3.02, 
    2.68, 2.99, 3.02, 2.83, 3.02, 2.95, 2.92, 3.06, 3.25, 3.08, 
    3.04, 2.95, 3.65, 3.68, 3.52, 3.23, 3.23, 3.47, 3.55, 3.49, 
    3.2, 3.3, 3.41, 3.5, 3.65, 3.96, 3.8, 3.9, 3.9, 4.12, 4.14, 
    3.97, 4.55, 4.4, 4.65, 4.63, 4.3, 4.33, 4.19, 4.04, 4.31, 
    4.31, 4.06, 4.23, 4.55, 4.4, 4.16, 4.2, 4.06, 3.95, 3.99, 
    4.11, 4.1, 4.06, 4.1, 4.26, 4.3, 4.28, 4.24, 4.33, 4.36, 
    4.3, 4.4, 4.39, 4.43, 4.48, 4.4, 4.28, 4.33, 4.31, 4.28, 
    4.43, 4.49, 4.44, 4.57, 4.54, 4.49, 4.38, 4.75, 5.05, 5.38, 
    5.13, 5.3, 5.37, 5.42, 5.44, 5.4, 5.3, 5.37, 5.47, 5.33, 
    5.38, 5.15, 5.05, 5.12, 5.05, 4.88, 4.7, 4.58, 4.56, 5.07, 
    5.27, 5.14, 4.78, 4.64, 4.58, 4.61, 4.81, 4.78, 4.87, 4.63, 
    4.64, 4.7, 4.73, 4.78, 4.47, 4.57, 4.63, 4.46, 4.33, 4.44, 
    4.44, 4.48, 4.38, 4.41, 4.41, 4.36, 4.36, 4.47, 4.49, 4.7, 
    4.65, 4.8, 4.77, 4.84, 4.69, 4.7, 4.65, 4.69, 4.56, 4.45, 
    4.52, 4.73, 5.01, 5.04, 5.05, 4.93, 5.02, 4.97, 5.34, 5.47, 
    5.55, 5.6, 5.48, 5.93, 5.97, 6.01, 6.45, 6.34, 6.3, 6.21, 
    6.31, 6.7, 6.74, 6.92, 6.85, 7.1, 6.85, 6.47, 6.83, 6.97, 
    6.65, 6.89, 6.71, 6.72, 6.93, 6.81, 6.51, 6.19, 6.15, 6, 
    6, 6.1, 6.08, 5.8, 5.35, 5.1, 4.17, 4.9, 4.6, 4.75, 4.72, 
    5.03, 5, 5.26, 5.3, 5.23, 5.46, 5.62, 5.77, 5.52, 5.21, 5.39, 
    5.28, 5.03, 5.25, 5.09, 5.1, 5.28, 5.34, 4.75, 4.72, 4.6, 
    4.59, 5.23, 5.08, 4.98, 5.05, 5.1, 4.94, 5.19, 5.5, 5.41, 
    5.45, 5.4, 5.63, 5.68, 5.55, 5.6, 5.56, 5.64, 5.84, 5.77, 
    5.7, 5.82, 5.8, 5.97, 5.91, 5.5, 5.6, 5.72, 5.64, 5.7, 5.88, 
    6.07, 6, 6.02, 6.1, 6.1, 5.99, 6.05, 6.1, 6.11, 6.29, 6.27, 
    6.17, 6.2, 5.93, 5.95, 6.07, 6.22, 6.18, 6.34, 6.28, 6.43, 
    6.34, 6.3, 6.26, 6.38, 6.4, 6.25, 6.35, 6.25, 6.04, 6.25, 
    6.23, 6.07, 6.06, 5.95, 5.81, 5.83, 5.94, 5.98, 5.9, 5.72, 
    5.8, 5.85, 5.68, 5.74, 5.67, 6.06, 6.03, 5.88, 6.01, 6.16, 
    6.23, 6.03, 6.15, 6.16, 6.35, 6.33, 6.26, 6.38, 6.35, 6.3, 
    6.56, 6.61, 6.6, 6.7, 6.68, 6.78, 6.7, 6.64, 6.91, 6.93, 
    6.88, 7.02, 7.05, 7.09, 7.02, 6.95, 7, 6.77, 6.66, 6.58, 
    6.6, 6.65, 6.64, 6.6, 6.93, 6.7, 6.71, 6.59, 6.77, 7.1, 7.05, 
    7.06, 7.25, 7.02, 6.81, 6.76, 6.82, 6.76, 6.92, 6.96, 7.06, 
    7.06, 6.87, 6.92, 6.86, 6.88, 7.29, 7.35, 7.66, 7.62, 7.37, 
    7.26, 7.2, 7.66, 7.29, 6.93, 6.9, 6.82, 6.96, 6.7, 6.66, 
    6.67, 6.61, 6.26, 6.36, 6.45, 6.7, 6.7, 6.94, 7.02, 6.77, 
    7, 6.9, 6.81, 6.79, 7.14, 7.08, 7.12, 6.96, 6.82, 6.77, 6.82, 
    6.83, 6.88, 7.07, 7.06, 7.27, 7.33, 7.16, 7.52, 7.5, 7.5, 
    7.43, 7.5, 7.62, 7.48, 7.54, 7.51, 7.38, 7.3, 7.25, 7.28, 
    7.2, 7.22, 7.23, 7.26, 7.4, 7.38, 7.48, 7.58, 7.52, 7.54, 
    7.48, 7.45, 7.41, 7.43, 7.45, 7.52, 7.5, 7.36, 7.36, 7.42)), 
    class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
        -500L))
tvec <- TVECM(price, nthresh = 2, lag = 2, ngridBeta = 300, ngridTh = 300, plot = TRUE, trim = 0.15)
#> Error in allgammas[seq(from = wh.thresh + ninter, to = length(allgammas) -  : only 0's may be mixed with negative subscripts

Created on 2022-05-31 by the reprex package (v2.0.1)

MatthieuStigler commented 2 years ago

@Justinxi , your example is unfortunately still not self-reproducible. :-( I thought the link How to make a great R reproducible example provided clear guidelines about reproducibility?

But well, can you please attach (in csv format) the smallest part of your dataset such that the error above happens?

Justinxi commented 2 years ago

I have revised my previous self-reproducible example. Please check.

MatthieuStigler commented 2 years ago

good job! Now we have a reproducible example, and I confirm this is a bug. I will need to look into this

Justinxi commented 2 years ago

Could you please let me know if you fix it?