devianceTest
Analysis of deviance for generalized linear regression model
Syntax
Description
Examples
Perform Deviance Test
Perform a deviance test on a generalized linear regression model.
Generate sample data using Poisson random numbers with two underlying predictors X(:,1)
and X(:,2)
.
rng('default') % For reproducibility rndvars = randn(100,2); X = [2 + rndvars(:,1),rndvars(:,2)]; mu = exp(1 + X*[1;2]); y = poissrnd(mu);
Create a generalized linear regression model of Poisson data.
mdl = fitglm(X,y,'y ~ x1 + x2','Distribution','poisson')
mdl = Generalized linear regression model: log(y) ~ 1 + x1 + x2 Distribution = Poisson Estimated Coefficients: Estimate SE tStat pValue ________ _________ ______ ______ (Intercept) 1.0405 0.022122 47.034 0 x1 0.9968 0.003362 296.49 0 x2 1.987 0.0063433 313.24 0 100 observations, 97 error degrees of freedom Dispersion: 1 Chi^2statistic vs. constant model: 2.95e+05, pvalue = 0
Test whether the model differs from a constant in a statistically significant way.
tbl = devianceTest(mdl)
tbl=2×4 table
Deviance DFE chi2Stat pValue
__________ ___ __________ ______
log(y) ~ 1 2.9544e+05 99
log(y) ~ 1 + x1 + x2 107.4 97 2.9533e+05 0
The small pvalue indicates that the model significantly differs from a constant. Note that the model display of mdl
includes the statistics shown in the second row of the table.
Input Arguments
mdl
— Generalized linear regression model
GeneralizedLinearModel
object  CompactGeneralizedLinearModel
object
Generalized linear regression model, specified as a GeneralizedLinearModel
object created using fitglm
or stepwiseglm
, or a CompactGeneralizedLinearModel
object created using compact
.
Output Arguments
tbl
— Analysis of deviance summary statistics
table
Analysis of deviance summary statistics, returned as a table.
tbl
contains analysis of deviance statistics for both a
constant model and the model mdl
. The table includes these columns
for each model.
Column  Description 

Deviance  Deviance is twice the difference between the loglikelihoods of the
corresponding model ( 
DFE  Degrees of freedom for the error (residuals), equal to n – p, where n is the number of observations, and p is the number of estimated coefficients 
chi2Stat  Fstatistic or chisquared statistic, depending on whether the dispersion is estimated (Fstatistic) or not (chisquared statistic)

pValue  pvalue associated with the test: chisquared
statistic with p – 1 degrees of freedom, or Fstatistic with p – 1 numerator degrees of freedom and 
More About
Deviance
Deviance is a generalization of the residual sum of squares. It measures the goodness of fit compared to a saturated model.
The deviance of a model M_{1} is twice the difference between the loglikelihood of the model M_{1} and the saturated model M_{s}. A saturated model is a model with the maximum number of parameters that you can estimate.
For example, if you have n observations (y_{i}, i = 1, 2, ..., n) with potentially different values for X_{i}^{T}β, then you can define a saturated model with n parameters. Let L(b,y) denote the maximum value of the likelihood function for a model with the parameters b. Then the deviance of the model M_{1} is
$$2\left(\mathrm{log}L\left({b}_{1},y\right)\mathrm{log}L\left({b}_{S},y\right)\right),$$
where b_{1} and b_{s} contain the estimated parameters for the model M_{1} and the saturated model, respectively. The deviance has a chisquared distribution with n – p degrees of freedom, where n is the number of parameters in the saturated model and p is the number of parameters in the model M_{1}.
Assume you have two different generalized linear regression models M_{1} and M_{2}, and M_{1} has a subset of the terms in M_{2}. You can assess the fit of the models by comparing their deviances D_{1} and D_{2}. The difference of the deviances is
$$\begin{array}{l}D={D}_{2}{D}_{1}=2\left(\mathrm{log}L\left({b}_{2},y\right)\mathrm{log}L\left({b}_{S},y\right)\right)+2\left(\mathrm{log}L\left({b}_{1},y\right)\mathrm{log}L\left({b}_{S},y\right)\right)\\ \text{\hspace{1em}}\text{\hspace{1em}}\text{\hspace{1em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}=2\left(\mathrm{log}L\left({b}_{2},y\right)\mathrm{log}L\left({b}_{1},y\right)\right).\end{array}$$
Asymptotically, the difference D has a chisquared distribution with
degrees of freedom v equal to the difference in the number of parameters
estimated in M_{1} and
M_{2}. You can obtain the
pvalue for this test by using 1 —
chi2cdf(D,v)
.
Typically, you examine D using a model M_{2} with a constant term and no predictors. Therefore, D has a chisquared distribution with p – 1 degrees of freedom. If the dispersion is estimated, the difference divided by the estimated dispersion has an F distribution with p – 1 numerator degrees of freedom and n – p denominator degrees of freedom.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2012a
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)