Now that we know how to estimate the AR model using ARIMA, we can create a simple forecast based on the model.
Step 1: Fit the model
The first step is to fit the model as ARIMA(1, 0, 0). We have already seen this in the previous lesson.
> msft_ar <- arima(msft\_ts,c(1,0,0))
Step 2: Create Forecast
We can now use the predict() function to create a forecast using the fitted AR model. It takes as its inputs, the model object that we created in step 1, and an additional parameter n.ahead which establishes the forecast horizon, that is, how many steps (periods) in the future we want to create the forecast. In our example, we will provide n.ahead=20, which will create forecast for next 20 steps which corresponds to 20 days for our daily data.
The object generated by the predict() command contains two time series: 1) $pred which contains the forecasted values and 2) $se which contains the standard error for the forecast. We will use the $pred time series to plot the forecast and the $se time series to add confidence intervals to our plot.
We can now use the plot.ts() function to first plot the original data and then add points for the forecasted values using the points() function as shown below: