How to remove outliers from a linear fit?

21 visualizaciones (últimos 30 días)
Thar
Thar el 28 de En. de 2017
Comentada: Star Strider el 31 de En. de 2017
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!

Respuesta aceptada

Star Strider
Star Strider el 28 de En. de 2017
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.
  9 comentarios
Thar
Thar el 31 de En. de 2017
I want to save them in different figures. I wrote savefig('graphk1.fig'),but save the first graph only.
Star Strider
Star Strider el 31 de En. de 2017
You have two options, to save them all in the same file:
H(1) = figure(1)
plot( . . . )
. . .
H(22) = figure(22)
plot( . . . )
savefig(H,'graphk.fig')
or to save every figure individually:
savefig('graphk01.fig')
savefig('graphk02.fig')
. . .
savefig('graphk22.fig')
I would save them all in the same file (the first option). See the documentation for savefig for details. The documentation says it was ‘Introduced in R2013b’, so if you have that or a later version, you should be able to do this.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by