Solved – Null deviance in glm R

deviancer

This is a question about the difference in calculating the null deviance in a simple Poisson model with and without an intercept.

If

y = c(2,3,6,7,8,9,10,12,15)
x = c(-1, -1, 0, 0, 0, 0, 1, 1, 1)

glm(y~x, family = poisson)

# Call:  glm(formula = y ~ x, family = poisson)
# 
# Coefficients:
# (Intercept)            x  
#      1.8893       0.6698  
# 
# Degrees of Freedom: 8 Total (i.e. Null);  7 Residual
# Null Deviance:      18.42 
# Residual Deviance: 2.939        AIC: 41.05

The null deviance can be calculated as follows:

lf = sum(y * log(y) - y - log(factorial(y)))
ln = sum(y * log(mean(y)) - mean(y) - log(factorial(y)))

2*(lf - ln)

# [1] 18.42061

If I fit the model without intercept:

glm(y~x - 1, family = poisson)

# Call:  glm(formula = y ~ x - 1, family = poisson)
# 
# Coefficients:
#     x  
# 2.373  
# 
# Degrees of Freedom: 9 Total (i.e. Null);  8 Residual
# Null Deviance:      191.9 
# Residual Deviance: 94.74        AIC: 130.9

The null deviance is now 191.9.

Can someone tell me how to calculate the null deviance for this model – I was under the impression that it would be the same as for the intercept model, i.e. a single parameter equal to the mean, but obviously it is not.

I presume I've incorrectly assumed that the null model is the same in both cases. Is it not or am I making a stupid mistake? I'd actually never considered this case before in any detail and there is obviously a gap in my knowledge somewhere.

I can get the null deviance as follows:

glm(y~1-1, family=poisson)

# Call:  glm(formula = y ~ 1 - 1, family = poisson)
# 
# No coefficients
# 
# Degrees of Freedom: 9 Total (i.e. Null);  9 Residual
# Null Deviance:      191.9
# Residual Deviance: 191.9        AIC: 226

but I don't know what this model is.

Apologies if this has been answered before but the only similar question I have seen (Why does the null deviance in glm.nb differ between models of the same response variable?) does not give an explicit explanation.

Best Answer

Thanks to @jbaums.

y = c(2,3,6,7,8,9,10,12,15)
x = c(-1, -1, 0, 0, 0, 0, 1, 1, 1)

For the no intercept null model we have: $$ Y \sim {\rm Poisson}(1) $$ leading to the following null model likelihood term: $$ \mathcal L(y) = \exp(-1)/y! $$ or loglikelihood term: $$ l(y) = -1 - \log(y!) $$ The log-likehood for the null model is then the sum of these terms:

lnull = sum(-1 - log(factorial(y))) 

As usual the log-likelhood for the saturated model is:

lf = sum(y * log(y) - y - log(factorial(y))) 

so the null deviance is:

2*(lf - lnull) 
# [1] 191.8602