install.packages("gmm", dep=T) rm(list=ls()) # go to this link: http://www.brodrigues.co/2013/11/07/gmm-with-rmd/ library(gmm) dat <- read.table(file="benefits.R", header = T) attach(dat) native <- glm(y ~ age + age2, family = binomial(link = "logit"), na.action = na.pass) summary(native) dat.gmm <- data.frame(cbind(y, 1, age, age2)) moments <- function(theta, data) { y <- as.numeric(data[, 1]) x <- data.matrix(data[, 2:dim(data)[2]]) m <- x * as.vector((y - plogis(x%*%theta))) return(cbind(m)) } my_gmm <- gmm(moments, x = dat.gmm, t0 = c(0,1,0), type = "iterative", crit = 1e-25, wmatrix = "optimal", method = "Nelder-Mead", control = list(reltol = 1e-25, maxit = 20000)) summary(my_gmm) my_gmm <- gmm(moments, x = dat.gmm, t0 = c(0,1,0), type = "twoStep", crit = 1e-25, wmatrix = "optimal", method = "Nelder-Mead", control = list(reltol = 1e-25, maxit = 20000)) summary(my_gmm)