From f4e0b042ead42454fedb69bc57dd6cc6f666ce7f Mon Sep 17 00:00:00 2001 From: mitchelloharawild Date: Mon, 16 Sep 2024 22:14:13 +1000 Subject: [PATCH] Added cointegration tests from urca --- NAMESPACE | 2 + R/features.R | 31 +++++++ man/ACF.Rd | 2 +- man/cointegration_johansen.Rd | 116 +++++++++++++++++++++++++ man/cointegration_phillips_ouliaris.Rd | 43 +++++++++ 5 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 man/cointegration_johansen.Rd create mode 100644 man/cointegration_phillips_ouliaris.Rd diff --git a/NAMESPACE b/NAMESPACE index 1d767f9a..de499163 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -44,6 +44,8 @@ export(autoplot) export(box_pierce) export(classical_decomposition) export(coef_hurst) +export(cointegration_johansen) +export(cointegration_phillips_ouliaris) export(feat_acf) export(feat_intermittent) export(feat_pacf) diff --git a/R/features.R b/R/features.R index bb5b568b..fb38a9d4 100644 --- a/R/features.R +++ b/R/features.R @@ -234,6 +234,37 @@ unitroot_nsdiffs <- function(x, alpha = 0.05, unitroot_fn = ~ feat_stl(.,.period c(nsdiffs = max(differences[c(TRUE, keep)], na.rm = TRUE)) } +#' @inherit urca::ca.jo +#' +#' @seealso [urca::ca.jo()] +#' +#' @export +cointegration_johansen <- function(x, ...) { + require_package("urca") + result <- urca::ca.jo(x, ...) + + pct <- as.numeric(sub("pct", "", colnames(result@cval)))/100 + pval <- mapply( + function(cval, teststat) { + stats::approx(cval, pct, xout=teststat, rule=2)$y + }, split(result@cval, row(result@cval)), result@teststat + ) + names(pval) <- names(result@teststat) <- c(paste0("R<=", rev(seq_len(length(pval) - 1))), "R=0") + c(johansen_stat = list(result@teststat), johansen_pvalue = list(pval)) +} + +#' @inherit urca::ca.po +#' +#' @seealso [urca::ca.po()] +#' +#' @export +cointegration_phillips_ouliaris <- function(x, ...) { + require_package("urca") + result <- urca::ca.po(x, ...) + pval <- stats::approx(result@cval[1,], as.numeric(sub("pct", "", colnames(result@cval)))/100, xout=result@teststat[1], rule=2)$y + c(phillips_ouliaris_stat = result@teststat, phillips_ouliaris_pvalue = pval) +} + #' Longest flat spot length #' #' "Flat spots” are computed by dividing the sample space of a time series into diff --git a/man/ACF.Rd b/man/ACF.Rd index bf70b163..5b8922c4 100644 --- a/man/ACF.Rd +++ b/man/ACF.Rd @@ -38,7 +38,7 @@ CCF( where N is the number of observations and m the number of series. Will be automatically limited to one less than the number of observations in the series.} -\item{type}{character string giving the type of acf to be computed. +\item{type}{character string giving the type of \abbr{acf} to be computed. Allowed values are \code{"correlation"} (the default), \code{"covariance"} or \code{"partial"}. Will be partially matched.} diff --git a/man/cointegration_johansen.Rd b/man/cointegration_johansen.Rd new file mode 100644 index 00000000..69ccb46a --- /dev/null +++ b/man/cointegration_johansen.Rd @@ -0,0 +1,116 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/features.R +\name{cointegration_johansen} +\alias{cointegration_johansen} +\title{Johansen Procedure for VAR} +\usage{ +cointegration_johansen(x, ...) +} +\arguments{ +\item{x}{Data matrix to be investigated for cointegration.} +} +\value{ +An object of class \code{ca.jo}. +} +\description{ +Conducts the Johansen procedure on a given data set. The + \code{"trace"} or \code{"eigen"} statistics are reported and the + matrix of eigenvectors as well as the loading matrix. +} +\details{ +Given a general VAR of the form: + + \deqn{\bold{X}_t = \bold{\Pi}_1 \bold{X}_{t-1} + \dots + \bold{\Pi}_k + \bold{X}_{t-k} + \bold{\mu} + \bold{\Phi D}_t + \bold{\varepsilon}_t + , \quad (t = 1, \dots, T),} + + the following two specifications of a VECM exist: + + \deqn{\Delta \bold{X}_t = \bold{\Gamma}_1 \Delta \bold{X}_{t-1} + + \dots + \bold{\Gamma}_{k-1} \Delta \bold{X}_{t-k+1} + \bold{\Pi + X}_{t-k} + \bold{\mu} + \bold{\Phi D}_t + \bold{\varepsilon}_t} + + where + + \deqn{\bold{\Gamma}_i = - (\bold{I} - \bold{\Pi}_1 - \dots - + \bold{\Pi}_i), \quad (i = 1, \dots , k-1),} + + and + + \deqn{\bold{\Pi} = -(\bold{I} - \bold{\Pi}_1 - \dots - \bold{\Pi}_k)} + + The \eqn{\bold{\Gamma}_i} matrices contain the cumulative long-run + impacts, hence if \code{spec="longrun"} is choosen, the above VECM is + estimated. + + The other VECM specification is of the form: + + \deqn{\Delta \bold{X}_t = \bold{\Gamma}_1 \Delta \bold{X}_{t-1} + + \dots + \bold{\Gamma}_{k-1} \Delta \bold{X}_{t-k+1} + \bold{\Pi + X}_{t-1} + \bold{\mu} + \bold{\Phi D}_t + \bold{\varepsilon}_t} + + where + + \deqn{\bold{\Gamma}_i = - (\bold{\Pi}_{i+1} + \dots + \bold{\Pi}_k), + \quad(i = 1, \dots , k-1),} + + and + + \deqn{\bold{\Pi} = -(\bold{I} - \bold{\Pi}_1 - \dots - \bold{\Pi}_k).} + + The \eqn{\bold{\Pi}} matrix is the same as in the first specification. + However, the \eqn{\bold{\Gamma}_i} matrices now differ, in the sense + that they measure transitory effects, hence by setting + \code{spec="transitory"} the second VECM form is estimated. Please note + that inferences drawn on \eqn{\bold{\Pi}} will be the same, regardless + which specification is choosen and that the explanatory power is the + same, too. + + If \code{"season"} is not NULL, centered seasonal dummy variables are + included. + + If \code{"dumvar"} is not NULL, a matrix of dummy variables is included + in the VECM. Please note, that the number of rows of the matrix + containing the dummy variables must be equal to the row number of + \code{x}. + + Critical values are only reported for systems with less than + 11 variables and are taken from Osterwald-Lenum. +} +\examples{ +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) +# +data(finland) +sjf <- finland +sjf.vecm <- ca.jo(sjf, ecdet = "none", type="eigen", K=2, +spec="longrun", season=4) +summary(sjf.vecm) +} +\references{ +Johansen, S. (1988), Statistical Analysis of Cointegration Vectors, + \emph{Journal of Economic Dynamics and Control}, \bold{12}, 231--254. + + Johansen, S. and Juselius, K. (1990), Maximum Likelihood Estimation and + Inference on Cointegration -- with Applications to the Demand for + Money, \emph{Oxford Bulletin of Economics and Statistics}, \bold{52, + 2}, 169--210. + + Johansen, S. (1991), Estimation and Hypothesis Testing of + Cointegration Vectors in Gaussian Vector Autoregressive Models, + \emph{Econometrica}, \bold{Vol. 59, No. 6}, 1551--1580. + + Osterwald-Lenum, M. (1992), A Note with Quantiles of the Asymptotic + Distribution of the Maximum Likelihood Cointegration Rank Test + Statistics, \emph{Oxford Bulletin of Economics and Statistics}, + \bold{55, 3}, 461--472. +} +\seealso{ +\code{\link[urca:ca.jo]{urca::ca.jo()}} +} +\author{ +Bernhard Pfaff +} diff --git a/man/cointegration_phillips_ouliaris.Rd b/man/cointegration_phillips_ouliaris.Rd new file mode 100644 index 00000000..84d27550 --- /dev/null +++ b/man/cointegration_phillips_ouliaris.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/features.R +\name{cointegration_phillips_ouliaris} +\alias{cointegration_phillips_ouliaris} +\title{Phillips and Ouliaris Cointegration Test} +\usage{ +cointegration_phillips_ouliaris(x, ...) +} +\value{ +An object of class \code{ca.po}. +} +\description{ +Performs the Phillips and Ouliaris \code{"Pu"} and \code{"Pz"} + cointegration test. +} +\details{ +The test \code{"Pz"}, compared to the test \code{"Pu"}, has the + advantage that it is invariant to the normalization of the + cointegration vector, \emph{i.e.} it does not matter which variable + is on the left hand side of the equation. In case convergence + problems are encountered by matrix inversion, one can pass a higher + tolerance level \emph{via} \code{"tol=..."} to the \code{solve()}-function. +} +\examples{ +data(ecb) +m3.real <- ecb[,"m3"]/ecb[,"gdp.defl"] +gdp.real <- ecb[,"gdp.nom"]/ecb[,"gdp.defl"] +rl <- ecb[,"rl"] +ecb.data <- cbind(m3.real, gdp.real, rl) +m3d.po <- ca.po(ecb.data, type="Pz") +summary(m3d.po) +} +\references{ +Phillips, P.C.B. and Ouliaris, S. (1990), Asymptotic Properties of + Residual Based Tests for Cointegration, \emph{Econometrica}, + \bold{Vol. 58, No. 1}, 165--193. +} +\seealso{ +\code{\link[urca:ca.po]{urca::ca.po()}} +} +\author{ +Bernhard Pfaff +}