Module 12 Assignment
Daniel Tafmizi
Lis 4273
Dr. Ajani
April 6, 2024
Module 12 Assignment
Charge<-c(31.9,27,31.3,31,39.4,40.7,42.3,49.5,45,50,50.9,58.5,39.4,36.2,40.5,44.6,46.8,44.7,52.2,54,48.8 ,55.8,58.7,63.4) #creates a list of all charges.
Chargetimeseries <-ts(Charge) #the 24 charges are arranged in a time series of 24 values.
Chargetimeseries <-ts(Charge, frequency = 12, start=c(2012,1), end = c(2013, 12)) #applies the time span
?ts
Time series function consists of (data, start: natural time unit, frequency: # of obs per time unit)
Data is charge, frequency is 12 observations per 1 year, start is 2012 with time unit being 1 year
plot(Chargetimeseries)
The visualization shows that there were more charges in 2013 than 2012. Charges tend to increase as the year progresses.
We should use Holt-Winters exponential smoothing because we see an increasing trend and seasonality.
?HoltWinters
Chargeforecast<-HoltWinters(Chargetimeseries)
plot(Chargeforecast)
Chargeforecast
Holt-Winters exponential smoothing with trend and additive seasonal component.
Call:
HoltWinters(x = Chargetimeseries)
Smoothing parameters:
alpha: 0.4786973
beta : 0
gamma: 0.1
Coefficients:
[,1]
a 51.4481469
b 0.6088578
s1 -6.6831338
s2 -10.5867440
s3 -6.6998393
s4 -3.0320795
s5 -1.4068647
s6 -4.0422184
s7 0.4727766
s8 6.6378768
s9 1.4431586
s10 5.6809745
s11 5.7999737
s12 12.6976853
Chargeforecast$SSE
SSE = 62.88946
Alpha value tells us level smoothing. Being near the middle at 0.48, the values used for smoothing come from the entire dataset. Meaning newer or older data is not specifically preferred.
Beta value tells us trend smoothing. Being 0, means that the trend is not updated over the time series. This makes sense because the trend stays similar between 2012-2013. Or is it because 2013+ is based off 2012. Meaning only one period of trend is defined given only 2 total periods.
Gamma value tells us seasonal smoothing. being 0.1, means the model is giving a little extra weight to recent seasonal values, but is still using older data to present seasonal changes.
Holt-Winters smoothing only applies to 2013+ in the graph. I thought this was an error with my time series. It is not. The issue is that R "need at least 2 periods to compute seasonal start values". R is using the data in period 1 and 2 but can only apply smoothing to 2 because no previous data explains period 1.
Looking at the chargeforecast graph we see that Holt-Winters did a good job of matching the peaks.
install.packages("forecast")
library(forecast)
Chargeforecast2 <- forecast(Chargeforecast, h=24) # uses Holt Winters smoothing to extend 24 months
plot(Chargeforecast2)
to check our forecast, we can use correlogram, Ljung-box test, and hist of residuals.
acf(na.omit(Chargeforecast2$residuals, lag.max=10))
Box.test(Chargeforecast2$residuals, lag=10, type="Ljung-Box")
Box-Ljung test
data: Chargeforecast2$residuals
X-squared = 8.5163, df = 10, p-value = 0.5785
Autocorrelations of in sample forecast errors do not exceed bounds. This means the model does a good job of presenting the patterns in the data.
P value indicates little evidence of non-zero autocorrelation at lags 1-10.
P val of .5785 shows no significant autocorrelation. Which is good because this means the model is showing an accurate forecast where the data varies over time.
hist(Chargeforecast2$residuals) # forecast errors are evenly distributed with mean 0.
Overall, we can see that Holt-Winters did a good job of modeling the predictions. However, this is to be expected as we have a very limited dataset and cannot do a longitude analysis.
Comments
Post a Comment