Hot Topics in Analytics

# Time Series Analysis – II ### Time series analysis using R:

In our article – Time Series Analysis – I, method to perform time series analysis using R is explained. In this article we will learn another method to perform Time Series Analysis.

Method-2:

First we need to load some libraries which is used for the time series analysis in R The next thing is we need to import the data on which we need to perform the analysis. The data set “sales-data.csv” has 77 rows and 2 columns.

These are the first few rows of the data set. Now lets take only first 71 rows for modeling and then use the other 6 rows for testing. But before splitting the data we find the total data time series. The plot of the time series of the input data is as follows ARMA:
Let us create a data frame “timeserdf” with the month and the time series values which we generated from the ts() function in the previous step. Name the columns as Month and Sales. Now let us build a linear regression model on the “timeserdf” to model the global predictable part that is trend and seasonality and predict the values of sales using that model. We plot the global values predicted. It will resemble the original time series.  Now as discussed in the steps of time series analysis we have to remove the global predictable part from the total time series so that we get the local predictable part which is a weakly stationary series. To check whether the resulted series is weakly stationary or not we plot ACF and PACF plots.
This local predictable part should be modeled using classical decomposition method or ARIMA. First we will see the classical decomposition method. Note: auto.arima automatically select the best p and q value.  The result of the armafit is as follows, In ARIMA(p,i,q) , p and q are the order of AR and MA process and i is the differencing factor which is 0 in classical decomposition method.
Now we will check the residual series which remains after armafit model values are separated from local predictable series. And this residual should be strong stationary series. So we perform ADF and KPSS test to check it. The result of ADF test is as follows, Here the p-value < 0.05 means we reject the null hypothesis i.e. the residual series is strongly stationary.
The result of KPSS test is as follows, Here the p-value > 0.05 means we accept the null hypothesis i.e. the residual series is strongly stationary.
Now let us evaluate the model using MAPE. For that we need to predict the values of the next 6 observations of the data set. Now let us compare our prediction with the actual values by using MAPE. Here we get MAPE as 37.06 which is a better value.
Now let us plot the predicted values along with the original values for better understanding that how well the model is fit. ARIMA modeling:

First we model the timeser that is here we don’t need to separate global part and local part and model them separately. Now we remove the predictable part from the time series and we get the residual. We need to check whether this residual is strongly stationary or not. The results of the ADF test is as follows, Here the p-value < 0.05 means we reject the null hypothesis i.e. the residual series is strongly stationary.

The result of KPSS test is as follows, Here the p-value > 0.05 means we accept the null hypothesis i.e. the residual series is strongly stationary.
Now let us evaluate the model using MAPE. For that we need to predict the values of the next 6 observations of the data set. The MAPE value is 137.14.
Now let us plot the predicted values along with the original values for better understanding that how well the model is fit. Note: Here we noticed that MAPE value for ARMA model is less than the MAPE value for ARIMA model. So we can say that the ARMA model is better in this case for the time series analysis.

### References: 