Assignment 4
Assignment 4
Hint: You may start with the following code. This may require some modification. Also you have to
replicate the code for other food items.
d=read.csv("Apollo.csv",header=T)
attach(d)
# Modeling Idly Demand
Idly=ts(Idly)
Idly_Train=window(Idly,start=1,end=108)
Idly_Test=window(Idly,start=109,end=115)
# Modeling on Training Data
# Plot
plot(Idly_Train,col="red",lwd=2)
# Check whether white noise
Box.test(Idly_Train)
Box.test(Idly_Train,type="Lj")
# ACF and PACF Plot
par(mfrow=c(1,2))
Acf(Idly_Train,main="",col="red",lwd=2)
Pacf(Idly_Train,main="",col="red",lwd=2)
# Stationarity Check
# Formal Tests
adf.test(Idly_Train, alternative = "stationary")
kpss.test(Idly_Train)
# ARIMA Model
fit=auto.arima(Idly_Train)
# Diagnostic Tests
res=residuals(fit)
par(mfrow=c(1,2))
Acf(res,main="ACF",col="red",lwd=2)
Pacf(res,main="PACF",col="red",lwd=2)
# Formal Checks
Box.test(res,fitdf=1,lag=10)
Box.test(res,fitdf=1,lag=10,type="Lj")
# Forecasting
Idly_for<-forecast(fit,h=7)
#Accuracy
accuracy(Idly_for,Idly_Test)
# Auto Arima
fit=auto.arima(Idly_Train,xreg=BKFST_OCCUP_Train)
# Forecasting
Idly_for=forecast(fit,xreg=BKFST_OCCUP_Test,h=7)
#Accuracy
accuracy(Idly_for,Idly_Test)
Note: An alternative approach to fit a Time Series model is to divide the data into a training
set and a test set (with sufficient number of observations of course, say around 20%). Then
one should build a number of models based on the training data and obtain the test error
(say, MAPE) for each one of them. The model with minimum test error is the recommended
model. This was briefly suggested in the class. To fit the models manually, you should use
Arima() function. However, for answering the questions above, you use auto.arima()
function based on 108 observations to get the optimal model, then use the model to forecast
for next seven days. See how your model is performing for the next seven days based on point
and interval forecasts and the actual observations. Finally refit your model on the entire data
set and provide one-step or two-step ahead forecasts.