%JKNIFECLASS Jacknife classification
%function [confmat, srmat]=jknifeclass(trndat,grp)
%
%Leave-one-out jacknife classification to test training data classification
%TRNDAT: Training data matrix with columns of attributes. 
%GRP: column of group (class) code (integers), with same number of rows
%as training data.
%CONFMAT: Classification confusion matrix giving the conditional
%         probability Pr(true group | predicted group)
%         Rows correspond to predicted group, columns to true group.
%         E.g. confmat(2,3) is the probability that the true group =3
%         when the predicted group =2.
%SRMAT:   Classification success ratio matrix giving the conditional
%         probability Pr(predicted group | true group).
%         Rows correspond to true group, columns to predicted group.
%         E.g. srmat(2,3) is the probability that the predicted group =3
%         when the true group =2.
%The diagonal diag(srmat)*100 gives the percent correct classification for each group.
%Classification done by Mahalanobis distance.
%Uses the statistical toolbox.
%See also CLASSIFY

%Written by T. Mukerji

nclass=max(grp); ndat=size(trndat,1); pctcorr=zeros(1,nclass);
ncorr=pctcorr; nmiss=ncorr; confmat=zeros(nclass,nclass); srmat=confmat;

randdx=randperm(ndat); trndat=trndat(randdx,:); grp=grp(randdx);

for kk=1:nclass, numclass(kk)=sum(grp==kk); numclass=numclass(:)'; end;
for k=1:ndat
dx=[1:ndat]~=k;
samplek=trndat(k,:); trndatk=trndat(dx,:); grpk=grp(dx);
clss(k)=classify(samplek,trndatk,grpk);
end;
clss=clss(:);
for pred=1:nclass
 for tru=1:nclass
   confmat(pred,tru) = sum((clss==pred) & (grp==tru))./sum(clss==pred);
   srmat(tru,pred) = sum((grp==tru) & (clss==pred))./sum(grp==tru);
end;end;

if nargout==0
bar(1:nclass,100*diag(srmat)); ylim([0 100]); ylabel('success %')
confmat
srmat
end;
