R Generalized Method Of Moments Regression Estimation With Instruments

0 votes

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 in Machine Learning by Dev
• 6,000 points
697 views

1 answer to this question.

0 votes

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 AI ML Course.

answered Apr 13, 2022 by anonymous

Related Questions In Machine Learning

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to add regression line equation and R2 on graph?

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

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,250 points
6,732 views
0 votes
1 answer

Modular programming in R language

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

answered Jan 31, 2019 in Data Analytics by Tyrion anex
• 8,700 points
795 views
0 votes
1 answer

How to export regression equations for grouped data?

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

answered Mar 14, 2022 in Machine Learning by Dev
• 6,000 points
586 views
0 votes
1 answer

R: Force regression coefficients to add up to 1

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

answered Mar 23, 2022 in Machine Learning by Nandini
• 5,480 points
1,891 views
0 votes
1 answer

Plotting logistic regression in R with the Smarket dataset

The first, third, and fourth methods of ...READ MORE

answered Apr 12, 2022 in Machine Learning by Dev
• 6,000 points
984 views
0 votes
1 answer

different results for Random Forest Regression in R and Python

Random Forests, as others have mentioned, have ...READ MORE

answered Apr 12, 2022 in Machine Learning by Dev
• 6,000 points
1,536 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP