How can I fix the VECM warning message of "the condition has length > 1 and only the first element will be used"?

Looking at your code, there are potentially two issues:

  1. Reading your dput structure, your column "x8" is actually "X8". Note the capitalization of the x. As such, I think you need to change this line to be read as:
e6<-data1[,c("X8", "x1","x2","x3","x4","x5","x6")] #note the upper case first x.
  1. If that doesn't fix it, please upload your whole data somewhere. I ran your data and it executed fine without errors (I just had to lower the lag as I had fewer observations).

The underlying problem is not in your code, but rather in the source code for the tsDyn package.

Diagnosis

In the definition of tsDyn:::myformat() within the file misc.R, we find these lines:

    if(class(x)=="numeric")
        return(noquote(r))
    if(class(x)=="matrix")
        return(matrix(noquote(r), ncol=ncol(x), nrow=nrow(x)))

This is a rudimentary way to check that the input x is the right kind of object. However, class() returns a character vector of class names: not only the class of the object itself, but also every class from which it inherits.

For example:

x <- matrix(1:9)
class(x)
#> [1] "matrix" "array" 

x <- as.data.frame(x)
class(x)
#> [1] "data.frame"

x <- tibble::as_tibble(x)
class(x)
#> [1] "tbl_df"     "tbl"        "data.frame"

This means that when tsDyn:::myformat() performs its checks, it is getting multiple values:

x <- matrix(1:9)

class(x)
#> [1] "matrix" "array"

class(x) == "matrix"
#> [1]  TRUE FALSE
class(x) == "numeric"
#> [1] FALSE FALSE

Now in R, an if statement expects a condition like so:

A length-one logical vector that is not NA. Conditions of length greater than one are currently accepted with a warning, but only the first element is used.

So when we run if statements with "conditions of length greater than one"

if(class(x) == "matrix") {
  # ...
}

# Equivalently.
if(c(TRUE, FALSE)) {
  # ...
}

we get your warning:

Warning message:
In if (class(x) == "matrix") { :
  the condition has length > 1 and only the first element will be used

Verification

You should verify this is the case for your installation of tsDyn. You can do so by entering

View(tsDyn:::myformat)

and inspecting the source code for the problematic lines: 10

Solution

This fork of the tsDyn package was fixed in November 2020. Its implementation of tsDyn:::myformat() now follows best practice, by using inherits() to check the class of x:

  if(inherits(x, "numeric")) {
    return(noquote(r))
  } else if(inherits(x, "matrix")) {
    return(matrix(noquote(r), ncol=ncol(x), nrow=nrow(x)))
  }

Given that the original version was last updated in 2011, you might want to reinstall from the aforementioned fork, which was last updated in March 2021:

devtools::install_github("MatthieuStigler/tsDyn/tsDyn", ref = "master")

Verification

You should inspect tsDyn::myformat() once more

View(tsDyn:::myformat)

and verify that the problematic lines have been updated: 18