I still keep checking from my previous question here. The next, I tried the case of proportion(=yes/yes+no), using previous best answer. Yes, I got it. But, I couldn’t understand the case without “weight=n”. When estimating as the proportion without “weight=n”, I can’t understand how to estimate deviance(or log-likelihood). please give me some advice.
x <- c(2, 3, 5, 6)
yes <- c(2, 1, 3, 4)
no <- c(3, 4, 2, 1)
n <- yes + no
yp <- yes / n
with “weight = n”
modelcp <- glm(yp ~ x,family = binomial, weight = n)
(result <- summary(modelcp))
# Estimate Std. Error z value Pr(>|z|)
#(Intercept) -2.0608 1.3486 -1.528 0.126
#x 0.5152 0.3147 1.637 0.102
# Null deviance: 4.2576 on 3 degrees of freedom
#Residual deviance: 1.2762 on 2 degrees of freedom
deviance(modelcp)
#[1] 1.276154
-2*logLik(modelcp)
#'log Lik.' 9.096343 (df=2)
#-----Using modified method of best answer on my previous question-----#
beta <- c(-2.0608, 0.5152)
logistic <- function(x) 1/(1+exp(-x)) # Common helper function
Lambda.0 <- function(beta, x, success, failure,y, with.binomial=TRUE) {
p <- logistic(beta[1]+beta[2]*x)
cnst <- ifelse(isTRUE(with.binomial), sum((lchoose((success+failure), success*n/(success + failure)))),0)
cnst+sum(n*(y*log(p)+(1-y)*log(1-p)))
}
-2 * Lambda.0(beta, x, yes, no, yp) # 9.096343: includes log binomial coefficients
-2 * Lambda.0(beta, x, yes, no, yp,with.binomial=FALSE) # 24.74444
sum(lchoose(n, yp*n)) * -2 # -15.64809 = 24.74444 - 9.096343
without “weight=n”
modelcpout <- glm(yp~x,family=binomial)
(result <- summary(modelcpout))
# 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
deviance(modelcpout)
#[1] 0.2552307
-2*logLik(modelcpout)
#'log Lik.' 3.094208 (df=2)
my previous question : Difference between binary and count data of same data on logistic regression in R