Load the Following Libraries

library(tidyverse) 
library(tsibble) 
library(feasts) 
library(lubridate) 
library(fpp3) 
library(kableExtra)
library(ggplot2)
library(ggpubr)
library(TTR)
library(TTR)
library(xts)
library(lmtest)
library(base)
library(forecast)
library(ggdist)
library(ggthemes)

Rstudio Assignment on Public Expenditure

The data below lists West Germany’s public expenditure (in billion D-Marks) between 1961 and 1990. Compute a simple moving average of order 3, 5 and 10 to estimate the possible trend. Plot the original as well as the filtered ones and compare and comment on the curves.

data <- read_csv("expenditure.csv")
data
# A tibble: 30 × 2
    Year `Public Expenditures`
   <dbl>                 <dbl>
 1  1961                  113.
 2  1962                  130.
 3  1963                  140.
 4  1964                  153.
 5  1965                  170.
 6  1966                  182.
 7  1967                  194.
 8  1968                  211.
 9  1969                  133.
10  1970                  264.
# ℹ 20 more rows

Fit the Moving Averages

# Load necessary libraries
library(zoo)
library(ggplot2)

Calculating SMAs of orders 3, 5, and 10

data$SMA_3 <- rollmean(data$`Public Expenditures`, k = 3, fill = NA)
data$SMA_5 <- rollmean(data$`Public Expenditures`, k = 5, fill = NA)
data$SMA_10 <- rollmean(data$`Public Expenditures`, k = 10, fill = NA)

View the data

data
# A tibble: 30 × 5
    Year `Public Expenditures` SMA_3 SMA_5 SMA_10
   <dbl>                 <dbl> <dbl> <dbl>  <dbl>
 1  1961                  113.   NA    NA     NA 
 2  1962                  130.  128.   NA     NA 
 3  1963                  140.  141.  141.    NA 
 4  1964                  153.  155.  155     NA 
 5  1965                  170.  168.  168.   169.
 6  1966                  182.  182.  182.   188.
 7  1967                  194.  195.  178.   209.
 8  1968                  211.  179.  197.   234.
 9  1969                  133.  203.  221.   263.
10  1970                  264.  233.  250.   296.
# ℹ 20 more rows

Plotting

data |>
  ggplot(aes(x = Year))+
  geom_line(aes(y = data$`Public Expenditures`, color = "Original Data"))+
  geom_line(aes(y = data$SMA_3, color = "SMA3"))+
  geom_line(aes(y = data$SMA_5, color = "SMA5"))+
  geom_line(aes(y = data$SMA_10, color = "SMA10"))+
  labs(y = "Public Expenditure", 
       x = "Time (Years)",
       title = "A Time Series Plot of the Original Data and Simple Moving Averages")

Alternatively

Declare the data time series

public.ts <- ts(data$`Public Expenditures`, start = c(1961), end = c(1990), frequency = 1)
plot(public.ts, xlab = "Years", ylab= "Public Expenditure", main = "Time Series Plot")

Load the Forecast Libraries

library(forecast)

Simple Moving Average for the Model window with order 3

SMA.3 <- forecast::ma(public.ts, order = 3, centre = TRUE)

Plot the Original Series

plot(public.ts, xlab = "Years", ylab= "Public Expenditure", main = "Time Series Plot")
lines(SMA.3, col = "blue", lwd = 2)

Simple Moving Average for the Model window with order 5

SMA.5 <- forecast::ma(public.ts, order = 5, centre = TRUE)

Plot the Original and SMA.10

plot(public.ts, xlab = "Years", ylab= "Public Expenditure", main = "Time Series Plot")

lines(SMA.5, col = "red", lwd = 2)

Simple Moving Average for the Model window with order 10

SMA.10 <- forecast::ma(public.ts, order = 10, centre = TRUE)

Plot the Original Series and SMA.10

plot(public.ts, xlab = "Years", ylab= "Public Expenditure", main = "Time Series Plot")
lines(SMA.10, col = "steelblue", lwd = 2)

Plot the Original Series and the Simple Moving Averages

public.ts <- ts(data$`Public Expenditures`, start = c(1961), end = c(1990), frequency = 1)
plot(public.ts, xlab = "Years", ylab= "Public Expenditure", main = "Time Series Plot")
lines(SMA.3, col = "blue", lwd = 2)
lines(SMA.5, col = "red", lwd = 2)
lines(SMA.10, col = "steelblue", lwd = 2)

Comment

The comparison of simple moving averages (SMAs) of orders 3, 5, and 10 for trend estimation revealed distinct performance characteristics. While all SMAs aimed to smooth fluctuations and identify trends, the SMA of order 3 exhibited superior efficacy. In the plotted comparison, the SMA 3 managed to maintain a closer alignment with the original data’s trends, capturing shorter-term fluctuations while preserving the overall trajectory. On the other hand, the SMA 5 and SMA 10, despite their attempts to provide smoother trends, appeared to lag behind significant shifts and exhibited a delayed response to changes in the data. The SMA 3’s agility in tracking short-term variations while outlining the broader trend hints at its suitability for this dataset, underscoring the importance of selecting the appropriate window size for accurate trend estimation in time series analysis.The comparison of simple moving averages (SMAs) of orders 3, 5, and 10 for trend estimation revealed distinct performance characteristics. While all SMAs aimed to smooth fluctuations and identify trends, the SMA of order 3 exhibited superior efficacy. In the plotted comparison, the SMA 3 managed to maintain a closer alignment with the original data’s trends, capturing shorter-term fluctuations while preserving the overall trajectory. On the other hand, the SMA 5 and SMA 10, despite their attempts to provide smoother trends, appeared to lag behind significant shifts and exhibited a delayed response to changes in the data. The SMA 3’s agility in tracking short-term variations while outlining the broader trend hints at its suitability for this dataset, underscoring the importance of selecting the appropriate window size for accurate trend estimation in time series analysis. We can relate the results to the graph below.

Orders (n = 30, n = 200)

knitr::include_graphics("order.png")

The Plot

knitr::include_graphics("SMA.png")