Johansen Cointegration for More Than Two Price Series in R
Summary
The document explains how to estimate cointegrating relationships among multiple time series using the Johansen procedure in R. It recommends the `ca.jo` function from the vars package, illustrating its use on four series from the package’s Denmark dataset, with choices for deterministic terms, test type, lag order, and seasonal adjustment.
The fitted result contains cointegrating vectors in its V matrix; multiplying the original series by those vectors produces error-correction terms. This extends a two-series workflow based on estimating a hedge ratio and testing a residual spread. The example is instructional rather than an empirical trading result: it does not assess stability, data suitability, or out-of-sample performance. Applying the method requires appropriate model specification and care with time-series properties; the snippet’s settings are examples, not universal defaults.
Key ideas
- The Johansen procedure can estimate cointegrating relationships across more than two time series.
- In R, the vars package provides the `ca.jo` function for this analysis.
- The fitted object’s V matrix contains cointegrating vectors that can be applied to the original series.
- The example demonstrates software usage but does not establish a profitable trading strategy.
Tags
Full text
# How to construct a cointegrating vector using more than 2 price series in R?
# How to construct a cointegrating vector using more than 2 price series in R?
I use now this code from hier Why does the following data fail my cointegration test? with slightly modification of possibility to load something directly from Dropbox file storage .
```
library(RCurl)
library(zoo)
library(tseries)
x <- getURL("https://dl.dropboxusercontent.com/u/12337149/stat/CBA.csv")
y <- read.csv(text = x)
x1 <- getURL("https://dl.dropboxusercontent.com/u/12337149/stat/WBC.csv")
y1 <- read.csv(text = x1)
##gld <- read.csv("CBA.csv", stringsAsFactors=F)
##gdx <- read.csv("GDX.csv", stringsAsFactors=F)
gld <- y
gdx <- y1
gld <- zoo(gld[,5], as.Date(gld[,1]))
gdx <- zoo(gdx[,5], as.Date(gdx[,1]))
t.zoo <- merge(gld, gdx, all=FALSE)
t <- as.data.frame(t.zoo)
cat("Date range is", format(start(t.zoo)), "to", format(end(t.zoo)), "\n")
m <- lm(gld ~ gdx + 0, data=t)
beta <- coef(m)[1]
cat("Assumed hedge ratio is", beta, "\n")
sprd <- t$gld - beta*t$gdx
ht <- adf.test(sprd, alternative="stationary", k=0)
cat("ADF p-value is", ht$p.value, "\n")
if (ht$p.value < 0.05) {
cat("The spread is likely mean-reverting\n")
} else {
cat("The spread is not mean-reverting.\n")
}
```
How to construct a cointegrating vector using more than 2 price series in R?
## Answer by Richard Hardy (score 1, accepted)
https://quant.stackexchange.com/a/24566
Use package "vars" function `ca.jo` for cointegration analysis (the Johansen procedure) of a multivariate time series. Here is a code snippet from the functions' help file:
```
data(denmark)
sjd <- denmark[, c("LRM", "LRY", "IBO", "IDE")]
sjd.vecm <- ca.jo(sjd, ecdet = "const", type="eigen", K=2, spec="longrun",
season=4)
summary(sjd.vecm)
```
It loads a dataset `denmark`, extracts four times series into a matrix `sjd`, conducts the Johansen procedure by the function `ca.jo` and prints its summary.
You can extract the cointegrating vectors by addressing the slot `V` by `@V` like `sjd.vecm@V`. This will be a matrix where each column is a cointegrating vector. You can multiply the original multivariate series (like `sjd`) to the `V` matrix to get the error correction terms.
A good introduction is the "vignette" of the "vars" package and Pfaff "Analysis of Integrated and Cointegrated Time Series with R" (a textbook).Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.