How to | Get Results for Fitted Models
When fitting data to a model, it is often important to obtain additional results to compare the data to the fitted function. You may wish to check the significance of parameters and assess the assumptions of the model, the influence of data points, and the goodness of fit. In Mathematica you can obtain these results directly from FittedModel objects returned by model fitting functions such as LinearModelFit, NonlinearModelFit, and GeneralizedLinearModelFit.
First, define a dataset to work with:
You can fit a simple linear regression model using LinearModelFit:
The output is a FittedModel object that contains information about the fitting and original data. You can get results and diagnostics directly from the object.
Use Normal to give the functional form of the fitted model:
You can also evaluate the model at specific points.
Evaluate the model as a function of rather than the original variable :
Evaluating at a numeric value gives the value predicted by the function at that numeric value of :
Because the FittedModel object evaluates like a function, you can plot it directly using Plot:
You can visualize the data points with the fitted function by using Show to combine a ListPlot of the data points with the previous plot:
FittedModel objects also contain many results and diagnostics that are relevant to the type of model they represent (in this case a linear model). You can evaluate the FittedModel with to get a full list of the available properties for that model.
Here Length is used to give the number of properties known for this model:
It is often important to ask whether or not the parameter estimates are statistically significant, that is, whether or not the parameter values are significantly different from 0. For instance, with a linear model you might look at the -value for a -statistic associated with that parameter. The -value is the probability of observing a -statistic at least as far from 0 as the one obtained.
Use with the FittedModel to display a table that summarizes the parameter estimates, -statistics, and -values:
You can use other properties to get the -statistics and -values individually. This is particularly useful if you want to compute other results based on those properties. Use with the FittedModel to view just the -values:
View multiple properties at once by entering a list of properties. View the -statistics and -values together:
You can also get confidence intervals for the parameters:
By default, 95% confidence intervals are returned. You can get other intervals with other levels of confidence by using the ConfidenceLevel option.
View the 99% confidence intervals for the parameters:
Additional pointwise diagnostics are also included, such as residuals.
Here, the residuals for the fitted model are computed and assigned to :
Visualize the residuals using ListPlot:
Though the available properties differ somewhat, results and diagnostics for nonlinear and generalized linear models can be obtained in the same way. You can fit models of these types by using NonlinearModelFit, GeneralizedLinearModelFit, LogitModelFit, or ProbitModelFit.
Define another dataset to work with:
Use NonlinearModelFit to fit the data to an exponential model:
Just as in the linear model case, use Show to combine the plots of the data points and the model:
You can also view tabular results for estimates at individual points. Use with the FittedModel to view such a table of predicted values with their error estimates and confidence intervals:
You can also visualize the associated prediction bands.
First, define functions for the 80%, 90%, 95%, and 99% prediction bands of the fitted function:
Visualize the regions bounded by the bands along with the fitted function and use Filling to more easily see the regions of each confidence level: