In this vignette, we illustrate how to apply the pre-trained unitrootML unit root test to a set of time series.
Set up requires installation from the Github page
#Load package library(unitrootML) library(forecast) library(ggplot2)
For this example, we will simulate five time series: two with a unit root (gamma = 1
) and three without a unit root (gamma < 1
). These time series are stored in a list object. Let's take a look at a few of these time series using autoplot
from the forecast package and arranged into a grid using grid.arrange
.
#Example time series set.seed(123) out <- list(series1 = ts(dgp_enders1(gamma = 1), freq = 12, start = c(2010,1)), series2 = ts(dgp_enders1(gamma = 1), freq = 12, start = c(2010,1)), series3 = ts(dgp_enders1(gamma = 0.98), freq = 12, start = c(2010,1)), series4 = ts(dgp_enders1(gamma = 0.8), freq = 12, start = c(2010,1)), series5 = ts(dgp_enders1(gamma = 0.5), freq = 12, start = c(2010,1))) #Plot gridExtra::grid.arrange(autoplot(out[[1]]), autoplot(out[[2]]), autoplot(out[[3]]), autoplot(out[[4]]), autoplot(out[[5]]), ncol = 2)
To screen these time series, apply the ml_test_all
function, which expects a list object containing time series objects (bank =
) and apvalue = 0.05
to return results evaluated at a 5% level.
res <- ml_test(bank = out, pvalue = 0.05)
The res
object captures the outputs of a battery of tests, including pre-trained ML-based unit root tests. The first element summarizes the verdict that each unit root test reached when evaluating each time series ("yes" = "unit root"). While each of the traditional tests may disagree, it is worth noting that the tests built on Random Forest (verdict_ranger
) and Gradient Boosting (verdict_xgbTree
) offer markedly improved diagnostic accuracy.
verdicts <- res$verdicts print(verdicts[,1:5])
The test statistics and their critical thresholds are recorded in the results
element.
results <- res$results print(results[,1:5])
Lastly, all the input features for the ML-based tests that were calculated from the time series list are available in the features
element.
feats <- res$features feats[,1:4]
For more in-depth background on the construction and properties of the ML-based unit root test, read Cornwall, Chen, and Sauley (2021).
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.