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]
After trying a number of different things, what worked was dropping x variables until the number of x variables was the rank of the matrix including all of them as columns: it appears
gmm
does not like having any singularity in the matrix of exogenous instruments used in the moment conditions. Explicitly, when I changed to this set of moment conditions, the same gmm call worked: