HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • R Code Examples
    • R Code to Create and Manipulate Vectors
    • R Code to Work with Data Frames
    • R Code to Handle Factors and Categorical Data
    • Example R Code for Basic Data Visualization with ggplot2
    • R Code to Aggregate Data Using dplyr
    • R Code to Apply Functions with lapply and sapply
    • R Code to Handle Missing Data
    • Example R Code for String Manipulation with stringr
    • R Code to Transform Data with tidyr
    • R Code to Perform ADF Test
    • R Code to Perform Data Import and Export with CSV
    • R Code for Filtering Data
    • R Code for Easily Summarizing Data
    • R Code to Perform Linear Regression for Statistical Analysis
    • R Code to Perform t-tests for Statistical Analysis
    • Example R Code for Time Series Analysis
    • R Code for Doing Web Scraping with Examples
    • R Code to Showcase Geospatial Analysis
    • Example R Code to Filter Multiple Conditions (for Data Manipulation)

R Code to Perform ADF Test

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Related Articles
  • R Code to Transform Data with tidyr
  • Example R Code for String Manipulation with stringr
  • R Code to Handle Missing Data
  • R Code to Apply Functions with lapply and sapply
  • R Code to Aggregate Data Using dplyr
  • Example R Code for Basic Data Visualization with ggplot2

No luck finding what you need? Contact Us

Previously
R Code to Transform Data with tidyr
Up Next
R Code to Perform Data Import and Export with CSV
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.