I am trying to estimate a model with the GMM library from R. However, when I run my code I am getting the error "requires numeric/complex matrix/vector arguments". I converted my Data Frame to a matrix as suggested in multiple similar questions but I'm still getting the same error, any ideas on how to solve the issue? I know the model is very simple but I was trying to get to understand how the command works. The data that I am loading is all numeric, I've tested it with the mode (X) function.
library(gmm) # For gmm
data_r <- data.frame(replicate(7,sample(0:5,1000,rep=TRUE)))
g <- function(theta, data_r){
y <- as.numeric(data_r[, "X1"])
x <- as.matrix(data_r[,c("X2", "X3", "X4", "X5")])
z <- as.matrix(data_r[,c("X2", "X6", "X4", "X7")])
# Moment conditions
m1 <- z * as.vector(y - x %*% theta)
return(cbind(m1))
}
g_test <- gmm(g, data_r)
summary(g_test)
Which returns an error:
Error in x %*% theta: requires numeric/complex matrix/vector arguments
According to the help page of gmm
t0
A k × 1 k×1 vector of starting values. It is required only when "g" is a function because only then a numerical algorithm is used to minimize the objective function. ...
So, you need to supply the starting values (t0
), the length of which must be equal to the number of columns of x
, which is 4, since you multiply x
by theta in your function.
g_test <- gmm(g, data_r, t0 = rep(0, 4))
summary(g_test)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
Theta[1] -0.152082 0.322655 -0.471347 0.637393
Theta[2] -0.866059 3.465883 -0.249881 0.802679
Theta[3] -0.060006 0.099921 -0.600539 0.548147
Theta[4] 2.048832 3.735988 0.548404 0.583414
J-Test: degrees of freedom is 0
J-test P-value
Test E(g)=0: 0.00173925349613719 *******