Mathematica HowTo
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[313]:=
Click for copyable input
Use LinearModelFit to construct a linear model for the data:
In[314]:=
Click for copyable input
Out[314]=
Extract the functional form of the model:
In[315]:=
Click for copyable input
Out[315]=
Plot the functional form of the model:
In[316]:=
Click for copyable input
Out[316]=
Show the data and the line of best fit:
In[317]:=
Click for copyable input
Out[317]=
Obtain information about the parameter estimates:
In[318]:=
Click for copyable input
Out[318]=
Extract and plot the standardized residuals and fit residuals:
In[319]:=
Click for copyable input
Out[320]=
Plot the Cook distances by observation number:
In[321]:=
Click for copyable input
Out[321]=
Alternatively, plot the Cook distances versus predictor value:
In[322]:=
Click for copyable input
Out[322]=
The previous examples demonstrated a selection of properties supported by LinearModelFit; many more are available:
In[323]:=
Click for copyable input
Out[323]=
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team