# How to | Plot Diagnostics for Fitted Models

Diagnostics are an important part of analyzing models of data. Plots of residuals and leverage or influence measures provide valuable insight into whether assumptions of the model are reasonable and whether there are data points that exert too much influence on the fitting. You can create these types of graphics in the Wolfram Language by using results from functions such as LinearModelFit together with built‐in plotting functions.

First, define a dataset to work with:

In[1]:= |

A linear model is a model that is linear in the parameters that need to be estimated. A model of the form is quadratic in , but is a linear model in the parameters , , and .

Use LinearModelFit to fit this model:

In[2]:= |

Out[2]= |

You can evaluate the FittedModel object at values of , so the fitted curve can be plotted using Plot for the function .

In[3]:= |

Out[3]= |

You can plot the data using ListPlot and use Show to display the data along with the fitted curve:

In[4]:= |

Out[4]= |

You can ask the FittedModel object for a number of diagnostics and results.

For a linear model, the residuals should look like white noise, random normally distributed values with mean 0 and a constant variance. Any trends in the residuals may indicate a need to modify the model.

Use the property with the FittedModel object () to obtain the residuals and plot them for each data point:

In[5]:= |

Out[5]= |

As with any other graphic, you can use additional options to add features such as frames, labels, and filling.

Use the Frame option to add a frame around the residual plot and the Filling option to draw stems from the points to the axis:

In[6]:= |

Out[6]= |

Standardized residuals can be useful because they effectively remove the overall scale in the residuals. In the case of linear and nonlinear regression, standardized residuals should look like white noise with variance equal to 1.

Generate a plot of standardized residuals for the fitted linear model:

In[7]:= |

Out[7]= |

While the previous residual plots display the residuals for each data point, it can also be useful to plot residuals against predictor variables. In the following example, is the predictor variable.

You can plot the residuals against a predictor variable by creating pairs from the data values and the associated residuals. The predictor values are the first elements of each data point in :

In[8]:= |

Out[8]= |

Construct pairs of values and residuals using Transpose, and then plot the pairs using ListPlot:

In[9]:= |

Out[9]= |

Add a frame, stems, and labels to the plot:

In[10]:= |

Out[10]= |

Similarly, measures of the influence or leverage of points in the dataset can be plotted to assess if any individual points have too much influence on the fitting.

You can, for instance, plot Cook distances for each data point. The Cook distance for a data point provides a measure of that point's influence on the overall fit:

In[11]:= |

Out[11]= |

Other pointwise diagnostics include and , which are often referred to as DFFITS and DFBETAS, respectively. These values can be visualized to assess other types of influence.

Plotting the provides a measure of each point's influence on the predicted values:

In[12]:= |

Out[12]= |

Diagnostic plots can be obtained in a similar manner for nonlinear and generalized linear models fitted using NonlinearModelFit, GeneralizedLinearModelFit, LogitModelFit, and ProbitModelFit.

The same values used for the linear model will now be used for nonlinear and generalized linear fitting:

In[1]:= |

For example, use Exp with NonlinearModelFit to fit an exponential model to the data:

In[2]:= |

Out[2]= |

You can again use ListPlot and Plot to visualize the fitting:

In[3]:= |

Out[3]= |

As in the linear examples, you can also plot pointwise diagnostics such as residuals using ListPlot:

In[4]:= |

Out[4]= |

The next example fits a model of the form , but assumes the responses (the values in the data) follow an inverse Gaussian distribution. This is specified by setting the ExponentialFamily option in GeneralizedLinearModelFit to :

In[5]:= |

Out[5]= |

Unlike in the linear and nonlinear regression models, the error variance is no longer expected to be constant. For an inverse Gaussian model, the errors are expected to increase as the response values increase.

Various types of transformed residuals can be used with these models, and you may want to view several at once. First, specify the residuals that you want to view together:

In[6]:= |

Just as in linear and nonlinear regression, are the differences between the response and predicted values. transforms the fit residuals such that they should be closer to normal noise for the assumed distribution. scales based on the estimated variance at each point, while scales out the overall variance.

Next, define the labels for each residual plot you are about to create:

In[7]:= |

Create plots for the different types of residuals and add the labels to each. The output is suppressed here using a semicolon () and the plots are displayed in a grid in the next step:

In[8]:= |

Use Partition to make a 2×2 array and then display the plots in a grid using GraphicsGrid:

In[9]:= |

Out[9]= |