library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
library(httr)
# Load the built-in USAccDeaths dataset
data("USAccDeaths")
# Convert the dataset to a time series object
acc_deaths_ts <- ts(USAccDeaths, start = c(1973, 1), frequency = 12)
# Plot the monthly accidental deaths data
autoplot(acc_deaths_ts) +
ggtitle("Monthly Accidental Deaths in the US") +
xlab("Year") +
ylab("Number of Deaths")
# Fit ETS models
additive_model <- ets(acc_deaths_ts, model = "AAA")
multiplicative_model <- ets(acc_deaths_ts, model = "MAM")
no_trend_model <- ets(acc_deaths_ts, model = "ANA")
# Check the structure of the accuracy output
print(accuracy(additive_model))
## ME RMSE MAE MPE MAPE MASE
## Training set 7.651387 253.4873 200.3835 0.02844321 2.321291 0.4582814
## ACF1
## Training set 0.01341648
print(accuracy(multiplicative_model))
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 22.36998 247.086 194.7573 0.205294 2.234666 0.4454142 -0.008983535
print(accuracy(no_trend_model))
## ME RMSE MAE MPE MAPE MASE
## Training set -2.612086 262.698 202.6369 -0.08274519 2.32238 0.4634348
## ACF1
## Training set -0.007898621
# Extract RMSE values correctly
rmse_additive <- accuracy(additive_model)[, "RMSE"]
rmse_multiplicative <- accuracy(multiplicative_model)[, "RMSE"]
rmse_no_trend <- accuracy(no_trend_model)[, "RMSE"]
# Compare model performance using AIC, BIC, and RMSE
aic_values <- c(additive_model$aic, multiplicative_model$aic, no_trend_model$aic)
bic_values <- c(additive_model$bic, multiplicative_model$bic, no_trend_model$bic)
rmse_values <- c(rmse_additive[2], rmse_multiplicative[2], rmse_no_trend[2])
model_performance <- data.frame(
Model = c("Additive", "Multiplicative", "No Trend"),
AIC = aic_values,
BIC = bic_values,
RMSE = rmse_values
)
print(model_performance)
## Model AIC BIC RMSE
## 1 Additive 1141.005 1181.985 NA
## 2 Multiplicative 1140.126 1181.106 NA
## 3 No Trend 1140.145 1174.295 NA
# Plot fitted values against actual values
fitted_values <- data.frame(
Date = rep(time(acc_deaths_ts), 3),
Value = c(fitted(additive_model), fitted(multiplicative_model), fitted(no_trend_model)),
Model = rep(c("Additive", "Multiplicative", "No Trend"), each = length(acc_deaths_ts))
)
ggplot() +
geom_line(data = fitted_values, aes(x = Date, y = Value, color = Model)) +
geom_line(data = data.frame(Date = time(acc_deaths_ts), Value = as.numeric(acc_deaths_ts)), aes(x = Date, y = Value), color = "black", linetype = "dashed") +
ggtitle("Fitted Values from ETS Models vs Actual") +
xlab("Year") +
ylab("Number of Deaths") +
scale_color_manual(values = c("Additive" = "blue", "Multiplicative" = "green", "No Trend" = "red"))
Using the following three ETS models:
1. Additive mistakes, additive trend, and additive seasonality make up
the additive model (AAA).
2. Multiplicative errors, additive trend, and multiplicative
seasonality. (MAM)
3. No trend model (ANA): Additive errors, no trend, and additive seasonality.
For every model, compute the AIC, BIC, and RMSE. Then, make a data frame
to show the performance indicators. To properly extract RMSE values, use
print(accuracy(additive_model)) to examine the accuracy output’s
structure.
Now we are able to ascertain which ETS model fits the USAccDeaths
dataset the best. Usually, the model that performs the best has the
lowest values for AIC, BIC, and RMSE. Based on the Model, No trend model
ANA has the lowest value of the BIC, and the MAM model has the lowest
value of AIC. Additionally, superior performance have shown by ETS model
as we can see in the plot which fitted values closely match the plotted
actual data line.