Accelerating the pace of engineering and science

# anova

Class: LinearModel

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

 mdl Linear model, as constructed by fitlm or stepwiselm. anovatype ANOVA type: '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 there are 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. Default: 'component' sstype When anovatype is 'component', choose the sum of squares type: 123'h' For details, see Sum of Squares. Default: 'h'

## Output Arguments

 tbl Table containing summary ANOVA statistics. tbl depends on anovatype: 'component':Sum of squaresDegrees of freedomMean squaresF statisticp-valueFormula used for model'summary':Total Sum of SquaresModel Sum of SquaresLinear Sum of Squares (present if model has powers or interactions)Nonlinear Sum of Squares (present if model has powers or interactions)Residual Sum of SquaresLack-of-fit Sum of Squares (present if model has replicates)Pure error Sum of Squares (present if model has replicates)

## Examples

expand all

### Component ANOVA Table

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
```

### Summary ANOVA Table

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
______    __    ______    ______

Total            6005.3    93    64.573
Model              5317     4    1329.3    171.88
. Linear         5240.3     3    1746.8    225.87
. Nonlinear      76.688     1    76.688    9.9164
Residual         688.27    89    7.7334
. Lack of fit    663.77    86    7.7183    0.9451
. Pure error       24.5     3    8.1667

pValue
__________

Total
Model            5.5208e-41
. Linear         1.7302e-41
. Nonlinear       0.0022303
Residual
. Lack of fit       0.62874
. Pure error               ```

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 p-value as that term in Component ANOVA Table. The F 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.