This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
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 Mathematica by using results from functions such as LinearModelFit together with built-in plotting functions.
First, define a dataset to work with:
Click for copyable input
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:
Click for copyable input
You can evaluate the FittedModel object at values of , so the fitted curve can be plotted using Plot for the function .
Plot the fitted function:
Click for copyable input
You can plot the data using ListPlot and use Show to display the data along with the fitted curve:
Click for copyable input
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:
Click for copyable input
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:
Click for copyable input
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:
Click for copyable input
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 :
Click for copyable input
Construct pairs of values and residuals using Transpose, and then plot the pairs using ListPlot:
Click for copyable input
Add a frame, stems, and labels to the plot:
Click for copyable input
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:
Click for copyable input
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:
Click for copyable input
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:
Click for copyable input
For example, use Exp with NonlinearModelFit to fit an exponential model to the data:
Click for copyable input
You can again use ListPlot and Plot to visualize the fitting:
Click for copyable input
As in the linear examples, you can also plot pointwise diagnostics such as residuals using ListPlot:
Click for copyable input
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 :
Click for copyable input
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:
Click for copyable input
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:
Click for copyable input
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:
Click for copyable input
Use Partition to make a 2×2 array and then display the plots in a grid using GraphicsGrid:
Click for copyable input