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)

Example R Code for Time Series Analysis

Simple Time Series Analysis

First, I will show a basic example of time series analysis in R using the built-in AirPassengers dataset.

The dataset contains monthly totals of international airline passengers from 1949 to 1960.

# Load necessary libraries
library(forecast)

# Load the AirPassengers dataset
data("AirPassengers")

# Convert the dataset to a time series object
passengers_ts <- ts(AirPassengers, frequency = 12, start = c(1949, 1))

# Plot the time series data
plot(passengers_ts, main = "International Airline Passengers", ylab = "Number of Passengers", xlab = "Year")

# Decompose the time series into trend, seasonal, and residual components
decomposed_ts <- decompose(passengers_ts)

# Plot the decomposed components
plot(decomposed_ts)

Let me explain each and every line of the above code.

  • Start by loading the forecast library, which contains functions for time series analysis.
  • Load the AirPassengers dataset, which is included in R. This dataset contains the monthly totals of international airline passengers.
  • Convert the dataset to a time series object using the ts() function. We specify that the data has a frequency of 12 (monthly data) and starts in January 1949.
  • Plot the time series data using the plot() function. This gives us a visual representation of the number of international airline passengers over time.
  • Decompose the time series into its trend, seasonal, and residual components using the decompose() function. This helps us understand the underlying patterns in the data.
  • Plot the decomposed components using the plot() function. This allows us to visualize the trend, seasonal, and residual components separately.

This is a basic example to get you started with time series analysis in R. Depending on your specific analysis goals, you may want to explore further techniques such as forecasting or modeling.

Time Series Analysis using ARIMA

Now I’ll take you into time series analysis with an example that includes forecasting using the ARIMA (AutoRegressive Integrated Moving Average) model.

Let’s use the same AirPassengers dataset for this example.

# Load necessary libraries
library(forecast)

# Load the AirPassengers dataset
data("AirPassengers")

# Convert the dataset to a time series object
passengers_ts <- ts(AirPassengers, frequency = 12, start = c(1949, 1))

# Plot the time series data
plot(passengers_ts, main = "International Airline Passengers", ylab = "Number of Passengers", xlab = "Year")

# Decompose the time series into trend, seasonal, and residual components
decomposed_ts <- decompose(passengers_ts)

# Plot the decomposed components
plot(decomposed_ts)

# Fit an ARIMA model to the data
arima_model <- auto.arima(passengers_ts)

# Print the model summary
print(summary(arima_model))

# Forecast future values using the ARIMA model
forecast_values <- forecast(arima_model, h = 24)  # Forecasting the next 24 months

# Plot the forecasted values
plot(forecast_values, main = "Forecasted International Airline Passengers", ylab = "Number of Passengers", xlab = "Year")

Again, I will explain the steps in detail.

  • Load the forecast library, which contains functions for time series analysis.
  • Load the AirPassengers dataset, which contains the monthly totals of international airline passengers.
  • Convert the dataset to a time series object using the ts() function.
  • Plot the time series data to visualize the number of international airline passengers over time.
  • Decompose the time series into its trend, seasonal, and residual components using the decompose() function. This helps us understand the underlying patterns in the data.
  • Plot the decomposed components to visualize the trend, seasonal, and residual components separately.
  • Use the auto.arima() function to automatically fit an ARIMA model to the time series data.
  • Print a summary of the ARIMA model to see the details of the fitted model.
  • Use the forecast() function to forecast future values of the time series using the fitted ARIMA model. Here, we forecast the next 24 months.
  • Plot the forecasted values to visualize the predicted trend of international airline passengers for the next 24 months.

In simple terms, this example shows how to fit an ARIMA model to time series data, make forecasts, and visualize the results.

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 Perform t-tests for Statistical Analysis
Up Next
R Code for Doing Web Scraping with Examples
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.