forked from cab79/Matlab_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlda_class.m
68 lines (62 loc) · 2.37 KB
/
lda_class.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
function out = lda_class(dat,groups,nfold,ndec,perm)
% multivariate classification between two groups
% using linear discriminant analysis
dat=round(dat,ndec);
rm=all(isnan(dat),1) | all(diff(dat)==0); % remove nans and constants
dat(:,rm)=[];
if isempty(dat)
out.empty=1;
return
end
try
lda = fitcdiscr(dat,groups,'DiscrimType','linear');
catch
disp('trying pseudolinear LDA')
lda = fitcdiscr(dat,groups,'DiscrimType','pseudolinear');
end
ldaClass = resubPredict(lda);
% misclassification error (the proportion of misclassified observations) on the training set.
ldaResubErr = resubLoss(lda);
% confusion matrix
[ldaResubCM,grpOrder] = confusionmat(groups,ldaClass);
%Estimate the true test error for LDA using 10-fold stratified cross-validation.
cp = cvpartition(groups,'KFold',nfold);
cvlda = crossval(lda,'CVPartition',cp);
out.ldaCVErr = kfoldLoss(cvlda);
if perm
for i = 1:perm
disp(['LDA permutation ' num2str(i) '/' num2str(perm)])
groups = groups(randperm(length(groups)));
try
lda = fitcdiscr(dat,groups,'DiscrimType','linear');
catch
disp('trying pseudolinear LDA')
lda = fitcdiscr(dat,groups,'DiscrimType','pseudolinear');
end
ldaClass = resubPredict(lda);
% misclassification error (the proportion of misclassified observations) on the training set.
ldaResubErr = resubLoss(lda);
% confusion matrix
[ldaResubCM,grpOrder] = confusionmat(groups,ldaClass);
%Estimate the true test error for LDA using 10-fold stratified cross-validation.
cp = cvpartition(groups,'KFold',nfold);
cvlda = crossval(lda,'CVPartition',cp);
out.perm.ldaCVErr(i) = kfoldLoss(cvlda);
end
out.pval = sum(out.perm.ldaCVErr<out.ldaCVErr)/perm;
end
% clear 'ClassificationDiscriminant' ...
% 'classreg.learning.modelparams.DiscriminantParams' ...
% 'classreg.learning.modelparams.EnsembleParams' ...
% 'classreg.learning.generator.Partitioner' ...
% 'classreg.learning.classif.CompactClassificationDiscriminant' ...
% 'classreg.learning.FitTemplate' ...
% 'update' ...
% 'cvpartition' ...
% 'classreg.learning.internal.ClassLabel' ...
% 'classreg.learning.modifier.BlankModifier' ...
% 'classreg.learning.combiner.WeightedAverage' ...
% 'classreg.learning.impl.CompactEnsembleImpl'
%
% [M,X,C] = inmem;
% C