### Goals

This tutorial covers the use of user-defined moment equations with `gmmFit`

. After this tutorial, you should be able to

- Define a moment function
- Use a custom moment function to estimate a linear model
- Use a custom moment function to estimate an instrumental variables model
- Estimate a model with multiple moment equations

## The Moment Equation

The `gmmFit`

procedure requires a pointer to a user-specified moment procedure as an input. There are several concepts to keep in mind when creating your moment procedure:

- Procedures are created in GAUSS by placing commands to be executed between the
`proc`

statement and`endp`

statement. - Values are returned from a function using the
`retp`

statement. - Moment procedures must have the parameter vectors as the first input, a data matrix as a second input and must have a single return.

All moment procedures should take the general form :

```
//Procedure definition for moment equation
proc (1) = meqn(b, yt, ...);
…
retp(g);
endp;
```

**Note:**The

`...`

indicates that the moment equation may accept a flexible number of inputs following the first data matrix input. These inputs will be passed untouched from the `gmmFit`

procedure (in the order they are input) to the moment equation.

## Estimating a Linear Model

Consider the case of a linear model:

- The moments for this model are given by $E[x_t u_t(\theta_0)] = 0$ with $u_t(\theta_0) = y_t - \beta_t x_t$.
- This inputs for this equation will include the parameter vector,
`b`

, the dependent variable matrix,`yt`

, and the independent variable matrix,`xt`

.

```
//Procedure definition for moment equation
proc (1) = meqn(b, yt, xt);
local ut, dt;
/** OLS resids **/
ut = yt - b[1] - b[2]*xt[., 2] - b[3]*xt[., 3];
/** Moment conditions **/
dt = ut.*xt;
retp(dt);
endp;
```

The corresponding call to `gmmFit`

takes a pointer to the moment equation, `meqn`

, as the first input, followed by the `y`

and `x`

matrices.

```
//Perform estimation
call gmmFit(&meqn, y, x, gctl);
```

**Note:**The final input to

`gmmFit`

above, `gctl`

, is a `gmmControl`

structure, which will be discussed in the next tutorial.

## Estimating an Instrumental Variables Model

Now suppose that we suspect that there is an issue with endogenous variables and wish to use instrumental variables:

- The moments for this model are given by $E[z u_t(\theta_0)] = 0$ with $u_t(\theta_0) = y_t - \beta_t x_t$.
- This inputs for this equation now include the parameter vector,
`b`

, the dependent variable matrix,`yt`

, the independent variable matrix,`xt`

, and the instruments matrix,`zt`

.

```
//Procedure definition for moment equation
proc (1) = meqn(b, yt, xt, zt);
local ut, dt;
/** OLS residuals **/
ut = yt - b[1] - b[2]*xt[., 2] - b[3]*xt[., 3];
/** Moment conditions **/
dt = ut.*zt;
retp(dt);
endp;
```

The corresponding call to `gmmFit`

again includes a pointer to the moment equation `meqn`

but this time must also include the inputs `y`

, `x`

, and `z`

.

```
//Perform estimation
call gmmFit(&meqn, y, x, z, gctl);
```

## Using Multiple Moments

Finally, let's consider the case where we wish to include multiple moment equations. For this example, we will estimate the degrees of freedom of a t-distribution is the second and fourth moments :

- The moments for this model are given by $E[y^2 - \frac{\nu}{\nu -2}] = 0$ and $E[y^4 - \frac{3\nu^2}{(\nu -2)(\nu - 4)}] = 0$.
- This inputs for this equation include just the parameter vector,
`b`

, the dependent variable matrix,`yt`

. No additional inputs are required. - When multiple moments are included, they must be concatenated together and returned as a single output from the moment equation.

```
//Procedure definition for moment equation
proc(1) = meqn(b, yt);
local g1, g2;
g1 = yt.^2 - b/(b-2);
g2 = yt.^4 - (3*b^2)/((b-2)*(b-4));
retp(g1~g2);
endp;
```

The corresponding call to `gmmFit`

includes a pointer to the moment equation `meqn`

and `y`

.

```
//Perform estimation
call gmmFit(&meqn, y);
```

### Conclusion

Congratulations! You have:

- Defined a moment equation for use in the
`gmmFit`

procedure. - Created a custom moment equation to estimate a linear model.
- Created a custom moment function to estimate an instrumental variables model.
- Estimated a model with multiple moment equations.

The next tutorial describes the model control settings available in the `gmmControl`

structure.