MATLAB: How to remove outliers from a linear fit

remove outliers

Hi all!
I have points P(xi,yi)and the linear fit y=ax+b. I want to remove the maximum outlier from the linear fit and I will do a new linear fit. Then to remove the maximum outlier and a new linear fit and so on, until I have the 50% of points P(xi,yi). Any ideas?
Thank you!

Best Answer

My idea:
x = 1:100; % Create Data

y = randi(99, 1, 100); % Create Data
nr_pts_to_remove = fix(length(x)/2);
for k1 = 1:nr_pts_to_remove
dm = [ones(size(x(:))) x(:)]; % Design Matrix
b = dm\y(:); % Estimate Parameters
yfit = dm*b; % Fit Data
[~,idx] = max(abs(y(:) - yfit)); % Index Of Maximum Residual
x(idx) = []; % Remove Maximum ‘x’
y(idx) = []; % Remove Maximum ‘y’
end
xv = linspace(min(x), max(x)); % Create Vectors For Plot

yv = [ones(size(xv(:))) xv(:)]*b; % Create Vectors For Plot
figure(1)
plot(x, y, 'pg', 'MarkerFaceColor','g')
hold on
plot(xv, yv, '-r')
hold off
grid
The ‘(:)’ creates column vectors for all data and other variables.