# Augmented Dickey-Fuller Test with R

Last Update: December 22, 2020

First order trend stationary time series consist of random processes that have constant mean which don’t exhibit trend pattern.

This topic is part of Pairs Trading Analysis with R course. Feel free to take a look at Course Curriculum.

This tutorial has an educational and informational purpose and doesn’t constitute any type of trading or investment advice. All content, including code and data, is presented for personal educational use exclusively and with no guarantee of exactness of completeness. Past performance doesn’t guarantee future results. Please read full Disclaimer.

Augmented Dickey-Fuller test [1] consists of evaluating whether time series was first order trend stationary with null hypothesis that it had a unit root and was not stationary.

1. Formula notation.

1.1. Augmented Dickey-Fuller test formula notation.

$\Delta&space;y_{t}=c+\beta&space;t+\gamma&space;y_{t-1}+\sum_{i=1}^{p}\delta_{i}\Delta&space;y_{t-i}+e_{t}$

Where $\Delta&space;y_{t}=y_{t}-y_{t-1}$ = current period asset prices difference, $c$ = regression constant term, $\beta,\gamma,\delta_{i}$ = regression coefficients, $t$ = linear trend variable, $y_{t-1}$ = previous period asset price, $\Delta&space;y_{t-i}$ = previous periods asset prices differences, $p$ = number of lags included within test, $e_{t}$ = regression residuals or forecasting errors.

1.2. Augmented Dickey-Fuller test formula notation constant and linear trend variable assumptions options.

$i)\;c=0\;and\;\beta=0$

$ii)\;c\neq0\;and\;\beta=0$

$iii)\;c\neq0\;and\;\beta\neq0$

Where $c$ = regression constant term, $\beta$ = linear trend variable regression coefficient.

1.3. Augmented Dickey-Fuller test.

Augmented Dickey-Fuller individual test $\gamma$ coefficient t-statistic approximated $p-value$:

• If Augmented Dickey-Fuller individual test $\gamma$ coefficient t-statistic approximated $p-value<\alpha$ level of statistical significance then time series was first order trend stationary with $(1-\alpha)$ level of statistical confidence.
• If Augmented Dickey-Fuller individual test $\gamma$ coefficient t-statistic approximated $p-value>\alpha$ level of statistical significance then higher differentiation order needed for first order trend stationary time series with $(1-\alpha)$ level of statistical confidence.

2. R script code example.

2.1. Load R packages [2].

library('quantmod')
library('tseries')


2.2. Augmented Dickey-Fuller test data reading, training and testing ranges delimiting.

• Data: MSCI® Germany index replicating ETF (ticker symbol: EWG) daily adjusted close prices (2007-2016).
• Training and testing ranges delimiting not fixed and only included for educational purposes.
data <- read.csv('Augmented-Dickey-Fuller-Test-Data.txt',header=T)
data <- xts(data[,2],order.by=as.Date(data[,1]))
colnames(data) <- 'ger'

tdata <- data['::2014-12-31']
fdata <- data['2015-01-02::']


2.3. Augmented Dickey-Fuller test prices chart.

• Augmented Dickey-Fuller test prices chart within training range.
tger <- tdata
plot(tger,main='tger Prices Chart')


2.4. Augmented Dickey-Fuller test calculation and output.

• Augmented Dickey-Fuller test calculation within training range.
• Augmented Dickey-Fuller test function includes constant and linear trend variable by default.
• Augmented Dickey-Fuller test function alternative hypothesis and lag order to calculate test statistic not fixed and only included for educational purposes.
In:

Out:
Augmented Dickey-Fuller Test

data:  tger
Dickey-Fuller = -1.785, Lag order = 1, p-value = 0.6694
alternative hypothesis: stationary

##### 3. References.

[1] David A. Dickey and Wayne A. Fuller. “Distribution of the Estimators for Autoregressive Time Series with a Unit Root”. Journal of the American Statistical Association. 1979

[2] Jeffrey A. Ryan and Joshua M. Ulrich. “quantmod: Quantitative Financial Modelling Framework”. R package version 0.4-17. 2020.

Adrian Trapletti and Kurt Hornik. “tseries: Time Series Analysis and Computational Finance”. R package version 0.10-47. 2019.

+