## Functions

R.version.string
package
sapply(package, packageVersion)
 "R version 3.6.1 (2019-07-05)"
$tseries  0 10 47$CADFtest
 0 3 3

### Code & Help

list(function (x, alternative = c("stationary", "explosive"),
k = trunc((length(x) - 1)^(1/3)))
{
if ((NCOL(x) > 1) || is.data.frame(x))
stop("x is not a vector or univariate time series")
if (any(is.na(x)))
stop("NAs in x")
if (k < 0)
stop("k negative")
alternative <- match.arg(alternative)
DNAME <- deparse(substitute(x))
k <- k + 1
x <- as.vector(x, mode = "double")
y <- diff(x)
n <- length(y)
z <- embed(y, k)
yt <- z[, 1]
xt1 <- x[k:n]
tt <- k:n
if (k > 1) {
yt1 <- z[, 2:k]
res <- lm(yt ~ xt1 + 1 + tt + yt1)
}
else res <- lm(yt ~ xt1 + 1 + tt)
res.sum <- summary(res)
STAT <- res.sum$coefficients[2, 1]/res.sum$coefficients[2,
2]
table <- cbind(c(4.38, 4.15, 4.04, 3.99, 3.98, 3.96), c(3.95,
3.8, 3.73, 3.69, 3.68, 3.66), c(3.6, 3.5, 3.45, 3.43,
3.42, 3.41), c(3.24, 3.18, 3.15, 3.13, 3.13, 3.12), c(1.14,
1.19, 1.22, 1.23, 1.24, 1.25), c(0.8, 0.87, 0.9, 0.92,
0.93, 0.94), c(0.5, 0.58, 0.62, 0.64, 0.65, 0.66), c(0.15,
0.24, 0.28, 0.31, 0.32, 0.33))
table <- -table
tablen <- dim(table)
tableT <- c(25, 50, 100, 250, 500, 100000)
tablep <- c(0.01, 0.025, 0.05, 0.1, 0.9, 0.95, 0.975, 0.99)
tableipl <- numeric(tablen)
for (i in (1:tablen)) tableipl[i] <- approx(tableT, table[,
i], n, rule = 2)$y interpol <- approx(tableipl, tablep, STAT, rule = 2)$y
if (!is.na(STAT) && is.na(approx(tableipl, tablep, STAT,
rule = 1)$y)) if (interpol == min(tablep)) warning("p-value smaller than printed p-value") else warning("p-value greater than printed p-value") if (alternative == "stationary") PVAL <- interpol else if (alternative == "explosive") PVAL <- 1 - interpol else stop("irregular alternative") PARAMETER <- k - 1 METHOD <- "Augmented Dickey-Fuller Test" names(STAT) <- "Dickey-Fuller" names(PARAMETER) <- "Lag order" structure(list(statistic = STAT, parameter = PARAMETER, alternative = alternative, p.value = PVAL, method = METHOD, data.name = DNAME), class = "htest") })  adf.test R Documentation ## Augmented Dickey–Fuller Test ### Description Computes the Augmented Dickey-Fuller test for the null that x has a unit root. ### Usage adf.test(x, alternative = c("stationary", "explosive"), k = trunc((length(x)-1)^(1/3)))  ### Arguments  x a numeric vector or time series. alternative indicates the alternative hypothesis and must be one of “stationary” (default) or “explosive”. You can specify just the initial letter. k the lag order to calculate the test statistic. ### Details The general regression equation which incorporates a constant and a linear trend is used and the t-statistic for a first order autoregressive coefficient equals one is computed. The number of lags used in the regression is k. The default value of trunc((length(x)-1)^(1/3)) corresponds to the suggested upper bound on the rate at which the number of lags, k, should be made to grow with the sample size for the general ARMA(p,q) setup. Note that for k equals zero the standard Dickey-Fuller test is computed. The p-values are interpolated from Table 4.2, p. 103 of Banerjee et al. (1993). If the computed statistic is outside the table of critical values, then a warning message is generated. Missing values are not allowed. ### Value A list with class “htest” containing the following components:  statistic the value of the test statistic. parameter the lag order. p.value the p-value of the test. method a character string indicating what type of test was performed. data.name a character string giving the name of the data. alternative a character string describing the alternative hypothesis. ### Author(s) A. Trapletti ### References A. Banerjee, J. J. Dolado, J. W. Galbraith, and D. F. Hendry (1993): Cointegration, Error Correction, and the Econometric Analysis of Non-Stationary Data, Oxford University Press, Oxford. S. E. Said and D. A. Dickey (1984): Testing for Unit Roots in Autoregressive-Moving Average Models of Unknown Order. Biometrika 71, 599–607. ### See Also pp.test ### Examples x <- rnorm(1000) # no unit-root adf.test(x) y <- diffinv(x) # contains a unit-root adf.test(y)  #### CADFtest list(function (model, X = NULL, type = c("trend", "drift", "none"), data = list(), max.lag.y = 1, min.lag.X = 0, max.lag.X = 0, dname = NULL, criterion = c("none", "BIC", "AIC", "HQC", "MAIC"), ...) UseMethod("CADFtest"))  CADFtest R Documentation ## Hansen’s Covariate-Augmented Dickey Fuller (CADF) test for unit roots ### Description This function is an interface to CADFtest.default that computes the CADF unit root test proposed in Hansen (1995). The asymptotic p-values of the test are also computed along the lines proposed in Costantini et al. (2007). Automatic model selection is allowed. A full description and some applications can be found in Lupi (2009). ### Usage CADFtest(model, X=NULL, type=c("trend", "drift", "none"), data=list(), max.lag.y=1, min.lag.X=0, max.lag.X=0, dname=NULL, criterion=c("none", "BIC", "AIC", "HQC", "MAIC"), ...)  ### Arguments  model a formula of the kind y ~ x1 + x2 containing the variable y to be tested and the stationary covariate(s) to be used in the test. If the model is specified as y ~ 1, then an ordinary ADF is carried out. Note that the specification y ~ . here does not imply a model with all the disposable regressors, but rather a model with no stationary covariate (which correspons to an ADF test). This is because the stationary covariates have to be explicitly indicated (they are usually one or two). An ordinary ADF is performed also if model=y is specified, where y is a vector or a time series. It should be noted that model is not the actual model, but rather a representation that is used to simplify variable specification. The covariates are assumed to be stationary. X if model=y, a matrix or a vector time series of stationary covariates X can be passed directly, instead of using the formula expression. However, the formula expression should in general be preferred. type defines the deterministic kernel used in the test. It accepts the values used in package urca. It specifies if the underlying model must be with linear trend (“trend”, the default), with constant (“drift”) or without constant (“none”). data data to be used (optional). This argument is effective only when model is passed as a formula. max.lag.y maximum number of lags allowed for the lagged differences of the variable to be tested. min.lag.X if negative it is maximum lead allowed for the covariates. If zero, it is the minimum lag allowed for the covariates. max.lag.X maximum lag allowed for the covariates. dname NULL or character. It can be used to give a special name to the model. If the NULL default is accepted and the model is specified using a formula notation, then dname is computed according to the used formula. criterion it can be either “none” (the default), “BIC”, “AIC”, “HQC” or “MAIC”. If criterion=“none”, no automatic model selection is performed. Otherwise, automatic model selection is performed using the specified criterion. In this case, the max and min orders serve as upper and lower bounds in the model selection. … Extra arguments that can be set to use special kernels, prewhitening, etc. in the estimation of ρ^2. A Quadratic kernel with a VAR(1) prewhitening is the default choice. To set these extra arguments to different values, see kernHAC in package sandwich (Zeileis, 2004, 2006). If Hansen’s results have to be duplicated, then kernel=“Parzen” and prewhite=FALSE must be specified. ### Value The function returns an object of class c(“CADFtest”, “htest”) containing:  statistic the t test statistic. parameter the estimated nuisance parameter ρ^2 (see Hansen, 1995, p. 1150). method the test performed: it can be either ADF or CADF. p.value the p-value of the test. data.name the data name. max.lag.y the maximum lag of the differences of the dependent variable. min.lag.X the maximum lead of the stationary covariate(s). max.lag.X the maximum lag of the stationary covariate(s). AIC the value of the AIC for the selected model. BIC the value of the BIC for the selected model. HQC the value of the HQC for the selected model. MAIC the value of the MAIC for the selected model. est.model the estimated model. estimate the estimated value of the parameter of the lagged dependent variable. null.value the value of the parameter of the lagged dependent variable under the null. alternative the alternative hypothesis. call the call to the function. type the deterministic kernel used. ### Author(s) Claudio Lupi ### References Costantini M, Lupi C, Popp S (2007). A Panel-CADF Test for Unit Roots, University of Molise, Economics & Statistics Discussion Paper 39/07. http://econpapers.repec.org/paper/molecsdps/esdp07039.htm Hansen BE (1995). Rethinking the Univariate Approach to Unit Root Testing: Using Covariates to Increase Power, Econometric Theory, 11(5), 1148–1171. Lupi C (2009). Unit Root CADF Testing with R, Journal of Statistical Software, 32(2), 1–19. http://www.jstatsoft.org/v32/i02/ Zeileis A (2004). Econometric Computing with HC and HAC Covariance Matrix Estimators, Journal of Statistical Software, 11(10), 1–17. http://www.jstatsoft.org/v11/i10/ Zeileis A (2006). Object-Oriented Computation of Sandwich Estimators, Journal of Statistical Software, 16(9), 1–16. http://www.jstatsoft.org/v16/i09/. ### See Also fUnitRoots, urca ### Examples ##---- ADF test on extended Nelson-Plosser data ---- ##-- Data taken from package urca data(npext, package="urca") ADFt <- CADFtest(npext$gnpperca, max.lag.y=3, type="trend")

##---- CADF test on extended Nelson-Plosser data ----
data(npext, package="urca")
npext$unemrate <- exp(npext$unemploy)      # compute unemployment rate
L <- ts(npext, start=1860)                 # time series of levels
D <- diff(L)                               # time series of diffs
S <- window(ts.intersect(L,D), start=1909) # select same sample as Hansen's
kernel="Parzen", prewhite=FALSE)


#### unitrootTest

list(function (x, lags = 1, type = c("nc", "c", "ct"), title = NULL,
description = NULL)
{
CALL <- match.call()
test <- list()
DNAME <- deparse(substitute(x))
test$data.name <- DNAME if (class(x) == "timeSeries") x <- series(x) x = as.vector(x) if (lags < 0) stop("Lags are negative") type = type lags = lags + 1 y = diff(x) n = length(y) z = embed(y, lags) y.diff = z[, 1] y.lag.1 = x[lags:n] tt = lags:n if (lags > 1) { y.diff.lag = z[, 2:lags] if (type == "nc") { res = lm(y.diff ~ y.lag.1 - 1 + y.diff.lag) } if (type == "c") { res = lm(y.diff ~ y.lag.1 + 1 + y.diff.lag) } if (type == "ct") { res = lm(y.diff ~ y.lag.1 + 1 + tt + y.diff.lag) } if (type == "ctt") { res = lm(y.diff ~ y.lag.1 + 1 + tt + tt^2 + y.diff.lag) } } else { if (type == "nc") { res = lm(y.diff ~ y.lag.1 - 1) } if (type == "c") { res = lm(y.diff ~ y.lag.1 + 1) } if (type == "ct") { res = lm(y.diff ~ y.lag.1 + 1 + tt) } if (type == "ctt") { res = lm(y.diff ~ y.lag.1 + 1 + tt + tt^2) } } res.sum = summary(res) test$regression = res.sum
if (type == "nc")
coefNum = 1
else coefNum = 2
STATISTIC <- res.sum$coefficients[coefNum, 1]/res.sum$coefficients[coefNum,
2]
names(STATISTIC) = "DF"
test$statistic = STATISTIC if (type == "nc") { itv = 1 } if (type == "c") { itv = 2 } if (type == "ct") { itv = 3 } if (type == "ctt") { itv = 4 } PVAL1 = .urcval(arg = STATISTIC, nobs = n, niv = 1, itt = 1, itv = itv, nc = 2) PVAL2 = .urcval(arg = STATISTIC, nobs = n, niv = 1, itt = 2, itv = itv, nc = 2) PVAL <- c(PVAL1, PVAL2) names(PVAL) = c("t", "n") test$p.value = PVAL
PARAMETER <- lags - 1
names(PARAMETER) <- "Lag Order"
test$parameter <- PARAMETER if (is.null(title)) title = "Augmented Dickey-Fuller Test" if (is.null(description)) description = date() new("fHTEST", call = CALL, data = list(x = x), test = test, title = as.character(title), description = description()) })  UnitrootTests R Documentation ## Unit Root Time Series Tests ### Description A collection and description of functions for unit root testing. The family of tests includes ADF tests based on Banerjee’s et al. tables and on J.G. McKinnons’ numerical distribution functions. The functions are:  adfTest Augmented Dickey--Fuller test for unit roots, the same based on McKinnons's test statistics.  unitrootTest ### Usage unitrootTest(x, lags = 1, type = c("nc", "c", "ct"), title = NULL, description = NULL) adfTest(x, lags = 1, type = c("nc", "c", "ct"), title = NULL, description = NULL)  ### Arguments  description a character string which allows for a brief description. lags the maximum number of lags used for error term correction. title a character string which allows for a project title. type a character string describing the type of the unit root regression. Valid choices are “nc” for a regression with no intercept (constant) nor time trend, and “c” for a regression with an intercept (constant) but no time trend, “ct” for a regression with an intercept (constant) and a time trend. The default is “c”. x a numeric vector or time series object. ### Details The function adfTest() computes test statistics and p values along the implementation from Trapletti’s augmented Dickey–Fuller test for unit roots. In contrast to Trapletti’s function three kind of test types can be selected. The function unitrootTest() computes test statistics and p values using McKinnon’s response surface approach. ### Value The tests return an object of class “fHTEST” with the following slots:  @call the function call. @data a data frame with the input data. @data.name a character string giving the name of the data frame. @test a list object which holds the output of the underlying test function. @title a character string with the name of the test. @description a character string with a brief description of the test. The entries of the @test slot include the following components:  $statistic the value of the test statistic. $parameter the lag order. $p.value the p-value of the test. $method a character string indicating what type of test was performed. $data.name a character string giving the name of the data. $alternative a character string describing the alternative hypothesis. $name the name of the underlying function, which may be wrapped. $output additional test results to be printed. ### Author(s) Adrian Trapletti for the tests adapted from R’s “tseries” package, Diethelm Wuertz for the Rmetrics R-port. ### References Banerjee A., Dolado J.J., Galbraith J.W., Hendry D.F. (1993); Cointegration, Error Correction, and the Econometric Analysis of Non-Stationary Data, Oxford University Press, Oxford. Dickey, D.A., Fuller, W.A. (1979); Distribution of the estimators for autoregressive time series with a unit root, Journal of the American Statistical Association 74, 427–431. MacKinnon, J.G. (1996); Numerical distribution functions for unit root and cointegration tests, Journal of Applied Econometrics 11, 601–618. Said S.E., Dickey D.A. (1984); Testing for Unit Roots in Autoregressive-Moving Average Models of Unknown Order, Biometrika 71, 599–607. ### Examples ## Time Series # A time series which contains no unit-root: x = rnorm(1000) # A time series which contains a unit-root: y = cumsum(c(0, x)) ## adfTest - adfTest(x) adfTest(y) ## unitrootTest - unitrootTest(x) unitrootTest(y)  #### pp.test list(function (x, alternative = c("stationary", "explosive"), type = c("Z(alpha)", "Z(t_alpha)"), lshort = TRUE) { if ((NCOL(x) > 1) || is.data.frame(x)) stop("x is not a vector or univariate time series") type <- match.arg(type) alternative <- match.arg(alternative) DNAME <- deparse(substitute(x)) x <- as.vector(x, mode = "double") z <- embed(x, 2) yt <- z[, 1] yt1 <- z[, 2] n <- length(yt) tt <- (1:n) - n/2 res <- lm(yt ~ 1 + tt + yt1) if (res$rank < 3)
stop("Singularities in regression")
res.sum <- summary(res)
u <- residuals(res)
ssqru <- sum(u^2)/n
if (lshort)
l <- trunc(4 * (n/100)^0.25)
else l <- trunc(12 * (n/100)^0.25)
ssqrtl <- .C(tseries_pp_sum, as.vector(u, mode = "double"),
as.integer(n), as.integer(l), ssqrtl = as.double(ssqru))$ssqrtl n2 <- n^2 trm1 <- n2 * (n2 - 1) * sum(yt1^2)/12 trm2 <- n * sum(yt1 * (1:n))^2 trm3 <- n * (n + 1) * sum(yt1 * (1:n)) * sum(yt1) trm4 <- (n * (n + 1) * (2 * n + 1) * sum(yt1)^2)/6 Dx <- trm1 - trm2 + trm3 - trm4 if (type == "Z(alpha)") { alpha <- res.sum$coefficients[3, 1]
STAT <- n * (alpha - 1) - (n^6)/(24 * Dx) * (ssqrtl -
ssqru)
table <- cbind(c(22.5, 25.7, 27.4, 28.4, 28.9, 29.5),
c(19.9, 22.4, 23.6, 24.4, 24.8, 25.1), c(17.9, 19.8,
20.7, 21.3, 21.5, 21.8), c(15.6, 16.8, 17.5,
18, 18.1, 18.3), c(3.66, 3.71, 3.74, 3.75, 3.76,
3.77), c(2.51, 2.6, 2.62, 2.64, 2.65, 2.66),
c(1.53, 1.66, 1.73, 1.78, 1.78, 1.79), c(0.43, 0.65,
0.75, 0.82, 0.84, 0.87))
}
else if (type == "Z(t_alpha)") {
tstat <- (res.sum$coefficients[3, 1] - 1)/res.sum$coefficients[3,
2]
STAT <- sqrt(ssqru)/sqrt(ssqrtl) * tstat - (n^3)/(4 *
sqrt(3) * sqrt(Dx) * sqrt(ssqrtl)) * (ssqrtl - ssqru)
table <- cbind(c(4.38, 4.15, 4.04, 3.99, 3.98, 3.96),
c(3.95, 3.8, 3.73, 3.69, 3.68, 3.66), c(3.6, 3.5,
3.45, 3.43, 3.42, 3.41), c(3.24, 3.18, 3.15,
3.13, 3.13, 3.12), c(1.14, 1.19, 1.22, 1.23,
1.24, 1.25), c(0.8, 0.87, 0.9, 0.92, 0.93, 0.94),
c(0.5, 0.58, 0.62, 0.64, 0.65, 0.66), c(0.15, 0.24,
0.28, 0.31, 0.32, 0.33))
}
else stop("irregular type")
table <- -table
tablen <- dim(table)
tableT <- c(25, 50, 100, 250, 500, 100000)
tablep <- c(0.01, 0.025, 0.05, 0.1, 0.9, 0.95, 0.975, 0.99)
tableipl <- numeric(tablen)
for (i in (1:tablen)) tableipl[i] <- approx(tableT, table[,
i], n, rule = 2)$y interpol <- approx(tableipl, tablep, STAT, rule = 2)$y
if (is.na(approx(tableipl, tablep, STAT, rule = 1)\$y))
if (interpol == min(tablep))
warning("p-value smaller than printed p-value")
else warning("p-value greater than printed p-value")
if (alternative == "stationary")
PVAL <- interpol
else if (alternative == "explosive")
PVAL <- 1 - interpol
else stop("irregular alternative")
PARAMETER <- l
METHOD <- "Phillips-Perron Unit Root Test"
names(STAT) <- paste("Dickey-Fuller", type)
names(PARAMETER) <- "Truncation lag parameter"
structure(list(statistic = STAT, parameter = PARAMETER, alternative = alternative,
p.value = PVAL, method = METHOD, data.name = DNAME),
class = "htest")
})
 pp.test R Documentation

## Phillips–Perron Unit Root Test

### Description

Computes the Phillips-Perron test for the null hypothesis that x has a unit root.

### Usage

pp.test(x, alternative = c("stationary", "explosive"),
type = c("Z(alpha)", "Z(t_alpha)"), lshort = TRUE)


### Arguments

 x a numeric vector or univariate time series. alternative indicates the alternative hypothesis and must be one of “stationary” (default) or “explosive”. You can specify just the initial letter. type indicates which variant of the test is computed and must be one of “Z(alpha)” (default) or “Z(t_alpha)”. lshort a logical indicating whether the short or long version of the truncation lag parameter is used.

### Details

The general regression equation which incorporates a constant and a linear trend is used and the Z(alpha) or Z(t_alpha) statistic for a first order autoregressive coefficient equals one are computed. To estimate sigma^2 the Newey-West estimator is used. If lshort is TRUE, then the truncation lag parameter is set to trunc(4(n/100)^0.25), otherwise trunc(12(n/100)^0.25) is used. The p-values are interpolated from Table 4.1 and 4.2, p. 103 of Banerjee et al. (1993). If the computed statistic is outside the table of critical values, then a warning message is generated.

Missing values are not handled.

### Value

A list with class “htest” containing the following components:

 statistic the value of the test statistic. parameter the truncation lag parameter. p.value the p-value of the test. method a character string indicating what type of test was performed. data.name a character string giving the name of the data. alternative a character string describing the alternative hypothesis.

A. Trapletti

### References

A. Banerjee, J. J. Dolado, J. W. Galbraith, and D. F. Hendry (1993): Cointegration, Error Correction, and the Econometric Analysis of Non-Stationary Data, Oxford University Press, Oxford.

P. Perron (1988): Trends and Random Walks in Macroeconomic Time Series. Journal of Economic Dynamics and Control 12, 297–332.

adf.test
x <- rnorm(1000)  # no unit-root