-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_outliers.m
72 lines (57 loc) · 1.7 KB
/
get_outliers.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function [idx_out,idx_in] = get_outliers(Data,out_fr,ncomp,mtd,show)
if ~exist('show','var')
show = 0;
end
if ~exist('T_fac','var')
out_fr = 0.01;
end
if ~exist('ncomp','var')
ncomp = 10;
end
if ~exist('mtd','var')
mtd = 'pca'; % 'pca', 'pls'
end
Y = Data(:,1);
X = Data(:,2:end);
if strcmp(mtd,'pca')
[~,score,~,~,var] = pca(X,'Centered',true);
XS = score(:,1:ncomp);
elseif strcmp(mtd,'pls')
[~,~,XS] = plsregress(X,Y,ncomp);
end
[~,~,mah,outliers] = robustcov(XS,...
'OutlierFraction',out_fr, 'ReweightingMethod','rmvn');
idx_out = find(outliers);
idx_in = find(outliers==0);
if show == 1
d_classical = pdist2(XS, mean(XS),'mahal');
p = size(XS,2);
chi2quantile = sqrt(chi2inv(0.999,p));
figure
plot(d_classical, mah, 'o')
line([chi2quantile, chi2quantile], [0, 30], 'color', 'r')
line([0, 6], [chi2quantile, chi2quantile], 'color', 'r')
hold on
plot(d_classical(outliers), mah(outliers), 'r+')
xlabel('Mahalanobis Distance')
ylabel('Robust Distance')
title('DD Plot, FMCD method')
hold off
d_classical1 = d_classical;
d1 = mah;
d_classical1(outliers) = [];
d1(outliers) = [];
figure
plot(d_classical1,d1, 'o')
line([0 5.5], [0, 5.5])
xlabel('Mahalanobis Distance')
ylabel('Robust Distance')
end
%
% mdl = fitlm(XS,Y, 'RobustOpts','on');
% plotResiduals(mdl,'probability')
% outliers_vals1 = (mdl.Diagnostics.CooksDistance);
% outliers_vals2 = (mdl.Diagnostics.Leverage);
% mean_med = mean([median(outliers_vals),mean(outliers_vals)]);
% outl = outliers_vals > T_fac*mean_med;
% outliers = find(outliers_vals > T_fac*mean_med);