MATLAB Examples

Robust Regression versus Standard Least-Squares Fit

This example shows how to use robust regression. It compares the results of a robust fit to a standard least-squares fit.

Contents

Step 1. Prepare data.

Load the moore data. The data is in the first five columns, and the response in the sixth.

load moore
X = [moore(:,1:5)];
y = moore(:,6);

Step 2. Fit robust and nonrobust models.

Fit two linear models to the data, one using robust fitting, one not.

mdl = fitlm(X,y); % not robust
mdlr = fitlm(X,y,'RobustOpts','on');

Step 3. Examine model residuals.

Examine the residuals of the two models.

subplot(1,2,1)
plotResiduals(mdl,'probability')
subplot(1,2,2)
plotResiduals(mdlr,'probability')

The residuals from the robust fit (right half of the plot) are nearly all closer to the straight line, except for the one obvious outlier.

Step 4. Remove the outlier from the standard model.

Find the index of the outlier. Examine the weight of the outlier in the robust fit.

[~,outlier] = max(mdlr.Residuals.Raw);
mdlr.Robust.Weights(outlier)
ans =

    0.0246

Check the median weight.

median(mdlr.Robust.Weights)
ans =

    0.9718

This weight of the outlier in the robust fit is much less than a typical weight of an observation.