I'm trying to train a regression model using the generalized method of moments in R. I have 3 endogenous regressors that are correlated with 6 things I know to be exogenous.

• My outcome variable is y
• I have 3 endogenous regressors: z1, z2, z1*z2
• I have 6 exogenous instruments: x1, x2, x3, x4, x5, x6

In order to do the training I set my data up in an data.matrix dat. The first column is y, the second column is all 1s, the third through eighth columns are the instruments x1-x6. The 9th through 11th columns are the regressors (z1, z2, and z1*z2).

Then I set my moment conditions as follows:

```moments <- function(theta, data) {
y <- as.numeric(data[, 1])
x <- data.matrix(data[, c(2,3:8)])
z <- data.matrix(data[, c(2,9,10,11)])
m <- x * as.vector((y - z %*% theta))
return(cbind(m))
}
```

and then I try to train my model:

```gmm_model <- gmm(
g = moments,
x = dat,
t0 = (lm(y ~ z1 + z2 + z1:z2,
data=dat))\$coefficients
)
```

When I run this I get an error: model order: 1 singularities in the computation of the projection matrix results are only valid up to model order 0Error in AA %*% t(X) : requires numeric/complex matrix/vector arguments

The error indicates that the rank of x is not big enough to estimate the coefficients on the variables corresponding to z.

But when I check Matrix::rankMatrix(dat[,3:8]) tells me my x has rank 5 and Matrix::rankMatrix(dat[2,9,10,11]) tells me my z has rank 4. Additionally, rankMatrix(t(x) %*% z ) yields 4. These values seem to be fine for GMM to me. What am I doing wrong?

I also tried to use pgmm from plm, but my data is really just a cross section (not a panel dataset) and I was getting errors for having multiple observations per individual when I tried to use it.

dat looks like:

```     y i x1 x2  x3   x4     x5      x6 z1        z2 z1*z2
[1,] 0 1 31  0 123 0.12 123456 1234567  0 0.2954545     0
[2,] 0 1 44  0 123 0.12 123456 1234567  0 0.1555556     0
[3,] 0 1 31  0 123 0.12 123456 1234567  0 0.2325581     0
[4,] 0 1 47  0 123 0.12 123456 1234567  0 0.2537313     0
[5,] 0 1 33  0 123 0.12 123456 1234567  0 0.1500000     0
[6,] 0 1 49  0 123 0.12 123456 1234567  0 0.2553191     0
```

x1 is an integer in [30-100] x2 is binary 0,1 x3 takes two values x4 takes two values x5 takes two values x6 takes two values

z1 is binary 0,1 z2 is continuous in [0,1]

Apr 11, 2022 341 views

## 1 answer to this question.

After a variety of approaches, it was discovered that dropping x variables until the number of x variables equaled the rank of the matrix with all of them as columns worked: it appears that gmm does not like singularities in the matrix of exogenous instruments employed in the moment conditions. Explicitly, the identical gmm call worked when I changed to this set of moment conditions:

```moment <- function(theta, data) {
a <- as.numeric(data[, 1])
b <- data.matrix(data[, c(2,3,4,5,6)]) # this is rank 5 still
x <- data.matrix(data[, c(2,9,10,11)]) # rank 4
h <- b * as.vector((a - x %*% theta))
return(cbind(h))
}```

Elevate your skills with our comprehensive Machine Learning Course.

answered Apr 13, 2022 by anonymous

## Example of Logistic regression with python code

Have a look at this: import csv import ...READ MORE

## Is there a way to force the coefficient of the independent variable to be a positive coefficient in the linear regression model used in R?

A Few Constraints This is an example of ...READ MORE

## How to add regression line equation and R2 on graph?

Below is one solution: # GET EQUATION AND ...READ MORE

## Modular programming in R language

R provides support to create subscripts. For ex. ...READ MORE

## How to export regression equations for grouped data?

First, you'll need a linear model with ...READ MORE

## R: Force regression coefficients to add up to 1

b1 + b2 = 1 Let us fit ...READ MORE