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 the Wolfram Language, LinearModelFit returns an object that contains fitting information for a linear regression model and allows for easy extraction of results and diagnostics.
data = Table[{3 + i + RandomReal[{-3, 7}], i + RandomReal[{-2, 5}]}, {i, 1, 20}];Use LinearModelFit to construct a linear model for the data:
model = LinearModelFit[data, x, x]Extract the functional form of the model:
model["BestFit"]Plot the functional form of the model:
Plot[model["BestFit"], {x, 0, 20}]Show the data and the line of best fit:
Show[ListPlot[data], Plot[model["BestFit"], {x, 0, 30}]]Obtain information about the parameter estimates:
model["ParameterTable"]Extract and plot the standardized residuals and fit residuals:
{sr, fr} = model[{"StandardizedResiduals", "FitResiduals"}];
{{ListPlot[sr], ListPlot[fr]}}//GraphicsGridPlot the Cook distances by observation number:
ListPlot[model["CookDistances"], Filling -> Axis, FillingStyle -> Thick, PlotStyle -> Thick]Alternatively, plot the Cook distances versus predictor value:
ListPlot[Transpose[{data[[All, 1]], model["CookDistances"]}], Filling -> Axis, FillingStyle -> Thick, PlotStyle -> Thick]The previous examples demonstrated a selection of properties supported by LinearModelFit; many more are available:
model["Properties"]