I’d like to know the difference of parameters(intercept, slopes) between “glm” and “optim” in R. I think those predictions would be fine, but I can’t understand why those parameters are different. If it’s a misinterpretation, please give me some advice.
glm; -23.36, 46.72, 46.72
optim; -73.99506, 330.09424, 122.50453
#data
x1<-c(0,0,1,1)
x2<-c(0,1,0,1)
y<-c(0,1,1,1)
#glm
model<-glm(y~x1+x2,family=binomial(link=logit))
summary(model)
# Estimate Std. Error z value Pr(>|z|)
#(Intercept) -23.36 71664.47 0 1
#x1 46.72 101348.81 0 1
#x2 46.72 101348.81 0 1
round(fitted(model))
#0 1 1 1
#optim
f<-function(par){
eta<-par[1]+par[2]*x1+par[3]*x2
p<-1/(1+exp(-eta))
-sum(log(choose(1,y))+y*log(p)+(1-y)*log(1-p),na.rm=TRUE)
}
(optim<-optim(c(1,1,1),f))
$par
#-73.99506 330.09424 122.50453
round(1/(1+exp(-(optim$par[1]+optim$par[2]*x1+optim$par[3]*x2))))
#0 1 1 1