We will now see how we can fit an MA model to a given time series using the arima() function in R. Recall that MA model is an ARIMA(0, 0, 1) model.
We can use the arima() function in R to fit the MA model by specifying order = c(0, 0, 1).
We will perform the estimation using the msft_ts time series that we created earlier in the first lesson. If you don't have the msft_ts loaded in your R session, please follow the steps to create it as specified in the first lesson.
Let's start by creating a plot of the original data using the plot.ts() function.
The msft_ma object also contains the residuals (εt ). We can extract the residuals, using the residuals() function in R.
residuals <- residuals(msft\_ma)
Once you find the residuals εt, the fitted values are just X̂t=Xt−εt. In R, we can do it as follows:
msft_fitted <- msft_ts - residuals
We can now plot both the original and the fitted time series to see how close the fit is.
1ts.plot(msft_ts)2points(msft_fitted,type="l", col =2, lty =2)3
As we can see, the model does not provide a good fit for the original series. One possible reason is that our original series was not stationary. We can fit the ARIMA model with first order differencing by passing the parameter I=1.