Skip to content

Time Series and Exponential Smoothing

Question 1

a. Construct a time series plot using R.

install.packages("ggplot2")
install.packages("tidyr")
library(ggplot2)
library(tidyr)
month <- c("Jan", "Feb", "March", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
charges_2012 <- c(31.9, 27, 31.3, 31, 39.4, 40.7, 42.3, 49.5, 45, 50, 50.9, 58.5)
charges_2013 <- c(39.4, 36.2, 40.5, 44.6, 46.8, 44.7, 52.2, 54, 48.8, 55.8, 58.7, 63.4)
data <- data.frame(
Month = month,
Charges_2012 = charges_2012,
Charges_2013 = charges_2013
)
data_long <- pivot_longer(data, cols = starts_with("Charges"),
names_to = "Year", values_to = "Charges")
ggplot(data_long, aes(x = Month, y = Charges, group = Year, color = Year)) +
geom_line(size = 1) +
geom_point(size = 2) +
labs(title = "Monthly Charges for Student Credit Card (2012 vs 2013)",
x = "Month", y = "Charges") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

charges_2012 <- c(31.9, 27, 31.3, 31, 39.4, 40.7, 42.3, 49.5, 45, 50, 50.9, 58.5)
charges_2013 <- c(39.4, 36.2, 40.5, 44.6, 46.8, 44.7, 52.2, 54, 48.8, 55.8, 58.7, 63.4)
model_2012 <- HoltWinters(charges_2012, beta = FALSE, gamma = FALSE)
fitted_2012 <- model_2012$fitted[, "xhat"]
model_2013 <- HoltWinters(charges_2013, beta = FALSE, gamma = FALSE)
fitted_2013 <- model_2013$fitted[, "xhat"]
cat("2012 Alpha (Smoothing Level):", model_2012$alpha, "\n")
cat("2012 SSE (Sum of Squared Errors):", sum(model_2012$residuals^2), "\n")
cat("2013 Alpha (Smoothing Level):", model_2013$alpha, "\n")
cat("2013 SSE (Sum of Squared Errors):", sum(model_2013$residuals^2), "\n")

Results: 2012 Alpha (Smoothing Level): 0.9033038 2012 SSE (Sum of Squared Errors): 0 2013 Alpha (Smoothing Level): 0.9689135 2013 SSE (Sum of Squared Errors): 0

c. Provide a discussion on time series and Exponential Smoothing Model result you led to.

Exponential smoothing was applied to in order to model the time series data for each year. This method uses weighted average of past observations and assigns exponentially decreasing weights to older data points. The outcomes of this data was:

2012:

  • Smoothing level: .899
  • Sum of Squared Errors: 268.62

2013:

  • Smoothing level: .967
  • Sum of Squared Errors: 220.63

Smoothing Level:

Due to the smoothing levels being close to 1 for both years, it indicates the model gives significant weight to recency.

Sum of Squared Errors:

The Sum of Squared Errors is lower in 2013 than 2012, this likely indicates more consistency from 2013.

Month20122013
Jan31.939.4
Feb27.036.2
March31.340.5
Apr31.044.6
May39.446.8
Jun40.744.7
Jul42.352.2
Aug49.554.0
Sep45.048.8
Oct50.055.8
Nov50.958.7
Dec58.563.4