1

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
3
  • I mean you could do something like: id <- "1EP3odZcVbNCSMZYAgbEtMV5_2oB8nRHl" ; data_r <- read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id)) but what if you deleted that file later on. Then as I explained, the question won't be useful to future readers.
    – M--
    Commented Jul 5 at 0:53
  • 1
    @M-- I've modified the code so that it generates some data and replicates de error, I think it now meet the standards. Are you able to reopen it if this is the case?
    – user280809
    Commented Jul 5 at 1:16
  • gmm(g, data_r, t0 = rep(0, 4)) ?
    – Edward
    Commented Jul 5 at 4:52

1 Answer 1

1

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  *******

Not the answer you're looking for? Browse other questions tagged or ask your own question.