Unit-root testing with unitroottools

This vignette shows the main functions of unitroottools with one data set: the logarithm of U.S. real GDP, 1947Q1–2012Q4, used by Enders to illustrate the testing of unit roots when the deterministic terms are unknown. The data are included in the package.

library(unitroottools)

gdp <- read.csv(system.file("fixtures", "enders_rgdp.csv", package = "unitroottools"))
lgdp <- ts(log(gdp$rgdp), start = c(1947, 1), frequency = 4)
plot(lgdp, ylab = "log real GDP")

1. The augmented Dickey-Fuller test

The ADF test regression is

Δyt=a+γt+byt−1+∑j=1kβjΔyt−j+εt, \Delta y_t = a + \gamma t + b\, y_{t-1} + \sum_{j=1}^{k} \beta_j \Delta y_{t-j} + \varepsilon_t ,

and the null hypothesis of a unit root is H0:b=0H_0: b = 0 against H1:b<0H_1: b < 0. The trend argument chooses the deterministic terms: "n" (none), "c" (constant) or "ct" (constant and trend).

adf(lgdp, trend = "ct", lags = 1)
#> Augmented Dickey-Fuller test (tau3)
#> 
#>   H0: unit root
#>   H1: stationary around a linear trend
#>   Deterministics: constant and linear trend
#>   Lags: 1 (fixed)
#>   Effective sample: N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)
#> 
#>   Statistic:        -1.489
#>   Critical values:  1%: -3.994   5%: -3.427   10%: -3.137
#>   Source:           MacKinnon (2010) response surface
#>   p-value:          0.831
#> 
#>   Decision at 5%: do not reject H0 (unit root)

Every result states the hypotheses in words, the deterministic terms, how many lags were used and how they were chosen, the effective sample, the critical values and where they come from, the p-value, and the decision. The critical values are MacKinnon’s, computed for the actual sample size rather than read from a table; the p-value (MacKinnon, 1996) is computed for the same sample size, so the two agree closely (to about four decimals at the tabulated levels). The decision is taken from the critical value.

summary() adds the accompanying ϕ\phi statistics, with their own critical values, and the test regression:

summary(adf(lgdp, trend = "ct", lags = 1))
#> Augmented Dickey-Fuller test (tau3)
#> 
#>   H0: unit root
#>   H1: stationary around a linear trend
#>   Deterministics: constant and linear trend
#>   Lags: 1 (fixed)
#>   Effective sample: N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)
#> 
#>   Statistic:        -1.489
#>   Critical values:  1%: -3.994   5%: -3.427   10%: -3.137
#>   Source:           MacKinnon (2010) response surface
#>   p-value:          0.831
#> 
#>   Decision at 5%: do not reject H0 (unit root)
#> 
#> Calibration
#>   Method: response_surface
#>   Source: MacKinnon (2010) response surface
#>   Note:   evaluated at N = 262
#>   p-value: 0.831 [MacKinnon (1996) finite-sample p-value (urca::punitroot)]
#> 
#> Accompanying statistics
#>   phi2       17.612   1%: 6.238   5%: 4.751   10%: 4.071   p = 0.001  reject
#>                       [unitroottools phi response surfaces (own simulation), T = 264, k = 1]
#>   phi3        2.966   1%: 8.445   5%: 6.337   10%: 5.381   p = 0.471  do not reject
#>                       [unitroottools phi response surfaces (own simulation), T = 264, k = 1]
#> 
#> Residual autocorrelation (Breusch-Godfrey F): order 4 p = 0.235, order 15 p = 0.111 [details: residual_check()]
#> 
#> Test regression
#>           Estimate Std. Error t value
#> const    0.1247701  7.911e-02   1.577
#> trend    0.0001117  8.506e-05   1.313
#> y_lag1  -0.0156305  1.050e-02  -1.489
#> dy_lag1  0.3662619  5.848e-02   6.263
#> 
#> Engine: internal 0.3.2

2. Choosing the number of lags

If lags is not given, the number of lagged differences is chosen by an information criterion; the default is the modified AIC (MAIC) of Ng and Perron (2001). The candidates are k=0,…,kmaxk = 0, \dots, k_{\max}, with Schwert’s rule kmax=⌊12(T/100)1/4⌋k_{\max} = \lfloor 12 (T/100)^{1/4} \rfloor unless you give max_lags. All candidates are compared on the same observations, and the whole table is shown, not only the winner:

select_lags(lgdp, trend = "ct")
#> Lag selection for the ADF regression (MAIC)
#> 
#>   Deterministics: constant and linear trend
#>   Common sample:  N = 248, 1951 Q1 to 2012 Q4 (t = 17, ..., 264), kmax = 15
#>   MAIC/MBIC detrending: OLS
#> 
#>   k     AIC     BIC    MAIC    MBIC     
#>   0 -9.3745 -9.3745 -9.3475 -9.3474     
#>   1 -9.5008 -9.4866 -9.4774 -9.4480   <-
#>   2 -9.4990 -9.4707 -9.4727 -9.4202     
#>   3 -9.4959 -9.4534 -9.4712 -9.4102     
#>   4 -9.4906 -9.4339 -9.4660 -9.3936     
#>   5 -9.4882 -9.4174 -9.4641 -9.3822     
#>   6 -9.4828 -9.3978 -9.4577 -9.3564     
#>   7 -9.4766 -9.3774 -9.4516 -9.3382     
#>   8 -9.4687 -9.3553 -9.4433 -9.3155     
#>   9 -9.4668 -9.3393 -9.4388 -9.2869     
#>  10 -9.4598 -9.3181 -9.4294 -9.2572     
#>  11 -9.4520 -9.2962 -9.4215 -9.2355     
#>  12 -9.4785 -9.3085 -9.4512 -9.2689     
#>  13 -9.4714 -9.2872 -9.4434 -9.2474     
#>  14 -9.4644 -9.2660 -9.4355 -9.2199     
#>  15 -9.4603 -9.2478 -9.4310 -9.2050     
#> 
#>   Selected by MAIC: k = 1

adf() uses this selection automatically. Once the order is chosen, the test regression is re-estimated with all the observations available for that order (here, the first two are lost instead of kmax+1=16k_{\max} + 1 = 16); use final_sample = "common" to keep the sample of the selection table instead.

adf(lgdp, trend = "ct")
#> Augmented Dickey-Fuller test (tau3)
#> 
#>   H0: unit root
#>   H1: stationary around a linear trend
#>   Deterministics: constant and linear trend
#>   Lags: 1 (MAIC, kmax = 15)
#>   Effective sample: N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)
#> 
#>   Statistic:        -1.489
#>   Critical values:  1%: -3.994   5%: -3.427   10%: -3.137
#>   Source:           MacKinnon (2010) response surface
#>   p-value:          0.831
#> 
#>   Decision at 5%: do not reject H0 (unit root)

3. Are the residuals white noise?

The lagged differences are there to absorb the serial correlation of Δyt\Delta y_t. Whatever the criterion chose, check that the residuals of the test regression are white noise. residual_check() gives the correlogram (autocorrelations, partial autocorrelations and the Ljung-Box QQ, as in EViews) and the Breusch-Godfrey test, which remains valid with lagged dependent variables. With quarterly data it looks at 3s+3=153s + 3 = 15 lags, and runs Breusch-Godfrey at orders 4 (one year) and 15:

rc <- residual_check(adf(lgdp, trend = "ct"))
print(rc, max_lags = 8)
#> 
#> Residual check: Augmented Dickey-Fuller test (constant and linear trend), k = 1 
#>   N = 262 residuals; frequency s = 4; correlogram up to 15 lags
#> 
#>  Lag     AC    PAC        Q   Prob
#>    1 -0.040 -0.040     0.42      -
#>    2  0.124  0.123     4.52  0.034
#>    3 -0.041 -0.033     4.97  0.083
#>    4 -0.042 -0.060     5.44  0.142
#>    5 -0.117 -0.114     9.16  0.057
#>    6  0.001  0.004     9.16  0.103
#>    7 -0.037 -0.012     9.53  0.146
#>    8 -0.037 -0.051     9.90  0.194
#>   Approximate bands for AC and PAC: +/- 0.121 (1.96/sqrt(N)).
#>   Q p-values from chi-squared(h - 1), approximate in a regression with lagged dependent variables.
#> 
#>   Breusch-Godfrey test (residuals before the sample set to zero):
#>  Order       F        df Prob F  Obs*R2 Prob Chi2
#>      4   1.399  (4, 254)  0.235   5.648     0.227
#>     15   1.485 (15, 243)  0.111  21.995     0.108
#> 
#>   No evidence of residual autocorrelation at the 5% level (Breusch-Godfrey F).
plot(rc)

With the lag chosen by MAIC there is no evidence of autocorrelation. Without lagged differences there is, and the result warns about it:

adf(lgdp, trend = "ct", lags = 0)$warnings
#> [1] "Residual autocorrelation at the 5% level (Breusch-Godfrey F(4), p < 0.001): 0 lagged differences may not be enough; try a larger `lags`. See residual_check()."

summary() of a test reports the Breusch-Godfrey p-values in one line, and the history of ur_strategy() has a column with the p-value for each step’s regression. The number of lags is never increased automatically: if the residuals fail, try a larger lags yourself and compare.

4. Does the evidence change over time? Recursive and rolling tests

recursive_adf() computes the ADF statistic on samples that end later and later (fixed start), or on rolling windows of fixed width, with the lags chosen once on the full sample:

plot(recursive_adf(lgdp, trend = "ct"))

rr <- recursive_adf(lgdp, trend = "ct", window = "rolling")
rr
#> 
#> Rolling ADF test (constant and linear trend)
#>   Series: lgdp 
#>   Window width: 40 observations; 225 windows, ending 1956 Q4 to 2012 Q4
#>   Lags: MAIC on the full sample (kmax = 15), fixed
#>   Statistic: min -4.699, max 0.730, last -2.585
#>   Below the pointwise 5% critical value in 14 of 225 windows (first ending 1959 Q2, last ending 2000 Q1)
#> 
#>   Note: the critical values are nominal pointwise references (one date chosen
#>   in advance, fixed lag order); they are not valid for the minimum over dates
#>   (Banerjee, Lumsdaine and Stock 1992). The lag order uses the full sample (look-ahead).
plot(rr)

The dashed line is the 5% critical value for each window on its own (pointwise). Looking at many windows and asking whether the statistic crosses the line somewhere is a different test, with larger critical values (Banerjee, Lumsdaine and Stock 1992): here the rolling statistic crosses in about 6% of the windows, which is what one would expect by chance. Recursive and rolling statistics are a description of the data, not a test of when the series stopped having a unit root.

5. Which deterministic terms? Specification strategies

The ADF test depends on the deterministic terms, but we rarely know which ones belong in the regression. ur_strategy() implements three answers, and records every step.

The original procedure (Dolado, Jenkinson and Sosvilla-Rivero, as set out by Enders) starts with constant and trend and tests its way down:

ur_strategy(lgdp, "original", lags = 1)
#> Specification strategy: Original (Dolado-Jenkinson-Sosvilla-Rivero / Enders)
#> 
#>   Lags: 1 (fixed)
#>   Sample (all regressions): N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)   Level: 5%
#> 
#>   Step Regr.  Test       Statistic Crit.val.  p-value Resid. p  Decision    Next
#>   1    ct     tau3          -1.489    -3.427    0.831    0.235  not reject  test phi3
#>   2    ct     phi3           2.966     6.337    0.471    0.235  not reject  go to stage C
#>   3    c      tau2          -2.049    -2.873    0.266    0.219  not reject  test phi1
#>   4    c      phi1          25.485     4.631    0.001    0.219  reject      test constant, b = 0 imposed
#>   5    aux_c  t(const)       6.797     1.969  < 0.001    0.219  reject      conditional t test of b
#>   6    c      t(b) cond.    -2.049    -1.651    0.021    0.219  reject      stop
#>   Resid. p: Breusch-Godfrey F test of order 4 on the residuals of the step's regression.
#> 
#>   Specification: constant
#>   Conclusion:    no unit root: stationary around a non-zero mean
#> 
#>   Note: p-values are for each step on its own;
#>   no p-value or size is reported for the sequence as a whole.

It ends by rejecting the unit root with a conventional t test, so it concludes that log GDP is stationary around a constant mean. Enders himself calls this result nonsense: the procedure has removed the trend and then justified a t test by the presence of the constant.

The conservative variant stops when τ2\tau_2 does not reject, keeping the constant:

ur_strategy(lgdp, "conservative", lags = 1)
#> Specification strategy: Conservative
#> 
#>   Lags: 1 (fixed)
#>   Sample (all regressions): N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)   Level: 5%
#> 
#>   Step Regr.  Test       Statistic Crit.val.  p-value Resid. p  Decision    Next
#>   1    ct     tau3          -1.489    -3.427    0.831    0.235  not reject  test phi3
#>   2    ct     phi3           2.966     6.337    0.471    0.235  not reject  go to stage C
#>   3    c      tau2          -2.049    -2.873    0.266    0.219  not reject  stop
#>   Resid. p: Breusch-Godfrey F test of order 4 on the residuals of the step's regression.
#> 
#>   Specification: constant
#>   Conclusion:    unit root (constant retained)
#> 
#>   Note: p-values are for each step on its own;
#>   no p-value or size is reported for the sequence as a whole.

Hamilton’s rule does not test for the deterministic terms at all: you decide, by looking at the data and thinking about the economics, whether the series is trending. Log GDP clearly is:

ur_strategy(lgdp, "hamilton", trending = TRUE, lags = 1)
#> Specification strategy: Hamilton (rule-based)
#> 
#>   Lags: 1 (fixed)
#>   Sample (all regressions): N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)   Level: 5%
#> 
#>   Step Regr.  Test       Statistic Crit.val.  p-value Resid. p  Decision    Next
#>   1    ct     tau3          -1.489    -3.427    0.831    0.235  not reject  stop
#>   Resid. p: Breusch-Godfrey F test of order 4 on the residuals of the step's regression.
#> 
#>   Specification: constant and linear trend
#>   Conclusion:    unit root (with drift)
#> 
#>   Note: p-values are for each step on its own;
#>   no p-value or size is reported for the sequence as a whole.

There is no default for trending: the choice is yours, and the result says so. summary() on any strategy shows each step with its null hypothesis, the critical value and its source, and the reason for the next step. Note that the history separates rejections of the unit root from rejections that only decide the path (the ϕ\phi tests and the tests of the deterministic terms).

Each step has its own p-value. These p-values describe each test on its own. The procedure as a whole has no known size: it is a sequence of tests, each conditional on the results of the previous ones, so no p-value is given for it.

6. Other tests

The Phillips-Perron test corrects for serial correlation non-parametrically instead of adding lagged differences; DF-GLS detrends by GLS, which improves power; and the KPSS test reverses the null hypothesis: it tests stationarity against a unit root.

pp(lgdp, trend = "ct")
#> Phillips-Perron test (Z_tau)
#> 
#>   H0: unit root
#>   H1: stationary around a linear trend
#>   Deterministics: constant and linear trend
#>   Long-run variance: Bartlett kernel, 5 autocovariances (short rule, trunc(4 ((T-1)/100)^(1/4)))
#>   Effective sample: N = 263, 1947 Q2 to 2012 Q4 (t = 2, ..., 264)
#> 
#>   Statistic:        -1.111
#>   Critical values:  1%: -3.994   5%: -3.427   10%: -3.137
#>   Source:           MacKinnon (2010) response surface
#>   p-value:          0.924
#> 
#>   Decision at 5%: do not reject H0 (unit root)
dfgls(lgdp, trend = "ct")
#> DF-GLS test (tau_GLS)
#> 
#>   H0: unit root
#>   H1: stationary around a linear trend
#>   Deterministics: constant and linear trend
#>   Lags: 1 (MAIC, kmax = 15)
#>   Effective sample: N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)
#> 
#>   Statistic:        -1.019
#>   Critical values:  1%: -3.480   5%: -2.915   10%: -2.627
#>   Source:           unitroottools DF-GLS response surfaces (own simulation)
#>   p-value:          0.863
#> 
#>   Decision at 5%: do not reject H0 (unit root)
kpss(lgdp, trend = "ct")
#> KPSS test of stationarity (eta_tau)
#> 
#>   H0: stationary around a linear trend
#>   H1: unit root
#>   Deterministics: constant and linear trend
#>   Long-run variance: Bartlett kernel, 5 autocovariances (short rule, trunc(4 (T/100)^(1/4)))
#>   Effective sample: N = 264, 1947 Q1 to 2012 Q4 (t = 1, ..., 264)
#> 
#>   Statistic:        0.701
#>   Critical values:  1%: 0.218   2.5%: 0.177   5%: 0.148   10%: 0.119
#>   Source:           limiting distribution, computed numerically (kernel eigenvalues, Imhof inversion)
#>   p-value:          < 0.001
#> 
#>   Decision at 5%: reject H0 (stationary around a linear trend)

Two details of dfgls(). Without lags, the number of lagged differences is chosen as recommended by Perron and Qu (2007): by MAIC, exactly as for adf(), on the series detrended by ordinary least squares; the DF-GLS statistic is then computed with that number of lags. And its critical values and p-value come from this package’s own simulations: in samples of usual size the DF-GLS statistic has a heavier left tail than its limit distribution, and the asymptotic critical values used by other software reject a true unit root too often (about 8% of the time at the 5% level with T=100T = 100 and a constant).

The Ng-Perron M tests and the ERS point-optimal test use the same GLS detrending and lag choice; ng_perron() reports MZtMZ_t as the test and the other M statistics alongside:

ng_perron(lgdp, trend = "ct")
#> Ng-Perron MZt test (MZt)
#> 
#>   H0: unit root
#>   H1: stationary around a linear trend
#>   Deterministics: constant and linear trend
#>   Lags: 1 (MAIC, kmax = 15)
#>   Effective sample: N = 262, 1947 Q3 to 2012 Q4 (t = 3, ..., 264)
#> 
#>   Statistic:        -1.066
#>   Critical values:  1%: -3.438   5%: -2.893   10%: -2.615
#>   Source:           unitroottools GLS simulation tables (own simulation)
#>   p-value:          0.845
#> 
#>   Decision at 5%: do not reject H0 (unit root)

With the bootUR package installed, boot_ur() gives bootstrap versions of the ADF test (and the union-of-rejections test, union = TRUE); the decision uses the bootstrap p-value:

boot_ur(lgdp, trend = "ct", B = 499, seed = 1)
#> Bootstrap ADF test (autoregressive wild bootstrap, OLS detrending) (t_b)
#> 
#>   H0: unit root
#>   H1: stationary around a linear trend
#>   Deterministics: constant and linear trend
#>   Lags: 2 (MAIC (rescaled), kmax = 15)
#>   Effective sample: N = 261, 1947 Q4 to 2012 Q4 (t = 4, ..., 264)
#> 
#>   Statistic:        -1.920
#>   Critical values:  none (the decision uses the bootstrap p-value)
#>   Source:           bootUR 1.0.5, AWB, B = 499
#>   p-value:          0.643
#> 
#>   Decision at 5%: do not reject H0 (unit root)

A non-rejection is not evidence for the null. Using tests with both nulls is one way to see how strong the evidence is in each direction.

7. Collecting results in a table

glance() turns a test into one row of a data frame and tidy() gives one row per statistic, so results are easy to compare or to save. For example, the four tests on log GDP with constant and trend:

tests <- list(adf(lgdp, trend = "ct"), pp(lgdp, trend = "ct"),
              dfgls(lgdp, trend = "ct"), kpss(lgdp, trend = "ct"))
res <- do.call(rbind, lapply(tests, glance))
res[, c("test", "statistic_name", "statistic", "p_value", "reject", "lags", "nobs")]
#>     test statistic_name  statistic   p_value reject lags nobs
#> 1    ADF           tau3 -1.4891169 0.8311624  FALSE    1  262
#> 2     PP          Z_tau -1.1105222 0.9243191  FALSE    5  263
#> 3 DF-GLS        tau_GLS -1.0192578 0.8625359  FALSE    1  262
#> 4   KPSS        eta_tau  0.7007647 0.0010000   TRUE    5  264

tidy() shows the accompanying statistics as well, for instance the ϕ\phi statistics of the ADF test:

tidy(adf(lgdp, trend = "ct"))[, c("statistic_name", "statistic", "critical_value", "p_value", "reject")]
#>   statistic_name statistic critical_value   p_value reject
#> 1           tau3 -1.489117      -3.427381 0.8311624  FALSE
#> 2           phi2 17.611863       4.750761 0.0010000   TRUE
#> 3           phi3  2.965690       6.336793 0.4707509  FALSE

The same functions work on a strategy: glance() gives its outcome in one row and tidy() its history.

8. Why special critical values?

Under the unit-root null, the t ratio of bb does not have a normal or Student t distribution. simulate_df() shows this by simulation:

s <- simulate_df(T = 100, R = 2000, trend = "c", seed = 1)
s
#> Simulated Dickey-Fuller distribution of tau2
#> 
#>   Data: driftless random walk, T = 100 (N = 99); R = 2000 replications
#>   Test regression: constant
#> 
#>   Level   Simulated  MacKinnon (resp.)     N(0,1)
#>   1%         -3.459             -3.498     -2.326
#>   5%         -2.889             -2.891     -1.645
#>   10%        -2.556             -2.583     -1.282
#> 
#>   Share of simulated tau below the normal 5% quantile: 0.460
#>   (a correctly sized test would reject 0.050 +/- 0.010)
plot(s)

The simulated quantiles agree with MacKinnon’s critical values, which are far to the left of the normal ones. Using the normal 5% value would reject a true unit root much more often than 5% of the time.

9. Data formats

The functions accept numeric vectors, ts, zoo and xts series, data frames, tibbles and tsibbles. When there are several numeric columns, name the one to test with var:

df <- data.frame(date = as.Date(gdp$date), lgdp = log(gdp$rgdp),
                 growth = c(NA, diff(log(gdp$rgdp))))
adf(df, var = "lgdp", trend = "ct", lags = 1)$statistic
#> [1] -1.489117

Missing values are never dropped silently: the growth column above starts with NA, and testing it gives an error that says where the problem is.