skoestlmeier
skoestlmeier

Reputation: 220

gmm Error: Incorrect number of dimensions

I would like to apply a gmm estimation in R and use an individual function g as a formula for the moment restrictions:

library(sandwich)
library(gmm)

#set up data for 10 observations and asume a linear function y~x
intercept <- 1
beta <- 1
x <- 1:10
e <- rnorm(10)
y <- intercept + beta*x + e

#generate matrix
data <- cbind(x,y)

#define function g with moments restrictions for gmm estimation
g <- function(data,theta){
    x <- data[,1]
    y <- data[,2]
    tmp <- x*(y-theta*x)
    return (tmp)
}

estimation <- gmm(g,data)

The example above results in the error:

Error in data[,1]: Incorrect number of dimensions.

I get

dim(data)
[1] 10 2

data[,1]
[1] 1 2 3 4 5 6 7 8 9 10

Apart from the statistic model being a very simple example on learning gmm, what am i doing wrong with my data with regards to the dimensions of my variables?

Upvotes: 0

Views: 1002

Answers (1)

skoestlmeier
skoestlmeier

Reputation: 220

I found my flaw, wich is due to the definition of the function g. As the reference manual for gmm in R states, g can be a formula (if you use linear models) or an explicit function with two parameters. The declaration of g as a function must be the estimated vector of parameter theta as the first argument and the matrix of data x as the second argument. In fact,

g <- function(data,theta){
...
}

has to be replaced by

g <- function(theta,data){
...
}    

Upvotes: 1

Related Questions