# plotDiagnostics

Class: NonLinearModel

Plot diagnostics of nonlinear 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 leverage as the plot type.

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

`h = plotDiagnostics(...)` returns handles to the lines in the plot.

`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

`mdl`

Nonlinear regression model, constructed by `fitnlm`.

`plottype`

String specifying the type of plot:

 `'contour'` Residual vs. leverage with overlayed Cook's contours `'cookd'` Cook's distance `'leverage'` Leverage (diagonal of Hat matrix)

Default: `'leverage'`

### 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 the line or marker, a string or `ColorSpec` specification. For details, see `linespec`. `'LineStyle'` Type of line, a string or Chart Line Properties specification. For details, see `linespec`. `'LineWidth'` Width of the line or edges of filled area, in points, a positive scalar. One point is 1/72 inch. Default: `0.5` `'MarkerEdgeColor'` Color of the marker or edge color for filled markers, a string or `ColorSpec` specification. For details, see `linespec`. `'MarkerFaceColor'` Color of the marker face for filled markers, a string or `ColorSpec` specification. For details, see `linespec`. `'MarkerSize'` Size of the marker in points, a strictly positive scalar. One point is 1/72 inch.

## Output Arguments

 `h` Vector of handles to lines or patches in the plot.

## Definitions

### Hat Matrix

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

${J}_{i,j}={\frac{\partial f}{\partial {\beta }_{j}}|}_{{x}_{i},\beta }$

Here f is the nonlinear model function, and β is the vector of model coefficients.

The Hat Matrix H is

H = J(JTJ)–1JT.

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

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 ei is the ith residual.

## Examples

collapse all

### Nonlinear Model Leverage Plot

Create a leverage plot of a fitted nonlinear model, and find the points with high leverage.

Load the reaction data and fit a model of the reaction rate as a function of reactants.

```load reaction mdl = fitnlm(reactants,rate,@hougen,[1 .05 .02 .1 2]);```

Create a leverage plot of the fitted model.

`plotDiagnostics(mdl)`

To examine the observation with high leverage, activate the Data Cursor and click the observation.

Alternatively, find the high-leverage observation at the command line.

`find(mdl.Diagnostics.Leverage > 0.8)`
```ans = 6```

## References

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