dat <- as.matrix(cbind(data.frame(IsVersicolor = as.numeric(iris$Species == "versicolor"), Intercept=1), iris[,1:4]))
head(dat)
# IsVersicolor Intercept Sepal.Length Sepal.Width Petal.Length Petal.Width
# [1,] 0 1 5.1 3.5 1.4 0.2
# [2,] 0 1 4.9 3.0 1.4 0.2
# [3,] 0 1 4.7 3.2 1.3 0.2
# [4,] 0 1 4.6 3.1 1.5 0.2
# [5,] 0 1 5.0 3.6 1.4 0.2
# [6,] 0 1 5.4 3.9 1.7 0.4
dat <- as.matrix(cbind(data.frame(IsVersicolor = as.numeric(iris$Species == "versicolor"), Intercept=1), iris[,1:4]))
head(dat)
# IsVersicolor Intercept Sepal.Length Sepal.Width Petal.Length Petal.Width
# [1,] 0 1 5.1 3.5 1.4 0.2
# [2,] 0 1 4.9 3.0 1.4 0.2
# [3,] 0 1 4.7 3.2 1.3 0.2
# [4,] 0 1 4.6 3.1 1.5 0.2
# [5,] 0 1 5.0 3.6 1.4 0.2
# [6,] 0 1 5.4 3.9 1.7 0.4
summary(glm(IsVersicolor~., data=as.data.frame(dat[,-2]), family="binomial"))
# Coefficients:
# Estimate Std. Error z value Pr(>|z|)
# (Intercept) 7.3785 2.4993 2.952 0.003155 **
# Sepal.Length -0.2454 0.6496 -0.378 0.705634
# Sepal.Width -2.7966 0.7835 -3.569 0.000358 ***
# Petal.Length 1.3136 0.6838 1.921 0.054713 .
# Petal.Width -2.7783 1.1731 -2.368 0.017868 *
summary(glm(IsVersicolor~., data=as.data.frame(dat[,-2]), family="binomial"))
# Coefficients:
# Estimate Std. Error z value Pr(>|z|)
# (Intercept) 7.3785 2.4993 2.952 0.003155 **
# Sepal.Length -0.2454 0.6496 -0.378 0.705634
# Sepal.Width -2.7966 0.7835 -3.569 0.000358 ***
# Petal.Length 1.3136 0.6838 1.921 0.054713 .
# Petal.Width -2.7783 1.1731 -2.368 0.017868 *
moments <- function(b, X) {
A <- 1
as.vector(X[,1] - A / (1 + exp(-(X[,-1] %*% cbind(b))))) * X[,-1]
}
moments <- function(b, X) {
A <- 1
as.vector(X[,1] - A / (1 + exp(-(X[,-1] %*% cbind(b))))) * X[,-1]
}
init.coef <- lm(IsVersicolor~., data=as.data.frame(dat[,-2]))$coefficients
library(gmm)
fitted <- gmm(moments, x = dat, t0 = init.coef,
type = "iterative", crit = 1e-25,wmatrix = "optimal", method = "Nelder-Mead",
control = list(reltol = 1e-25, maxit = 20000))
fitted
# (Intercept) Sepal.Length Sepal.Width Petal.Length Petal.Width
# 7.37849 -0.24536 -2.79657 1.31364 -2.77834
#
# Convergence code = 0
init.coef <- lm(IsVersicolor~., data=as.data.frame(dat[,-2]))$coefficients
library(gmm)
fitted <- gmm(moments, x = dat, t0 = init.coef, type = "iterative", crit = 1e-25,
wmatrix = "optimal", method = "Nelder-Mead",
control = list(reltol = 1e-25, maxit = 20000))
fitted
# (Intercept) Sepal.Length Sepal.Width Petal.Length Petal.Width
# 7.37849 -0.24536 -2.79657 1.31364 -2.77834
#
# Convergence code = 0