The Augmented Dickey-Fuller (ADF) test is a statistical test used to determine whether a unit root is present in a time series dataset. A unit root represents that a time series is non-stationary, meaning its mean and variance change over time.

R code is always interesting.

Let me show you an example code on how you can perform an ADF test, using a built-in dataset (AirPassengers dataset).

# Load required libraries library(tseries) # For ADF test library(forecast) # For loading AirPassengers dataset # Load dataset data("AirPassengers") # Inspect the dataset head(AirPassengers) # Check if the time series is stationary adf.test(AirPassengers)

The code is simple, I’ll explain it.

- The tseries library contains the adf.test() function. This function performs the Augmented Dickey-Fuller test. The forecast library is used to load the AirPassengers dataset.
- Now load the AirPassengers dataset, which is a built-in dataset in R containing the monthly totals of international airline passengers from 1949 to 1960.
- It’s always good to inspect the dataset to understand its structure and content. You can use head() to view the first few rows of the dataset.
- The adf.test() function performs the actual ADF test on the time series data. It returns the test statistic and p-value.

The code generates the following output:

Augmented Dickey-Fuller Test data: AirPassengers Dickey-Fuller = -1.7306, Lag order = 9, p-value = 0.4133 alternative hypothesis: stationary

Let’s spend some more time to understand the output.

- The test statistic value (-1.7306 in this case).
- The number of lags used in the regression model (9 in this case).
- The p-value associated with the test statistic. In this case, it’s 0.4133.

This ADF test indicates whether the alternative hypothesis is “stationary” or “explosive”. Since the p-value is greater than the significance level of 0.05 (commonly used), we fail to reject the null hypothesis, suggesting that the time series is likely non-stationary.

You can apply the same kind of ADF test for any other dataset.