How to | Perform a Linear Regression

One of the most common statistical models is the linear regression model. A linear model predicts the value of a response variable by the linear combination of predictor variables or functions of predictor variables. In Mathematica, LinearModelFit returns an object that contains fitting information for a linear regression model and allows for easy extraction of results and diagnostics.

Simulate a dataset:

In[1]:=
Click for copyable input

Use LinearModelFit to construct a linear model for the data:

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

Extract the functional form of the model:

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

Plot the functional form of the model:

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

Show the data and the line of best fit:

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

Obtain information about the parameter estimates:

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

Extract and plot the standardized residuals and fit residuals:

In[8]:=
Click for copyable input
Out[9]=

Plot the Cook distances by observation number:

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

Alternatively, plot the Cook distances versus predictor value:

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

The previous examples demonstrated a selection of properties supported by LinearModelFit; many more are available:

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