Skip to content

Commit

Permalink
Added cointegration tests from urca
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelloharawild committed Sep 16, 2024
1 parent e742098 commit f4e0b04
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions R/features.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion man/ACF.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions man/cointegration_johansen.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions man/cointegration_phillips_ouliaris.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f4e0b04

Please sign in to comment.