Soft Independent Modeling of Class Analogy (SIMCA)

by

 

15 Mar 2011 (Updated )

M-files for classes modeling and prediction by SIMCA

simcapred.m
function simca_pred = simcapred(x,model,classes)
%
%
% Classes prediction using SIMCA model
%
% simca_pred = simcapred(x,model,classes)
% 
% input:
% x (samples x descriptors)	  new samples for prediction
% model (struct)              with SIMCA calibration parameters
% classes (samples x 1)       classes numbers (must be >0) (optional for model test)
% 
% output:
% simca_pred struct with:
%     class_p (samples x 1)   predicted classes for new samples
%     T2 (samples x 1)        T for new samples
%     Q2 (samples x 1)        Q for new samples
%     RMSEP (1 x 1)           Root Mean Square Error for Prediction (only if 'classes' is supplied)
%     R2 (1 x 1)              Correlation Coefficient for Prediction (only if 'classes' is supplied)
%     suc (1 x 1)             Success (%) of classification for new samples (only if 'classes' is supplied)
%     
% See also simca
%
% By Cleiton A. Nunes
% UFLA,MG,Brazil
%  
        
        

nclas=size(model.models,2);

for ic=1:nclas
    
L=model.models(ic).L;
nE=x*L;

[mx,nx]   = size(x);
[ml,nl]     = size(L);

rm=(x-nE*L')';
nr = (sum(rm.^2))';

eive=model.models(ic).ev;
if nl>1
  nt  = nE*inv(diag(eive(1:nl)));
  nts = sum((nt.^2)')';
else
  nts  = nE.^2/eive(1,1);
end

ts=model.models(ic).T2lim;
q=model.models(ic).Q2lim;

rt(:,ic) = nts/ts;
  rq(:,ic) = nr/q;
rtsum = sqrt(rt.^2 + rq.^2);
[mtq,ind] = min(rtsum');
class = ind';

u=model.stats.classes;
t=size(u,1);
sc=size(class,1);
    for i=1:sc
        for j=1:t
            if class(i)==j
            nnc(i)=u(j);
            end
        end
    end

T(:,ic)=nts;
Q(:,ic)=nr;
end

for ic=1:t
    for iam=1:mx
        if class(iam)==u(ic);
            T2(iam)=T(iam,ic);
            Q2(iam)=Q(iam,ic);
        end
    end
end
    
simca_pred.class_p=nnc';
simca_pred.T2=T2';
simca_pred.Q2=Q2';

if nargin==3
    RMSEP=sqrt(sum((nnc'-classes).^2)/size(classes,1));
    R=abs(nonzeros(diag((corrcoef(nnc',classes)),1)));
    ac=(size(nnc',1)-size(nonzeros(nnc'-classes),1))/size(nnc',1)*100;
    
    simca_pred.RMSEP=RMSEP;
    simca_pred.R2=R.^2;
    simca_pred.suc=ac;

end

Contact us