I still confuse my previous question on here1 and here2. About logLik of logistic regression in the case of proportion(=yes/yes+no). I try to validate it using optim() by following program. But it was not same. (I could check the same value in the case with “weight=n”). When estimating as the proportion without “weight=n”, I can’t understand how to estimate log-likelihood . Please give me some advice.
logLik() : -1.547104
optim : 2.474444
x<-c(2,3,5,6)
yes<-c(2,1,3,4)
no<-c(3,4,2,1)
n<-yes+no
yp<-yes/n
#-----glm
modelcp<- glm(yp~x,family=binomial)
(result<-summary(modelcp))
# Estimate Std. Error z value Pr(>|z|)
#(Intercept) -2.0608 3.0155 -0.683 0.494
#x 0.5152 0.7038 0.732 0.464
# Null deviance: 0.85152 on 3 degrees of freedom
#Residual deviance: 0.25523 on 2 degrees of freedom
logLik(modelcp)
#'log Lik.' -1.547104 (df=2)
#-----optim
f1<-function(para){
eta<-para[1]+para[2]*x
p<-1/(1+exp(-eta))
-sum(log(choose(1,yp))+yp*log(p)+(1-yp)*log(1-p),na.rm=TRUE)
}
(optim1<-optim(c(1,1),fn=f1,hessian=TRUE))
#$par
#[1] -2.0608361 0.5152331
#$value
#[1] 2.474444
it was same, “with weight = n”
#-----glm
modelcp<- glm(yp~x,family=binomial,weight=n)
(result<-summary(modelcp))
logLik(modelcp)
#'log Lik.' -4.548172 (df=2)
#-----optim
f1<-function(para){
eta<-para[1]+para[2]*x
p<-1/(1+exp(-eta))
-sum(log(choose(n,yes))+yes*log(p)+(n-yes)*log(1-p),na.rm=TRUE)
}
(optim1<-optim(c(1,1),fn=f1,hessian=TRUE))
#$value
#[1] 4.548172
my previous question1 :Difference between binary and count data of same data on logistic regression in R
my previous question2 :Difference between with and without “weight” option of the same data on logistic regression in R
glm
model should cause some concern: Warning message: In eval(family$initialize) : non-integer #successes in a binomial glm! $\endgroup$weight=n
. I am a little surprised that the "warning" isn't a show-stopping error message. $\endgroup$