2. Time Series: Simple Moving Average
Types:
• Simple Moving Averages
• Exponential Moving Averages
A simple moving average is formed by computing the average value of a series over
a specific number of periods.
data("AirPassengers")
View(AirPassengers)
AirPassengers1=AirPassengers
plot(AirPassengers1)
library(TTR)
AirPassengers1_smoothened<- SMA(AirPassengers1,n=8)
#n= number of periods to average over
plot.ts(AirPassengers1_smoothened)
Rupak Roy
Before After
3. Time Series: Exponential Moving
Exponential Moving Averages:
• The weighting applied to the values depends on the number of
periods in the moving average.
• Further we can do predictive modeling and forecast future time
points with exponential smoothening averages by using HoltWinters()
function.
Rupak Roy
4. Time Series: Exponential Moving
data("EuStockMarkets")
euro_stocks<-EuStockMarkets
euro_stocks1<-as.data.frame(EuStockMarkets)
euro_stocks2<-euro_stocks1$DAX
#create time series data
eu_stocks_ts3<-ts(euro_stocks2,frequency=12,
start = c(1991,1), end = c(1998))
eu_stocks_ts3
plot(eu_stocks_ts3))
#Log transformation(Reducing the spread of data)
logTimeSeries<-log(eu_stocks_ts3)
plot.ts(logTimeSeries)
#Apply Exponential smoothening average- Holt winters
TimeSeries.exp<-HoltWinters(logTimeSeries,beta=FALSE)
TimeSeries.exp
plot(TimeSeries.exp)
#We can observe the accuracy of the HoltWinters output is very close to the original
values means it is able to predict with high accuracy
Rupak Roy
5. Time Series: Data smoothening
So exponential data smoothening method is more effective way to
reduce the noise for time series analysis.
TimeSeries.exp$fitted
TimeSeries.exp
Exponential Smoothing is controlled by 3 parameters.
Alpha: displays the estimates
Beta: If False, then the function used exponential smoothing.
Gamma: is the parameter used to display the seasonal component
Rupak Roy
6. Time Series: Data smoothening
Smoothing Parameters –
The estimated values of alpha, beta and gamma are 0.9, FALSE and
0.45 respectively.
If The value of alpha is relatively low(<0.5) then the estimates of the level
at the current time point is based upon both recent observations.
Beta = FALSE indicates the function will do exponential smoothening.
Gamma = used for the seasonal component. If set to FALSE, an non-
seasonal model is fitted.
If the Gamma Value >0.6, then the estimates of the seasonal
component at the current time point is based upon recent
observations.
TimesSeries.exp$SSE # is the error rate, the lower the value the better the
model is able to predict.
Rupak Roy
7. Time Series: Data smoothening
Now if we want to predict for say 36 months
library(forecast)
TimeSeriesForecast<-forecast(logTimeSeries,h=36)
plot(TimeSeriesForecast)
Rupak Roy