I would like to ask a question about the relationship between deviance, residuals, and log-likelihood in logistic regression. I'm currently fitting a logistic regression with a moderately sized data (N>300k). As far as I've known, residual deviance equals to -2 times log-likelihood, and it also equals to the sum of squared residuals of the regression model I fit.
I observed a weird result from my data, here is my code:
xnam <- "ns(ym,11)+as.factor(sex)+as.factor(m_edu)+as.factor(mage)+as.factor(ges)+as.factor(parity)"
mlist.form <- as.formula(paste('lbw ~', 'pm10_w + ', xnam, sep=''))
mod0 <- glm(formula = mlist.form, data = data.used, family = binomial(link='logit'))
mod0$deviance # 2704.049
sum(mod0$residuals ^2) # 1866549
logLik(mod0) # 'log Lik.' -1352.025 (df=24)
In my example, the sum of squared residuals is not the same as the residual deviance, but the residual deviance equals to the -2 times of log-likelihood.
But the more weird thing is my previous knowledge is confirmed in the small dataset like mtcars
.
data(mtcars)
mtcars <- as.data.frame(mtcars)
m1 <- glm(am ~ hp + wt, data =mtcars, family = binomial("logit"))
m1$deviance #10.05911 (residual deviance) = -2*log likelihood (lnL)
m1$aic #16.05911: -2*lnL + 2*k
m1$deviance + 2*3 #16.05911
sum(resid(m1)^2) #10.059110
I have no information that describes there is a relationship between the data size and the model fit. Could anyone explain the reason of such weird results?
Thank you.