I built a logistic regression in R using 6 predictor variables and the output is as shown:
fracmodel = glm(frx ~ age + meds + weight + hip_bmd + fall_risk + tneck,
family = binomial(link = 'logit'), data = fracture)
summary(fracmodel)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.782151 0.824666 -2.161 0.03069 *
age 0.013800 0.006251 2.208 0.02727 *
meds1 -0.200508 0.072979 -2.747 0.00601 **
weight 0.014985 0.003543 4.229 2.34e-05 ***
hip_bmd -3.278159 0.675582 -4.852 1.22e-06 ***
fall_risk1 0.219837 0.091021 2.415 0.01572 *
tneck -0.150665 0.105696 -1.425 0.15402
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 5173.2 on 6365 degrees of freedom
Residual deviance: 5055.7 on 6359 degrees of freedom
(93 observations deleted due to missingness)
AIC: 5069.7
I tried to derive MC facdden's R2 using an additional NULL model defined as below
nullmodel <- glm(frx~1, binomial(link = 'logit'), data = fracture)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.81177 0.03581 -50.59 <2e-16 ***
Null deviance: 5241.3 on 6458 degrees of freedom
Residual deviance: 5241.3 on 6458 degrees of freedom
AIC: 5243.3
#Pseudo R2
1- logLik(fracmodel)/logLik(nullmodel)
But if I had used the formula for R2 as R2 = 1- fracmodel$deviance/fracmodel$null.deviance
I should have obtained the same answer. But I'm not.
Then I noticed that the degrees of freedom for NULL Deviance are different for the fitted model and the NULL model. There are 6459 observations. DF for NULL deviance in fitted model is 6365 but for the NULL model is 6458.
What is the reason for the variation in degrees of freedom. Which one should I use to derive MCFadden's R squared?