# plotDiagnostics

Class: LinearModel

Plot diagnostics of linear regression model

## Syntax

`plotDiagnostics(mdl)plotDiagnostics(mdl,plottype)h = plotDiagnostics(___)h = plotDiagnostics(mdl,plottype,Name,Value)`

## Description

`plotDiagnostics(mdl)` plots diagnostics from the `mdl` linear model using the leverage values.

`plotDiagnostics(mdl,plottype)` plots diagnostics in a plot of type `plottype`.

`h = plotDiagnostics(___)` returns handles to the lines in the plot, using any of the previous syntaxes.

`h = plotDiagnostics(mdl,plottype,Name,Value)` plots with additional options specified by one or more `Name,Value` pair arguments.

## Tips

• For many plots, the Data Cursor tool in the figure window displays the x and y values for any data point, along with the observation name or number.

## Input Arguments

expand all

### `mdl` — Full, fitted linear regression model`LinearModel` object

Full, fitted linear regression model, specified as a `LinearModel` object constructed using `fitlm` or `stepwiselm`.

### `plottype` — Type of plot`'leverage'` (default) | `'contour'` | `'cookd'` | `'covratio'` | `'dfbetas'` | `'dffits'` | `'s2_i'`

Type of plot, specified as one of the following:

 `'contour'` Residual vs. leverage with overlayed Cook's contours `'cookd'` Cook's distance `'covratio'` Delete-1 ratio of determinant of covariance `'dfbetas'` Scaled delete-1 coefficient estimates `'dffits'` Scaled delete-1 fitted values `'leverage'` Leverage `'s2_i'` Delete-1 variance estimate

Delete-1 means compute a new model without the current observation. If the delete-1 calculation differs significantly from the model using all observations, then the observation is influential.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

 Note:   The plot property name-value pairs apply to the first returned handle `h(1)`.

### `'Color'` — Color of line or markerstring | `ColorSpec` specification

Color of the line or marker, specified as the comma-separated pair consisting of `'Color'` and a string or `ColorSpec` specification. For details, see `linespec`.

### `'LineStyle'` — Line stylestring | `Chart Line Properties` specification

Line style, specified as the comma-separated pair consisting of `'LineStyle'` and a string or Chart Line Properties specification. For details, see `linespec`.

### `'LineWidth'` — Width of line or edges0.5 (default) | positive numeric value

Width of the line or edges of filled area, in points, specified as the comma-separated pair consisting of `'LineWidth'` and a positive numeric value. One point is equal to 1/72 inch.

### `'MarkerEdgeColor'` — Color of marker or edgestring | `ColorSpec` specification

Color of the marker or edge color for filled markers, specified as the comma-separated pair consisting of `'MarkerEdgeColor'` and a string or `ColorSpec` specification. For details, see `linespec`.

### `'MarkerFaceColor'` — Color of marker facestring | `ColorSpec` specification

Color of the marker face for filled markers, specified as the comma-separated pair consisting of `'MarkerFaceColor'` and a string or `ColorSpec` specification. For details, see `linespec`.

### `'MarkerSize'` — Size of markerpositive numeric value

Size of the marker in points, specified as the comma-separated pair consisting of `'MarkerSize'` and a positive numeric value. One point is 1/72 inch.

## Output Arguments

expand all

### `h` — Graphics handlesvector of graphics handle objects

Graphics handles, returned as a vector of graphics handles corresponding to the lines or patches in the plot.

## Definitions

### Hat Matrix

The hat matrix H is defined in terms of the data matrix X:

H = X(XTX)–1XT.

The diagonal elements hii satisfy

`$\begin{array}{l}0\le {h}_{ii}\le 1\\ \sum _{i=1}^{n}{h}_{ii}=p,\end{array}$`

where n is the number of observations (rows of X), and p is the number of coefficients in the regression model.

### Leverage

The leverage of observation i is the value of the ith diagonal term, hii, of the hat matrix H. Because the sum of the leverage values is p (the number of coefficients in the regression model), an observation i can be considered to be an outlier if its leverage substantially exceeds p/n, where n is the number of observations.

### Cook's Distance

Cook's distance is the scaled change in fitted values. Each element in `CooksDistance` is the normalized change in the vector of coefficients due to the deletion of an observation. The Cook's distance, Di, of observation i is

`${D}_{i}=\frac{\sum _{j=1}^{n}{\left({\stackrel{^}{y}}_{j}-{\stackrel{^}{y}}_{j\left(i\right)}\right)}^{2}}{p\text{\hspace{0.17em}}MSE},$`

where

• ${\stackrel{^}{y}}_{j}$ is the jth fitted response value.

• ${\stackrel{^}{y}}_{j\left(i\right)}$ is the jth fitted response value, where the fit does not include observation i.

• MSE is the mean squared error.

• p is the number of coefficients in the regression model.

Cook's distance is algebraically equivalent to the following expression:

`${D}_{i}=\frac{{r}_{i}^{2}}{p\text{\hspace{0.17em}}MSE}\left(\frac{{h}_{ii}}{{\left(1-{h}_{ii}\right)}^{2}}\right),$`

where ri is the ith residual, and hii is the ith leverage value.

`CooksDistance` is an n-by-1 column vector in the `Diagnostics` table of the `LinearModel` object.

## Examples

expand all

### Leverage Plot of Linear Model

Plot the leverage values of observations in a fitted model.

Load the `carsmall` data and fit a linear model of the mileage as a function of model year, weight, and weight squared.

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

Plot the leverage values.

```plotDiagnostics(mdl) ```

Plot the Cook's distance.

```plotDiagnostics(mdl,'cookd') ```

The two diagnostic plots give different results.

## References

[1] Neter, J., M. H. Kutner, C. J. Nachtsheim, and W. Wasserman. Applied Linear Statistical Models, Fourth Edition. Irwin, Chicago, 1996.

## Alternatives

The `mdl.Diagnostics` property contains the information that `plotDiagnostics` uses to create plots.