# testDeviance

## Description

returns the `p`

= testDeviance(`mdl`

)*p*-value for a test that determines whether the fitted model
in the `MultinomialRegression`

model object `mdl`

fits significantly
better than an intercept-only model.

## Examples

### Perform Deviance Test

Load the `fisheriris`

sample data set.

`load fisheriris`

The column vector `species`

contains three iris flower species: setosa, versicolor, and virginica. The matrix `meas`

contains of four types of measurements for the flowers: the length and width of sepals and petals in centimeters.

Fit a multinomial regression model using `meas`

as the predictor data and `species`

as the response data.

mdl = fitmnr(meas,species);

`mdl`

is a multinomial regression model object that contains the results of fitting a nominal multinomial regression model to the data.

Perform a chi-squared test with the null hypothesis that an intercept-only model performs as well as the model `mdl`

.

p = testDeviance(mdl)

p = 7.0555e-64

The small *p*-value indicates that enough evidence exists to reject the null hypothesis and conclude that `mdl`

performs better than the intercept-only model.

### Get Test Statistic for Deviance Test

Load the `carbig`

sample data set.

`load carbig`

The variables `MPG`

and `Origin`

contain data for car mileage and country of origin, respectively.

Fit a multinomial regression model with `MPG`

as the predictor data and `Origin`

as the response. Estimate the dispersion parameter during the fitting.

mdl = fitmnr(MPG,Origin,EstimateDispersion=true);

`mdl`

is a multinomial regression model object that contains the results of fitting a nominal multinomial regression model to the data.

Perform an *F*-test with the null hypothesis that an intercept-only model fits the data as well as the model `mdl`

. Display the *p*-value and the *F*-statistic.

[p,tStats] = testDeviance(mdl)

p = 1.2314e-45

tStats = 39.1789

The small *p*-value indicates that enough evidence exists to reject the null hypothesis and conclude that `mdl`

performs better than the intercept-only model.

## Input Arguments

`mdl`

— Multinomial regression model object

`MultinomialRegression`

model object

Multinomial regression model object, specified as a `MultinomialRegression`

model object created with the `fitmnr`

function.

## Output Arguments

`p`

— Deviance test *p*-value

numeric scalar in the range [0,1]

Deviance test *p*-value, returned as a numeric scalar in the range
[0,1].

`testStat`

— Deviance test statistic

numeric scalar

Deviance test statistic, returned as a numeric scalar. If
`mdl.Dispersion`

is estimated, `testDeviance`

performs an *F*-test to determine whether the fitted model
`mdl`

fits better than an intercept-only model. If
`mdl.Dispersion`

is not estimated, `testDeviance`

performs a chi-squared test instead.

## 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 chi-squared 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 chi-squared 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
*p*-value 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 chi-squared 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.

## Alternative Functionality

`coefTest`

performs
an *F*-test to determine whether the coefficient estimates in
`mdl`

are zero. If you do not specify coefficients to test,
`coefTest`

tests whether the model `mdl`

is a better
fit to the data than a model with no coefficients.

## Version History

**Introduced in R2023a**

## See Also

## 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)