0
$\begingroup$

I try to calculate deviance of normal linear regression:

set.seed(123)
n = 20
p = 3
y = rnorm(n)
x = matrix(rnorm(n*p),n,p)

glm1 = glm(y~x,family = gaussian)
glm1$deviance # == [1] 17.28482
glm1$null.deviance # == [1] 17.97548  

I know deviance $$ D_p =\frac{1}{\sigma^{2}} \sum_{i=1}^{N}\left(y_{i}-\widehat{y}_{i}\right)^{2} $$
where $p$ denotes number of parameter, $\hat{y_i}$ denotes fitted value.
I try to use $\frac{1}{N-p} \sum_{i=1}^{N}\left(y_{i}-\widehat{y}_{i}\right)^{2}$ to estimate $\sigma^{2}$, but I fail to produce the same result as deviance of glm1
And, I found

var(y) * 19 == glm1$null.deviance
sum(residuals(glm1)^2) == glm1$deviance

What's wrong here?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.