# Set CRAN mirror
chooseCRANmirror(graphics = FALSE, ind = 1)
# Install and load necessary packages
install.packages("fpp2")
## Installing package into 'C:/Users/klynn/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'fpp2' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\klynn\AppData\Local\Temp\RtmpyML2Qy\downloaded_packages
install.packages("MASS")
## Installing package into 'C:/Users/klynn/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'MASS' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'MASS'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\klynn\AppData\Local\R\win-library\4.5\00LOCK\MASS\libs\x64\MASS.dll to
## C:\Users\klynn\AppData\Local\R\win-library\4.5\MASS\libs\x64\MASS.dll:
## Permission denied
## Warning: restored 'MASS'
##
## The downloaded binary packages are in
## C:\Users\klynn\AppData\Local\Temp\RtmpyML2Qy\downloaded_packages
install.packages("tseries")
## Installing package into 'C:/Users/klynn/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'tseries' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'tseries'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\klynn\AppData\Local\R\win-library\4.5\00LOCK\tseries\libs\x64\tseries.dll
## to C:\Users\klynn\AppData\Local\R\win-library\4.5\tseries\libs\x64\tseries.dll:
## Permission denied
## Warning: restored 'tseries'
##
## The downloaded binary packages are in
## C:\Users\klynn\AppData\Local\Temp\RtmpyML2Qy\downloaded_packages
install.packages("forecast")
## Installing package into 'C:/Users/klynn/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'forecast' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'forecast'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\klynn\AppData\Local\R\win-library\4.5\00LOCK\forecast\libs\x64\forecast.dll
## to
## C:\Users\klynn\AppData\Local\R\win-library\4.5\forecast\libs\x64\forecast.dll:
## Permission denied
## Warning: restored 'forecast'
##
## The downloaded binary packages are in
## C:\Users\klynn\AppData\Local\Temp\RtmpyML2Qy\downloaded_packages
library(fpp2)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching packages ────────────────────────────────────────────── fpp2 2.5 ──
## ✔ ggplot2 3.5.2 ✔ fma 2.5
## ✔ forecast 8.24.0 ✔ expsmooth 2.3
##
library(MASS)
##
## Attaching package: 'MASS'
## The following objects are masked from 'package:fma':
##
## cement, housing, petrol
library(tseries)
library(forecast)
# Load the dataset
data("wmurders", package = "fpp2")
plot(wmurders, type = "o", main = "Annual Number of Women Murdered",
ylab = "Murders per 100,000", xlab = "Time")

bc <- boxcox(wmurders ~ 1) # Apply Box-Cox transformation

lambda <- bc$x[which.max(bc$y)] # Find optimal lambda
lambda # Print optimal lambda value
## [1] 1.070707
diff_wmurders <- diff(wmurders) # First differencing
plot(diff_wmurders, type = "o", main = "Differenced Series",
ylab = "Differenced Values", xlab = "Time")

adf.test(wmurders) # ADF test on original series
##
## Augmented Dickey-Fuller Test
##
## data: wmurders
## Dickey-Fuller = -0.29243, Lag order = 3, p-value = 0.9878
## alternative hypothesis: stationary
acf(wmurders) # ACF plot

pacf(wmurders) # PACF plot

fit_arima <- arima(wmurders, order = c(1, 1, 1)) # ARIMA(1, 1, 1) model
summary(fit_arima) # View the ARIMA model summary
##
## Call:
## arima(x = wmurders, order = c(1, 1, 1))
##
## Coefficients:
## ar1 ma1
## -0.8255 0.6916
## s.e. 0.1941 0.2333
##
## sigma^2 estimated as 0.04317: log likelihood = 8.18, aic = -10.36
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.003836344 0.2058693 0.1549269 -0.03219612 4.470116 0.9527276
## ACF1
## Training set 0.1071846
fit_arma <- arima(diff_wmurders, order = c(1, 0, 1)) # ARMA(1, 1) model
summary(fit_arma) # View the ARMA model summary
##
## Call:
## arima(x = diff_wmurders, order = c(1, 0, 1))
##
## Coefficients:
## ar1 ma1 intercept
## -0.8260 0.6918 0.0038
## s.e. 0.1937 0.2329 0.0262
##
## sigma^2 estimated as 0.04315: log likelihood = 8.19, aic = -8.38
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -0.0002570187 0.2077243 0.1577287 414.8128 428.9562 0.6760764
## ACF1
## Training set 0.107438
residuals_arima <- residuals(fit_arima) # Get residuals from ARIMA model
plot(residuals_arima) # Residuals plot

acf(residuals_arima) # ACF of residuals to check for autocorrelations

plot(forecast(fit_arima, h = 12)) # Forecast for the next 12 periods using ARIMA

plot(forecast(fit_arma, h = 12)) # Forecast for the next 12 periods using ARMA
