library(readr)
delhiClimate <- read_csv("DailyDelhiClimateTest.csv")
## Rows: 114 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (4): meantemp, humidity, wind_speed, meanpressure
## date (1): date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
This data set was found on kaggle.com. It contains 114 observations. The Dataset is fully dedicated for the developers who want to train the model on Weather Forecasting for Indian climate. This dataset provides data from 1st January 2013 to 24th April 2017 in the city of Delhi, India. The 4 parameters here are meantemp, humidity, wind_speed, meanpressure.
test.climate = delhiClimate$meantemp[100:114]
train.climate = delhiClimate$meantemp[1:99]
climate=ts(delhiClimate$meantemp[1:99], start=2017, frequency=15)
fit1 = ses(climate, h=15)
fit2 = holt(climate, initial="optimal", h=15) ## optimal alpha and beta
fit3 = holt(climate,damped=TRUE, h=15 ) ## additive damping
fit4 = holt(climate,exponential=TRUE, damped=TRUE, h =15) ## multiplicative damp
fit5 = hw(climate,h=15, seasonal="additive") ## default h = 10
fit6 = hw(climate,h=15, seasonal="multiplicative")
fit7 = hw(climate,h=15, seasonal="additive",damped=TRUE)
fit8 = hw(climate,h=15, seasonal="multiplicative",damped=TRUE)
accuracy.table = round(rbind(accuracy(fit1), accuracy(fit2), accuracy(fit3), accuracy(fit4),
accuracy(fit5), accuracy(fit6), accuracy(fit7), accuracy(fit8)),4)
row.names(accuracy.table)=c("SES","Holt Linear","Holt Add. Damped", "Holt Exp. Damped",
"HW Add.","HW Exp.","HW Add. Damp", "HW Exp. Damp")
kable(accuracy.table, caption = "The accuracy measures of various exponential smoothing models
based on the training data")
| ME | RMSE | MAE | MPE | MAPE | MASE | ACF1 | |
|---|---|---|---|---|---|---|---|
| SES | 0.1261 | 1.7531 | 1.3857 | 0.0986 | 7.2119 | 0.3473 | -0.0053 |
| Holt Linear | 0.0058 | 1.7487 | 1.3649 | -0.5364 | 7.1316 | 0.3420 | -0.0001 |
| Holt Add. Damped | 0.1538 | 1.7595 | 1.3990 | 0.2630 | 7.2792 | 0.3506 | -0.0069 |
| Holt Exp. Damped | 0.1001 | 1.7563 | 1.3861 | -0.0211 | 7.2287 | 0.3473 | -0.0123 |
| HW Add. | -0.0185 | 1.5513 | 1.2171 | -0.5312 | 6.3272 | 0.3050 | -0.0246 |
| HW Exp. | 0.2111 | 1.7043 | 1.2994 | 0.6000 | 6.6533 | 0.3256 | 0.3449 |
| HW Add. Damp | 0.1323 | 1.5539 | 1.2025 | 0.2722 | 6.1986 | 0.3013 | -0.0029 |
| HW Exp. Damp | 0.0313 | 1.5535 | 1.1976 | -0.3477 | 6.2129 | 0.3001 | 0.0310 |
The above table shows that the HW additive seems to be the most appropriate.
par(mfrow=c(2,1), mar=c(3,4,3,1))
###### plot the original data
pred.id = 100:114
plot(1:99, train.climate, lwd=2,type="o", ylab="Beverage", xlab="",
xlim=c(1,135), ylim=c(0, 40), cex=0.3,
main="Non-seasonal Smoothing Models")
lines(pred.id, fit1$mean, col="red")
lines(pred.id, fit2$mean, col="blue")
lines(pred.id, fit3$mean, col="purple")
lines(pred.id, fit4$mean, col="navy")
##
points(pred.id, fit1$mean, pch=16, col="red", cex = 0.5)
points(pred.id, fit2$mean, pch=17, col="blue", cex = 0.5)
points(pred.id, fit3$mean, pch=19, col="purple", cex = 0.5)
points(pred.id, fit4$mean, pch=21, col="navy", cex = 0.5)
#points(fit0, col="black", pch=1)
legend("bottomright", lty=1, col=c("red","blue","purple", "navy"),pch=c(16,17,19,21),
c("SES","Holt Linear","Holt Linear Damped", "Holt Multiplicative Damped"),
cex = 0.7, bty="n")
###########
plot(1:99, train.climate, lwd=2,type="o", ylab="Beverage", xlab="",
xlim=c(1,135), ylim=c(0, 40), cex=0.3,
main="Holt-Winterd Teend and Seasonal Smoothing Models")
lines(pred.id, fit5$mean, col="red")
lines(pred.id, fit6$mean, col="blue")
lines(pred.id, fit7$mean, col="purple")
lines(pred.id, fit8$mean, col="navy")
##
points(pred.id, fit5$mean, pch=16, col="red", cex = 0.5)
points(pred.id, fit6$mean, pch=17, col="blue", cex = 0.5)
points(pred.id, fit7$mean, pch=19, col="purple", cex = 0.5)
points(pred.id, fit8$mean, pch=21, col="navy", cex = 0.5)
###
legend("bottomright", lty=1, col=c("red","blue","purple", "navy"),pch=c(16,17,19,21),
c("HW Additive","HW Multiplicative","HW Additive Damped", "HW Multiplicative Damped"),
cex = 0.7, bty="n")
We can see from the above accuracy table that HW’s linear trend with an additive seasonal model is the best of the eight smoothing models. This is consistent with the patterns in the original serial plot.
Since we train the model with the training data and identify the best model using both training and testing data. Both methods yield the same results. To use the model for real-forecast, we need to refit the model using the entire data to update the smoothing parameters in the final working model.
acc.fun = function(test.data, mod.obj){
PE=100*(test.data-mod.obj$mean)/mod.obj$mean
MAPE = mean(abs(PE))
###
E=test.data-mod.obj$mean
MSE=mean(E^2)
###
accuracy.metric=c(MSE=MSE, MAPE=MAPE)
accuracy.metric
}
pred.accuracy = rbind(SES =acc.fun(test.data=test.climate, mod.obj=fit1),
Holt.Add =acc.fun(test.data=test.climate, mod.obj=fit2),
Holt.Add.Damp =acc.fun(test.data=test.climate, mod.obj=fit3),
Holt.Exp =acc.fun(test.data=test.climate, mod.obj=fit4),
HW.Add =acc.fun(test.data=test.climate, mod.obj=fit5),
HW.Exp =acc.fun(test.data=test.climate, mod.obj=fit6),
HW.Add.Damp =acc.fun(test.data=test.climate, mod.obj=fit7),
HW.Exp.Damp =acc.fun(test.data=test.climate, mod.obj=fit8))
kable(pred.accuracy, caption="The accuracy measures of various exponential smoothing models
based on the testing data")
| MSE | MAPE | |
|---|---|---|
| SES | 26.42521 | 17.58257 |
| Holt.Add | 17.72304 | 13.88152 |
| Holt.Add.Damp | 26.48409 | 17.60938 |
| Holt.Exp | 27.55424 | 18.05462 |
| HW.Add | 13.64396 | 12.41508 |
| HW.Exp | 13.66827 | 12.31397 |
| HW.Add.Damp | 21.34232 | 16.22779 |
| HW.Exp.Damp | 19.81962 | 15.66466 |
We can see from the above accuracy table that HW’s linear trend with an additive seasonal model is the best of the eight smoothing models. This is consistent with the patterns in the original serial plot.
In the previous analysis, we train the model with the training data and identify the best model using both training and testing data. In real-forecast, we need to refit the model at the very end using the entire data to update the smoothing parameters in the final working model.
climate=ts(delhiClimate$meantemp[1:114], start=2017, frequency = 15)
final.model = hw(climate,h=15, seasonal="additive")
smoothing.parameter = final.model$model$par[1:3]
kable(smoothing.parameter, caption="Estimated values of the smoothing parameters in
Holt-Winters linear trend with additive seasonality")
| x | |
|---|---|
| alpha | 0.8466125 |
| beta | 0.0003930 |
| gamma | 0.0006414 |
In summary, the updated values of the three smoothing parameters in the Holt-Winters linear trend and with additive seasonality using the entire data are given in the above table.