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:

In[11]:=
Click for copyable input

You can fit a simple linear regression model using LinearModelFit:

In[12]:=
Click for copyable input
Out[12]=

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:

In[13]:=
Click for copyable input
Out[13]=

You can also evaluate the model at specific points.

Evaluate the model as a function of rather than the original variable :

In[14]:=
Click for copyable input
Out[14]=

Evaluating at a numeric value gives the value predicted by the function at that numeric value of :

In[24]:=
Click for copyable input
Out[24]=

Because the FittedModel object evaluates like a function, you can plot it directly using Plot:

In[25]:=
Click for copyable input
Out[25]=

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:

In[26]:=
Click for copyable input
Out[26]=

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:

In[27]:=
Click for copyable input
Out[27]=

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:

In[28]:=
Click for copyable input
Out[28]=

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:

In[7]:=
Click for copyable input
Out[7]=

View multiple properties at once by entering a list of properties. View the -statistics and -values together:

In[10]:=
Click for copyable input
Out[10]=

You can also get confidence intervals for the parameters:

In[11]:=
Click for copyable input
Out[11]=

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:

In[12]:=
Click for copyable input
Out[12]=

Additional pointwise diagnostics are also included, such as residuals.

Here, the residuals for the fitted model are computed and assigned to :

In[13]:=
Click for copyable input
Out[13]=

Visualize the residuals using ListPlot:

In[14]:=
Click for copyable input
Out[14]=
    

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:

In[15]:=
Click for copyable input

Use NonlinearModelFit to fit the data to an exponential model:

In[16]:=
Click for copyable input
Out[16]=

Just as in the linear model case, use Show to combine the plots of the data points and the model:

In[17]:=
Click for copyable input
Out[17]=

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:

In[18]:=
Click for copyable input
Out[18]=

You can also visualize the associated prediction bands.

First, define functions for the 80%, 90%, 95%, and 99% prediction bands of the fitted function:

In[19]:=
Click for copyable input

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:

In[20]:=
Click for copyable input
Out[20]=
New to Mathematica? Find your learning path »
Have a question? Ask support »