"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

August 15, 2016

Day #28 - R - Forecast Library Examples

Following Examples discussed. Library used - R - Forecast Library
  • Moving Average
  • Single Exponential Smoothing - Uses single smoothing factor
  • Double Exponential Smoothing - Uses two constants and is better at handling trends
  • Triple Exponential Smoothing - Smoothing factor, trend, seasonal factors considered
  • ARIMA

#Create Dataset
#Generate sample data of 100 records with mean = 850 and standard deviation = 900
myvector <- rnorm(1000, 850, 900)
myvector
#Split it into timeseries weekly data for 104 weeks
myts = ts(myvector, start=c(2014,1),end=c(2016,1),frequency=104)
myts
#Plot Data
plot(myts)
#Apply moving average
sm = ma(myts, order=4)#4 week average
plot(sm)
#Plotting with Single Exponential
fit = HoltWinters(myts,beta = FALSE, gamma = FALSE)
plot(fit)
#Plotting with Double Exponential
fit = HoltWinters(myts, gamma = FALSE)
plot(fit)
#triple exponential
fit = HoltWinters(myts)
#Additive triple exponential
fit1 = HoltWinters(myts, seasonal = "multiplicative")
#Multiplicative triple exponential
fit2 = HoltWinters(myts, seasonal = "additive")
#ARIMA
arimafit = arima(myts, order = c(0,0,0))
summary(arimafit)
plot(myts, xlim=c(2014,2016),lw=2,col="blue")
lines(predict(arimafit,n.ahead=50)$pred,lw=2,col="red")
#Auto Regression
arfit = ar(myts)
summary(arfit)
pred = predict(arfit,n.ahead=30)
plot(myts,type="l",xlim=c(2014,2016),ylim=c(100,2200),xlab="weeks",ylab="forecast")
lines(pred$pred,col="red")
Happy Learning!!!

No comments: