# Estimating Moving Average (MA) Model in R

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.

```
> plot.ts(msft_ts, main="MSFT prices", ylab="Prices")
```

We will fit the MA model to this data using the following command:

```
msft_ma <- arima(msft_ts, order = c(0, 0, 1))
```

The output contains many things including the estimated slope (ma1), mean (intercept), and innovation variance (sigma^2) as shown below:

```
> msft_ma
Call:
arima(x = msft_ts, order = c(0, 0, 1))
Coefficients:
ma1 intercept
0.8602 55.2735
s.e. 0.0259 0.2603
sigma^2 estimated as 4.952: log likelihood = -559.83, aic = 1125.66
>
```

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}=X_{t}−ε_{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.

```
ts.plot(msft_ts)
points(msft_fitted, type = "l", col = 2, lty = 2)
```

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.

```
> msft_ma <- arima(msft_ts, order = c(0, 1, 1))
> residuals <- residuals(msft_ma)
> msft_fitted <- msft_ts - residuals
> ts.plot(msft_ts)
> points(msft_fitted, type = "l", col = 2, lty = 2)
```

#### Course Downloads

Get smart about tech at work.

As a non-technical professional, learn how software works with simple explanations of tech concepts. Learn more...

- Financial Time Series Data
- Exploring Time Series Data in R
- Plotting Time Series in R
- Handling Missing Values in Time Series
- Creating a Time Series Object in R
- Check if an object is a time series object in R
- Plotting Financial Time Series Data (Multiple Columns) in R
- Characteristics of Time Series
- Stationary Process in Time Series
- Transforming a Series to Stationary
- Time Series Transformation in R
- Differencing and Log Transformation
- Autocorrelation in R
- Time Series Models
- ARIMA Modeling
- Simulate White Noise (WN) in R
- Simulate Random Walk (RW) in R
- AutoRegressive (AR) Model in R
- Estimating AutoRegressive (AR) Model in R
- Forecasting with AutoRegressive (AR) Model in R
- Moving Average (MA) Model in R
- Estimating Moving Average (MA) Model in R
- ARIMA Modelling in R
- ARIMA Modelling - Identify Model for a Time Series
- Forecasting with ARIMA Modeling in R - Case Study
- Automatic Identification of Model Using auto.arima() Function in R
- Financial Time Series in R - Course Conclusion

# Data Science for Finance Bundle: 43% OFF

**Data Science for Finance Bundle**for just $29 $51.