Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Moving Averages in R
Next article icon

Moving Averages in R

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Moving averages (MA) play a crucial role in time series analysis, providing a means to discern trends, patterns, and underlying structures within sequential data. In the context of R, a popular programming language for statistical computing, various functions and packages facilitate the computation and visualization of moving averages. This comprehensive guide delves into the theoretical foundations of moving averages, their types, and practical examples of their implementation in R.

What is the Moving average in R?

Moving averages are statistical calculations used to analyze data points over a specified time period. The primary purpose is to smooth out short-term fluctuations, making it easier to identify underlying trends or patterns in time series data.

Types of Moving Averages

Two main types of moving averages are shown below.

Simple Moving Average (SMA)

The simple moving average is a straightforward method that calculates the average of a set of values over a specified window. Mathematically, it is expressed as.

SMA_t = \frac{X_{t−1} + X_{t−2} +\cdots + X_{t−n}}{n}

where

  • SMA_t is the simple moving average at the time
  • X_{t−1},X_{t−2},\cdots,X_{t−n} are the data points within the specified window, and n is the window size.

Exponential Moving Average (EMA)

The exponential moving average gives more weight to recent observations, making it more responsive to changes. The formula for EMA is.

EMA_t=\alpha X_t+(1−\alpha)EMA_{t−1}

where

  • EMA_t is the exponential moving average at time t
  • X_t is the current value
  • EMA_{t−1} is the previous exponential moving average
  • α is the smoothing factor.

Applications of Moving Averages

Moving averages find applications in various fields, including finance, economics, signal processing, and environmental science. They are used to identify trends, seasonal patterns, and anomalies in time series data.

Implementing Moving Averages in R

Installing and Loading Required Packages

Before implementing moving averages in R, it's essential to install and load the necessary packages. The forecast, TTR, and zoo packages are commonly used for this purpose.

# Install and load required packages
install.packages("forecast")
library(forecast)

Creating Time Series Data

Generating or importing time series data is the first step. The data should be in a format compatible with time series analysis. In R, the ts function is often used to convert a numeric vector into a time series object.

# Create a sample time series
set.seed(123)
ts_data <- ts(rnorm(100), start = c(2020, 1), frequency = 12)

Calculating Moving Averages

Simple Moving Average:

The TTR::SMA function can be employed to calculate a simple moving average.

# Calculate a simple moving average with a window size of 3
sma_result <- TTR::SMA(ts_data, n = 3)

Exponential Moving Average:

The filter function from the base R stats package can be used for exponential moving averages.

# Calculate an exponential moving average with a smoothing factor of 0.2
ema_result <- stats::filter(ts_data, filter = 0.2, method = "recursive")

Visualizing Results

Once the moving averages are calculated, visualizing the results provides insights into the data trends.

# Plot the original time series and the moving average
plot(ts_data, col = "blue", main = "Time Series with Moving Average")
lines(sma_result, col = "red")

Example 1: Simple Moving Average in R

R
# Generate time series data set.seed(123) ts_data <- ts(rnorm(100), start = c(2020, 1), frequency = 12)  # Calculate a simple moving average with a window size of 3 sma_result <- TTR::SMA(ts_data, n = 3) sma_result 

Output:

              Jan          Feb          Mar          Apr          May          Jun
2020 NA NA 0.256018393 0.466346405 0.586168147 0.638287038
2021 0.661555692 0.290422665 -0.014795656 0.447251573 0.576307493 0.106048819
2022 -0.793311648 -1.013541269 -0.491315178 -0.231844383 -0.048992258 0.089683701
2023 0.688046330 0.393548732 0.062014426 -0.249448458 -0.460380215 -0.427698419
2024 -0.029858357 0.076646899 0.316638189 0.047134231 0.060633767 0.432395024
2025 0.239811765 0.031085866 -0.151963785 -0.618035407 -0.807857998 -0.595612656
2026 -0.598153839 -0.670877038 -0.130490285 -0.123879336 0.017596582 -0.159973117
2027 0.017743318 0.251890650 0.402711472 0.621267489 0.402029639 0.419352508
2028 0.982575285 1.039894677 1.161414420 0.090163122
Jul Aug Sep Oct Nov Dec
2020 0.768422976 0.303639986 -0.496999294 -0.799192019 0.030522325 0.379411218
2021 -0.255803592 -0.579350888 -0.279753071 -0.586196676 -0.770601023 -0.657623531
2022 0.180714069 0.461735887 0.342172800 0.492729222 0.864946743 0.796118274
2023 -0.722673536 0.231880779 0.703840537 0.751269793 -0.106010473 -0.664216257
2024 0.366653614 0.886433968 -0.086017728 0.184110517 -0.280094937 0.308136521
2025 -0.106684269 0.268247549 0.474493824 1.008452127 0.827106996 -0.250038452
2026 -0.441395747 -0.392768532 0.016058768 0.084051075 0.006794852 0.219665639
2027 0.605459963 0.896902811 0.593544184 0.053074206 0.323826036 0.044162262
2028

Plot the original time series and the moving average

R
# Plot the original time series and the moving average plot(ts_data, col = "blue", main = "Time Series with Simple Moving Average") lines(sma_result, col = "red") legend("topright", legend = c("Original", "Simple Moving Average"),         col = c("blue", "red"), lty = 1) 

Output:

gh
Simple Moving Averages (MA)
  • The code generates a synthetic time series with random values.
  • The simple moving average is then calculated using a window size of 3.
  • Finally, a plot is created to visualize the original time series and the calculated simple moving average.

The resulting plot helps in visually assessing how the simple moving average smoothens out short-term fluctuations, making it easier to identify trends or patterns in the time series data. The legend distinguishes between the original time series and the simple moving average, aiding in the interpretation of the plot.

Example 2: Exponential Moving Average in R

R
# Generate time series data set.seed(123) ts_data <- ts(rnorm(100), start = c(2020, 1), frequency = 12)  # Calculate an exponential moving average with a smoothing factor of 0.2 ema_result <- stats::filter(ts_data, filter = 0.2, method = "recursive") ema_result 

Output:

             Jan         Feb         Mar         Apr         May         Jun
2020 -0.56047565 -0.34227262 1.49025379 0.36855915 0.20299957 1.75566490
2021 0.51668038 0.21401879 -0.51303738 1.68430566 0.83471161 -1.79967483
2022 -0.81543945 -1.84978120 0.46783080 0.24693928 -1.08874908 1.03606510
2023 0.73291235 0.08467076 -0.28902851 -0.43827670 -0.78236232 -0.36438974
2024 0.66407494 0.04944592 0.26320770 0.02409478 -0.03805150 1.36099198
2025 0.43046911 -0.41622963 -0.41645331 -1.10186605 -1.29216444 0.04509575
2026 0.54218603 -0.60076356 -0.80816133 0.86393910 -0.11198519 -1.24311475
2027 -0.10339494 0.31110298 1.15905961 0.66699341 -0.19253290 1.11030104
2028 2.11731918 1.95607446 0.15551453 -0.99531799
Jul Aug Sep Oct Nov Dec
2020 0.81204919 -1.10265140 -0.90738313 -0.62713860 1.09865408 0.57954464
2021 0.34142093 -0.40450722 -1.14872515 -0.44771994 -1.11554844 -0.95200092
2022 0.63367724 -0.16833603 0.86145845 1.05042518 1.03166612 0.89497348
2023 -1.33827430 1.90130111 1.58822222 -0.80546414 -0.56397766 -0.57945089
2024 0.04642741 1.52575609 -1.24360159 0.33589343 0.19103293 0.25414815
2025 0.45722893 0.14445001 0.95115747 2.24031618 -0.04296793 -2.31776246
2026 -0.06731947 -0.15235526 -0.02470687 0.38033903 -0.29459223 0.58545810
2027 1.21556406 0.79150977 0.39703369 -0.54849934 1.25095258 -0.35006907
2028

Plot the original time series and the moving average

R
# Plot the original time series and the moving average plot(ts_data, col = "green", main = "Time Series with Exponential Moving Average") lines(ema_result, col = "yellow") legend("bottomleft", legend = c("Original", "Exponential Moving Average"),        col = c("green", "yellow"), lty = 1) 

Output:

gh
Exponential Moving Averages (MA)


Generate Time Series Data

  • set.seed(123): Sets the seed for reproducibility.
  • ts_data <- ts(rnorm(100), start = c(2020, 1), frequency = 12): Generates a time series ts_data with 100 monthly data points starting from January 2020, following a normal distribution.

Calculate Exponential Moving Average (EMA)

  • ema_result <- stats::filter(ts_data, filter = 0.2, method = "recursive"): Uses the filter function to calculate an exponential moving average (ema_result) with a smoothing factor of 0.2.
  • The "recursive" method ensures each data point is influenced by the previous EMA values.

Plotting the Time Series and EMA

  • plot(ts_data, col = "green", main = "Time Series with Exponential Moving Average"): Plots the original time series (ts_data) in green with a specified title.
  • lines(ema_result, col = "yellow"): Adds the exponential moving average (ema_result) to the plot in yellow.

Conclusion

In conclusion, moving averages are powerful tools in time series analysis, providing a means to discern trends, patterns, and fluctuations within sequential data. In this exploration of moving averages using R, we covered both the theory and practical implementation.

We began by understanding the foundational concepts, differentiating between the Simple Moving Average (SMA) and the Exponential Moving Average (EMA). SMA offers simplicity, calculating the average over a fixed window, while EMA provides responsiveness to recent observations through a weighted approach.


Next Article
Moving Averages in R

M

manojjai07nu
Improve
Article Tags :
  • Geeks Premier League
  • R Machine Learning
  • AI-ML-DS
  • Geeks Premier League 2023

Similar Reads

    Organising Data in R
    Organizing data is a fundamental step in data analysis and manipulation, and R Programming Language provides a powerful set of tools and techniques to help you efficiently structure and manage your data. Whether you're working with small datasets or massive datasets, understanding how to organize yo
    5 min read
    DataFrame Operations in R
    DataFrames are generic data objects of R which are used to store the tabular data. Data frames are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Data frames can also be taught as mattresses where each column of
    9 min read
    Add Moving Average Plot to Time Series Plot in R
    In time series analysis, a moving average is a widely used technique to smooth out short-term fluctuations and highlight longer-term trends or cycles. R provides several ways to compute and visualize moving averages alongside time series data. This article will guide you through the process of addin
    3 min read
    File Handling in R Programming
    In R programming, handling files (such as reading, writing, creating, and renaming files) can be done using built-in functions available in the base R package. These operations help in managing data stored in files, which is essential for tasks like data analysis, data manipulation, and automation.
    3 min read
    dcast() Function in R
    Reshaping data in R Programming Language is the process of transforming the structure of a dataset from one format to another. This transformation is done by the dcast function in R. dcast function in RThe dcast() function in R is a part of the reshape2 package and is used for reshaping data from 'l
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences