Module #12 Assignment
> # Load the forecast package
> library(forecast)
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Warning message:
package ‘forecast’ was built under R version 4.4.2
> # Define the data
> months <- c("Jan", "Feb", "March", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
> data_2012 <- c(31.9, 27, 31.3, 31, 39.4, 40.7, 42.3, 49.5, 45, 50, 50.9, 58.5)
> data_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)
> # Combine data and create time series objects
> time_series_2012 <- ts(data_2012, start = c(2012, 1), frequency = 12)
> time_series_2013 <- ts(data_2013, start = c(2013, 1), frequency = 12)
> # Plot the time series data
> plot(time_series_2012, type = "o", col = "blue", ylim = range(data_2012, data_2013), ylab = "Values", xlab = "Time", main = "Monthly Data for 2012 and 2013")
> lines(time_series_2013, type = "o", col = "red")
> legend("topleft", legend = c("2012", "2013"), col = c("blue", "red"), lty = 1)
> # Simple Exponential Smoothing for 2012 data
> ses_model_2012 <- ses(time_series_2012)
> plot(ses_model_2012, main = "SES Forecast for 2012 Data", ylab = "Values")
> summary(ses_model_2012)
Forecast method: Simple exponential smoothing
Model Information:
Simple exponential smoothing
Call:
ses(y = time_series_2012)
Smoothing parameters:
alpha = 0.8993
Initial states:
l = 31.449
sigma: 5.1829
AIC AICc BIC
73.11971 76.11971 74.57443
Error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set 2.434512 4.731306 3.86967 4.875845 9.263746 NaN -0.3525843
Forecasts:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 2013 57.72065 51.07852 64.36279 47.56238 67.87892
Feb 2013 57.72065 48.78778 66.65352 44.05901 71.38229
Mar 2013 57.72065 46.97476 68.46655 41.28622 74.15508
Apr 2013 57.72065 45.42624 70.01506 38.91798 76.52333
May 2013 57.72065 44.05205 71.38925 36.81633 78.62497
Jun 2013 57.72065 42.80392 72.63738 34.90748 80.53382
Jul 2013 57.72065 41.65246 73.78885 33.14647 82.29484
Aug 2013 57.72065 40.57816 74.86315 31.50347 83.93783
Sep 2013 57.72065 39.56733 75.87398 29.95753 85.48377
Oct 2013 57.72065 38.60988 76.83142 28.49326 86.94805
> # Simple Exponential Smoothing for 2013 data
> ses_model_2013 <- ses(time_series_2013)
> plot(ses_model_2013, main = "SES Forecast for 2013 Data", ylab = "Values")
> summary(ses_model_2013)
Forecast method: Simple exponential smoothing
Model Information:
Simple exponential smoothing
Call:
ses(y = time_series_2013)
Smoothing parameters:
alpha = 0.9675
Initial states:
l = 39.3014
sigma: 4.6971
AIC AICc BIC
70.75776 73.75776 72.21248
Error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set 2.062283 4.287855 3.788006 3.698949 7.678689 NaN -0.2657251
Forecasts:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 2014 63.24383 57.22424 69.26341 54.03766 72.44999
Feb 2014 63.24383 54.86814 71.61951 50.43432 76.05333
Mar 2014 63.24383 53.04242 73.44523 47.64212 78.84553
Apr 2014 63.24383 51.49711 74.99054 45.27878 81.20887
May 2014 63.24383 50.13269 76.35496 43.19207 83.29558
Jun 2014 63.24383 48.89745 77.59020 41.30294 85.18471
Jul 2014 63.24383 47.76044 78.72721 39.56404 86.92361
Aug 2014 63.24383 46.70140 79.78625 37.94437 88.54328
Sep 2014 63.24383 45.70620 80.78145 36.42234 90.06531
Oct 2014 63.24383 44.76451 81.72314 34.98216 91.50549
> # Combine 2012 and 2013 data
> combined_data <- c(data_2012, data_2013)
> time_series_combined <- ts(combined_data, start = c(2012, 1), frequency = 12)
> # Apply Holt-Winters method to combined data
> hw_model_combined <- HoltWinters(time_series_combined)
> forecast_combined <- forecast(hw_model_combined, h = 12)
> # Plot the combined forecast
> plot(forecast_combined, main = "Holt-Winters Forecast for Combined Data", ylab = "Values")
> summary(hw_model_combined)
Length Class Mode
fitted 48 mts numeric
x 24 ts numeric
alpha 1 -none- numeric
beta 1 -none- numeric
gamma 1 -none- numeric
coefficients 14 -none- numeric
seasonal 1 -none- character
SSE 1 -none- numeric
call 2 -none- call Interpretation:
- SES Models for Individual Years are useful for short-term forecasts, as they capture the general upward trend each year but do not account for seasonality due to limited periods.
- Holt-Winters Model for Combined Data is more robust, capturing both trend and seasonality, and it provides a more nuanced long-term forecast by extending the observed monthly pattern across both years.
This analysis indicates that while SES is effective for short-term smoothing, the Holt-Winters model is preferable when both trend and seasonal components are present and sufficient data is available to establish these patterns.
Comments
Post a Comment