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.

[Tex]SMA_t = \frac{X_{t−1} + X_{t−2} +\cdots + X_{t−n}}{n} [/Tex]

where

  • [Tex]SMA_t [/Tex] is the simple moving average at the time
  • [Tex]X_{t−1},X_{t−2},\cdots,X_{t−n} [/Tex] 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.

[Tex]EMA_t=\alpha X_t+(1−\alpha)EMA_{t−1}[/Tex]

where

  • [Tex]EMA_t [/Tex] is the exponential moving average at time t
  • [Tex]X_t [/Tex] is the current value
  • [Tex]EMA_{t−1} [/Tex] 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:

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:

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.



Moving Averages in R

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.

Similar Reads

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....