Creating Time Series

library(forecast)
data<-read.csv("/home/peopleanalytics/Downloads/arima.csv")
N1725<-data$N1725
myts<- ts(N1725, frequency=12, start=c(2014,1))
myts2 <- window(myts, start=c(2014, 1), end=c(2016, 12)) 

Data dari 3 tahun

print(myts)
##        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov
## 2014 44594 45479 48497 48944 48358 50988 43642 45806 46225 48675 49074
## 2015 35148 34835 40521 41493 37331 37662 23825 36042 36747 38584 38235
## 2016 28309 28303 29905 30873 32349 33991 23186 35027 34299 35279 36392
##        Dec
## 2014 49867
## 2015 35288
## 2016 35140

Seasonal Decomposition

A time series with additive trend, seasonal, and irregular components can be decomposed using the stl() function. Note that a series with multiplicative effects can often by transformed into series with additive effects through a log transformation (i.e., newts <- log(myts)).

library(forecast)
fit <- stl(myts, s.window="period")
plot(fit)

#additional plots
monthplot(myts)

library(forecast)
seasonplot(myts)

Exponential Models

Both the HoltWinters() function in the base installation, and the ets() function in the forecast package, can be used to fit exponential models.

Simple Exponential - models level

fit1 <- HoltWinters(myts, beta=FALSE, gamma=FALSE)
f1<-forecast(fit1, 3)
plot(forecast(fit1, 3))

print(f1)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Jan 2017        35056.9 28687.22 41426.58 25315.31 44798.48
## Feb 2017        35056.9 28073.80 42040.00 24377.17 45736.63
## Mar 2017        35056.9 27510.07 42603.72 23515.03 46598.77

Double exp - models level and trend

fit2 <- HoltWinters(myts, gamma=FALSE)
f2<-forecast(fit2, 3)
plot(forecast(fit2, 3))

print(f2)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Jan 2017       35209.00 28441.73 41976.28 24859.35 45558.66
## Feb 2017       35271.57 27556.31 42986.84 23472.09 47071.05
## Mar 2017       35334.15 26647.05 44021.24 22048.39 48619.90

Triple Exponential – models level, trend, and seasonal components

fit3 <- HoltWinters(myts)
f3<-forecast(fit3, 3)
plot(forecast(fit3, 3))

print(f3)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Jan 2017       24299.82 19599.85 28999.79 17111.84 31487.80
## Feb 2017       24222.71 19056.03 29389.39 16320.96 32124.46
## Mar 2017       28648.50 22901.45 34395.54 19859.15 37437.84

Automatic Model

The forecast package provides functions for the automatic selection of exponential and ARIMA models. The ets() function supports both additive and multiplicative models. The auto.arima() function can handle both seasonal and nonseasonal ARIMA models. Models are chosen to maximize one of several fit criteria.

Automated forecasting using an exponential model

fit4 <- ets(myts)
f4<-forecast(fit4, 3)
plot(forecast(fit4, 3))

print(f4)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Jan 2017          35024 28797.07 41250.92 25500.73 44547.26
## Feb 2017          35024 28223.82 41824.17 24624.03 45423.97
## Mar 2017          35024 27695.27 42352.72 23815.68 46232.31

Automated forecasting using an ARIMA model

fit5 <- auto.arima(myts)
f5<-forecast(fit5, 3)
plot(forecast(fit4, 3))

print(f5)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Jan 2017       29300.31 24980.42 33620.21 22693.61 35907.02
## Feb 2017       29295.87 24370.11 34221.63 21762.57 36829.17
## Mar 2017       30481.63 25016.76 35946.49 22123.84 38839.42