# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# anova

Class: CompactLinearModel

Analysis of variance for linear model

## Syntax

`tbl = anova(mdl)tbl = anova(mdl,anovatype)tbl = anova(mdl,anovatype,sstype)`

## Description

`tbl = anova(mdl)` returns a table with summary ANOVA statistics.

`tbl = anova(mdl,anovatype)` returns ANOVA statistics of the chosen type.

`tbl = anova(mdl,anovatype,sstype)` computes ANOVA statistics using the chosen type of sum of squares.

## Input Arguments

expand all

Linear model object, specified as a full `LinearModel` object constructed using `fitlm` or `stepwiselm`, or a compacted `CompactLinearModel` object constructed using `compact`.

ANOVA type, specified as one of the following:

• `'component'``tbl` displays a ‘components' ANOVA table, with sums of squares and F tests attributable to each term in the model except the constant term.

• `'summary'``tbl` displays a summary ANOVA table with an F test for the model as a whole.

• If there are both linear and higher-order terms, there is also an F test for the higher-order terms as a group.

• If `mdl` is a full `LinearModel` object and the sample data contains replications (multiple observations sharing the same predictor values), there is also an F test for lack-of-fit computed by decomposing the residual sum of squares into a sum of squares for the replicated observations and the remaining sum of squares. If `mdl` is a compacted `CompactLinearModel` object, then the ANOVA table presents, but does not decompose, the residual sum of squares.

Sum of squares type, specified as one of the following:

ValueDescription
`1`Type I sum of squares. The reduction in residual sum of squares obtained by adding that term to a fit that already includes the terms listed before it.
`2`Type II sum of squares. The reduction in residual sum of squares obtained by adding that term to a model consisting of all other terms that do not contain the term in question
`3`Type III sum of squares. The reduction in residual sum of squares obtained by adding that term to a model containing all other terms, but with their effects constrained to obey the usual "sigma restrictions" that make models estimable.
`'h'`Hierarchical model. Similar to Type II, but uses both continuous and categorical factors to determine the hierarchy of terms.

For Type III sum of squares:

• If `mdl` is a `CompactLinearModel` object and the model is non-hierarchical, `anova` errors.

• If `mdl` is a full `LinearModel` object and the model is non-hierarchical, then `anova` refits the model using effects coding whenever it needs to compute Type III sums of squares.

• If `mdl` is either a `CompactLinearModel` or a `LinearModel` object and the model is hierarchical, `anova` computes the results without refitting the model.

`sstype` only applies if `anovatype` is `'component'`.

For more information about sum of squares types, see the `sstype` argument on the `anovan` function reference page.

## Output Arguments

expand all

ANOVA summary statistics table, returned as table

The contents of `tbl` depends on the ANOVA type specified in `anovatype`.

• If `anovatype` is `'component'`, then `tbl` contains:

• Sum of squares

• Degrees of freedom

• Mean squares

• F statistic

• p-value

• Formula used for model

• If `anovatype` is `'summary'`, then `tbl` contains:

• Total Sum of Squares

• Model Sum of Squares

• Linear Sum of Squares (present if model has powers or interactions)

• Nonlinear Sum of Squares (present if model has powers or interactions)

• Residual Sum of Squares

• Lack-of-fit Sum of Squares (present if model has replicates)

• Pure error Sum of Squares (present if model has replicates)

## Examples

expand all

Create a component ANOVA table from a model of the `carsmall` data.

Load the `carsmall` data and make a model of the MPG as a function of weight and model year.

```load carsmall cars = table(MPG,Weight); cars.Year = ordinal(Model_Year); mdl = fitlm(cars,'MPG ~ Year + Weight^2'); ```

Create an ANOVA table.

```tbl = anova(mdl) ```
```tbl = SumSq DF MeanSq F pValue ______ __ ______ ______ __________ Weight 2050.2 1 2050.2 265.11 1.9885e-28 Year 849.55 2 424.77 54.927 2.9042e-16 Weight^2 76.688 1 76.688 9.9164 0.0022303 Error 688.27 89 7.7334 ```

Create a summary ANOVA table from a model of the `carsmall` data.

Load the `carsmall` data and make a model of the MPG as a function of weight and model year.

```load carsmall cars = table(MPG,Weight); cars.Year = ordinal(Model_Year); mdl = fitlm(cars,'MPG ~ Year + Weight^2'); ```

Create a summary ANOVA table.

```tbl = anova(mdl,'summary') ```
```tbl = SumSq DF MeanSq F pValue ______ __ ______ ______ __________ Total 6005.3 93 64.573 Model 5317 4 1329.3 171.88 5.5208e-41 . Linear 5240.3 3 1746.8 225.87 1.7302e-41 . Nonlinear 76.688 1 76.688 9.9164 0.0022303 Residual 688.27 89 7.7334 . Lack of fit 663.77 86 7.7183 0.9451 0.62874 . Pure error 24.5 3 8.1667 ```

The summary ANOVA table shows tests for groups of terms. The nonlinear group consists of just the `Weight^2` term, so it has the same -value as that term in the component ANOVA table. The statistic comparing the residual sum of squares to a "pure error" estimate from replicated observations shows no evidence of lack of fit.

## Alternatives

More complete ANOVA statistics are available in the `anova1`, `anova2`, and `anovan` functions.

## More About

Was this topic helpful?

Download now