39
$\begingroup$

I have the following result from running glm function.

How can I interpret the following values:

  • Null deviance
  • Residual deviance
  • AIC

Do they have something to do with the goodness of fit? Can I calculate some goodness of fit measure from these result such as R-square or any other measure?

Call:
glm(formula = tmpData$Y ~ tmpData$X1 + tmpData$X2 + tmpData$X3 + 
    as.numeric(tmpData$X4) + tmpData$X5 + tmpData$X6 + tmpData$X7)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-0.52628  -0.24781  -0.02916   0.25581   0.48509  

Coefficients:
                     Estimate Std. Error  t value Pr(>|t|)    
(Intercept         -1.305e-01  1.391e-01   -0.938   0.3482    
tmpData$X1         -9.999e-01  1.059e-03 -944.580   <2e-16 ***
tmpData$X2         -1.001e+00  1.104e-03 -906.787   <2e-16 ***
tmpData$X3         -5.500e-03  3.220e-03   -1.708   0.0877 .  
tmpData$X4         -1.825e-05  2.716e-05   -0.672   0.5017    
tmpData$X5          1.000e+00  5.904e-03  169.423   <2e-16 ***
tmpData$X6          1.002e+00  1.452e-03  690.211   <2e-16 ***
tmpData$X7          6.128e-04  3.035e-04    2.019   0.0436 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

(Dispersion parameter for gaussian family taken to be 0.08496843)

    Null deviance: 109217.71  on 3006  degrees of freedom
Residual deviance:    254.82  on 2999  degrees of freedom
  (4970 observations deleted due to missingness)
AIC: 1129.8

Number of Fisher Scoring iterations: 2
$\endgroup$
2
  • 3
    $\begingroup$ I realize this was migrated from SO, where one would not normally look for information on these statistical terms. You have a great resource here! For example, see what you can learn from a search on some of your terms, like AIC. A little time spent doing this should either fully answer your question or at least guide you to asking a more specific one. $\endgroup$
    – whuber
    Dec 20, 2012 at 23:56
  • $\begingroup$ Not related to gaussian glms, but if you have a bernoulli glm fitted to binary data, you cannot use the residual deviance to assess the model fit, because it turns out the data cancels out in the deviance formula. Now, you can use the difference of residual deviances in that case to compare two models, but not the residual deviance itself. $\endgroup$ Jul 14, 2016 at 19:53

3 Answers 3

27
$\begingroup$

Use the Null Deviance and the Residual Deviance, specifically:

1 - (Residual Deviance/Null Deviance)

If you think about it, you're trying to measure the ratio of the deviance in your model to the null; how much better your model is (residual deviance) than just the intercept (null deviance). If that ratio is tiny, you're 'explaining' most of the deviance in the null; 1 minus that gets you your R-squared.

In your instance you'd get .998.

If you just call the linear model (lm) instead of glm it will explicitly give you an R-squared in the summary and you can see it's the same number.

With the standard glm object in R, you can calculate this as:

reg = glm(...)
with(summary(reg), 1 - deviance/null.deviance)
$\endgroup$
4
  • 1
    $\begingroup$ Does this pseudo-R have a specific name? I can't find it in the literature. $\endgroup$ Nov 2, 2020 at 9:46
  • 3
    $\begingroup$ @GiuliaMartini, I believe this is McFadden's pseudo r-squared $\endgroup$
    – Forrest
    Apr 13, 2021 at 0:58
  • $\begingroup$ So is a larger value or lower value preferred? $\endgroup$
    – Ken Lee
    May 28, 2021 at 16:08
  • $\begingroup$ @KenLee When the Residual Deviance is small relative to the Null, then the ratio will also be small and that proposed GOF measure would be closer to 1 (and "preferred"). There is no adjustment of that measure for model complexity. $\endgroup$
    – DWin
    Nov 23, 2021 at 1:05
19
$\begingroup$

The default error family for a glm model in (the language) R is Gaussian, so with the code submitted you are getting ordinary linear regression where $R^2$ is a widely accepted measure of "goodness of fit". The R glm function doesn't report the Nagelkerke-pseudo-"$R^2$" but rather the AIC (Akaike Information Criterion). In the case of an OLS model, the Nagelkerke GOF measure will be roughly the same as the $R^2$.

$$R^2_{\mathrm{GLM}}=1-\frac{(\sum_id_{i,\mathrm{model}}^2)^{2/N} }{(\sum_id_{i,\mathrm{null}}^2)^{2/N}} ~~~~~~~~.=.~~~~~~~~ 1-\frac{\mathit{SSE}/n[\mathrm{model}]}{\mathit{SST}/n[\mathrm{total}]} = R^2_{\mathrm{OLS}}$$

There is some debate about how such a measure on the LHS gets interpreted, but only when the models depart from the simpler Gaussian/OLS situation. But in GLMs where the link function may not be "identity", as was here, and the "squared error" may not have the same clear interpretation, so the Akaike Information Criterion is also reported because it appears to be more general. There are several other contenders in the GLM GOF sweepstakes with no clear winner.

You might want to consider not reporting a GOF measure if you are going to be using GLMs with other error structures: Which pseudo-$R^2$ measure is the one to report for logistic regression (Cox & Snell or Nagelkerke)?

$\endgroup$
3
  • 9
    $\begingroup$ Where exactly is the " Nagelkerke-pseudo-"R2"" in the above output? $\endgroup$
    – Tom
    Sep 25, 2013 at 2:57
  • 1
    $\begingroup$ I'm echoing Tom's question. Where in the output is the Nagelkerke-pseudo-"R2", or how do I find it? I'm not looking for more information about the value, but rather where I can find it in R's output. There's nothing in the question's example output that looks to me like a goodness of fit value in the range [0-1], so I'm confused. $\endgroup$
    – Kevin
    Apr 29, 2015 at 0:38
  • 1
    $\begingroup$ See stats.stackexchange.com/questions/8511/… and stackoverflow.com/questions/6242818/… ... I don't see any R^2 in either the glm object or the summary output. I may have been thinking of the usual output from rms summary functions, since that is my favorite modeling environment. $\endgroup$
    – DWin
    Apr 29, 2015 at 0:54
9
$\begingroup$

If you are running a binary logistic model, you can also run the Hosmer Lemeshow Goodness of Fit test on your glm() model. Using the ResourceSelection library.

library(ResourceSelection)

model <- glm(tmpData$Y ~ tmpData$X1 + tmpData$X2 + tmpData$X3 + 
           as.numeric(tmpData$X4) + tmpData$X5 + tmpData$X6 + tmpData$X7, family = binomial)

summary(model)
hoslem.test(model$y, model$fitted)
$\endgroup$
4
  • $\begingroup$ Though note that this is only works for binary dependent variable models (e.g. if OP had set family = "binomial. OP's example is linear regression. $\endgroup$
    – Matthew
    Jul 14, 2016 at 17:29
  • $\begingroup$ @Matthew This is true, I'm sorry I missed that. I've been using binary logistic regressions so much lately my brain just went to the hoslem.test() $\endgroup$
    – dylanjm
    Jul 14, 2016 at 17:42
  • $\begingroup$ Understandable :) I suggested an edit to your post but forgot to update the R code as well. You may want to change that just for clarity's sake. $\endgroup$
    – Matthew
    Jul 14, 2016 at 17:43
  • 1
    $\begingroup$ glm() now only offers model$fitted.values. Is this the same thing? $\endgroup$
    – Adam_G
    May 24, 2022 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.