
%three groups



load Fl15


cxyz=[];


 
    
%grupos: 1 control, 2 dcl, 3 enfermo
g=[repmat(1,1,12) repmat(2,1,6) repmat(3,1,10)];



nrep=28;

for i=1:nrep
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'   ];
    
end

xmi=cxyz;


 %leave-one-out 
     
 
     e=[];
  for ni=1:23
  lni=ni;

errores=0;
erroresq=0;


for i=1:28

 indi=ones(28,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');



errores=errores + double(c1~=g(i));


if(c1~=g(i))

[i,c1]

end
end

e=[e errores]; %

  end
  
  e
  
 

  
  e =

     5     4     6     6     5     3     4     4     4     4     4     3     3     4     4     4     4     4     4     5     5     8     8

     ni=8
     
     ans =

    14     1


ans =

    15     3


ans =

    18     1


ans =

    20     2
     
     %Elegir cunatas componentes
     %Selecting the number of components
  
     
     cll=zeros(28,23); %matriz de clasificacion por ni

erl=zeros(28,23); %matriz de errores global:  matrix with errors

      for ni=1:23
  lni=ni;

errores=0;
erroresq=0;


for i=1:28

 indi=ones(28,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');


cll(i,ni)=c1;



errores=0;
grupo=g(indi);
for j=1:27
    
    indi2=ones(27,1);
    
    indi2(j)=0;
   indi2=logical(indi2);
[c1,e1]=classify(coica(j,:),coica(indi2,:),grupo(indi2),'linear','empirical');
errores=errores + double(c1~=grupo(j));

end

erl(i,ni)=errores;


end

 %

  end
  
 
     
      
figure
plot(1:23,sum(erl,1)) % mínimo en  8 (4 errors) 

sum(erl,1)


   153   114   152   161   121    91   108    74   109   120   124   140   184   168   147   171   183   249   334   347   366   385   432

%%%%%%%%%%%%%%%%%

%CN-MCI

  
cxyz=[];


 
    
%grupos: 1 control, 2 dcl, 3 enfermo
g=[repmat(1,1,12) repmat(2,1,6) repmat(3,1,10)];



nrep=18;

for i=1:nrep
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'   ];
    
end

xmi=cxyz;


%leave-one out :
     
     
     e=[];
  for ni=1:14
  lni=ni;

errores=0;
erroresq=0;


for i=1:18

 indi=ones(18,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');



errores=errores + double(c1~=g(i));


if(c1~=g(i))

[i,c1]

end
end

e=[e errores]; %

  end
  
 
     e =

     5     6     5     3     2     3     3     2     3     2     2     3     3     2

     ni=5
     
     ans =

    14     1


ans =

    15     1
     
%Elegir cunatas componentes: No. components
  
     
     cll=zeros(18,14); %matriz de clasificacion por ni

erl=zeros(18,14); %matriz de errores global

      for ni=1:14
  lni=ni;

errores=0;
erroresq=0;


for i=1:18

 indi=ones(18,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');


cll(i,ni)=c1;


errores=0;
grupo=g(indi);
for j=1:17
    
    indi2=ones(17,1);
    
    indi2(j)=0;
   indi2=logical(indi2);
[c1,e1]=classify(coica(j,:),coica(indi2,:),grupo(indi2),'linear','empirical');
errores=errores + double(c1~=grupo(j));

end

erl(i,ni)=errores;


end

 %

  end
  
 
     
      % plot(1:24,sum(er,1)) % 
figure
plot(1:14,sum(erl,1)) % mínimo en  5 
sum(erl,1)

83    88    54    27    23    40    50    49    60    73    79    80    78    99


%%%%%%%%%%
%
% CN- AD



cxyz=[];


 load Fl15
    
%grupos: 1 control, 2 dcl, 3 enfermo
g=[repmat(1,1,12) repmat(2,1,6) repmat(3,1,10)];



do=[1:12 19:28];

g=g(do);

for i=do
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'   ];
    
end

xmi=cxyz;

  %leave-one out
   
     
     e=[];
  for ni=1:17
  lni=ni;

errores=0;
erroresq=0;


for i=1:22

 indi=ones(22,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');


errores=errores + double(c1~=g(i));



if(c1~=g(i))

[i,c1]

end
end

e=[e errores]; %

  end
  
 
   e =

     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

     
%Elegir cuantas componentes: No. components
  
     
     cll=zeros(22,17); %matriz de clasificacion por ni

erl=zeros(22,17); %matriz de errores global

      for ni=1:17
  lni=ni;

errores=0;
erroresq=0;


for i=1:22

 indi=ones(22,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');


cll(i,ni)=c1;


errores=0;
grupo=g(indi);
for j=1:21
    
    indi2=ones(21,1);
    
    indi2(j)=0;
   indi2=logical(indi2);
[c1,e1]=classify(coica(j,:),coica(indi2,:),grupo(indi2),'linear','empirical');
errores=errores + double(c1~=grupo(j));

end

erl(i,ni)=errores;



end

 %

  end
  
 
     
      % plot(1:24,sum(er,1)) % 
figure
plot(1:17,sum(erl,1)) % mínimo en  1, most parsimonious

sum(erl,1)

 0     0     0     0     0     1     2     2     3     8     8     8     8    33    49    89   119


%%%%%%%%%%%%
%MCI-AD


    
cxyz=[];


 
    
%grupos: 1 control, 2 dcl, 3 enfermo
g=[ repmat(2,1,6) repmat(3,1,10)];



nrep=18;

for i=13:28
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'   ];
    
end

xmi=cxyz;

%leave-one out 
     
     
     e=[];
  for ni=1:12
  lni=ni;

errores=0;
erroresq=0;


for i=1:16

 indi=ones(16,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');



errores=errores + double(c1~=g(i));


if(c1~=g(i))

[i+12,c1]

end
end

e=[e errores]; %

  end
  
 
     e =

     7     8     2     2     2     3     3     2     2     3     3     3
     
     ni=3
     ans =

     13    3


ans =

     15    3
     
     
     
%Elegir cuantas componentes: No. components
  
     
     cll=zeros(16,12); %matriz de clasificacion por ni

erl=zeros(16,12); %matriz de errores global

      for ni=1:12
  lni=ni;

errores=0;
erroresq=0;


for i=1:16

 indi=ones(16,1);
 indi(i)=0;
 indi=logical(indi);
 
 [co, sc, la] = princomp(cxyz(indi,:)) ;
coica=sc(:,1:ni);

me=mean(cxyz(indi,:));
coi=(cxyz(i,:)-me)*co; 

coi=coi(1:ni);

[c1,e1]=classify(coi,coica,g(indi),'linear','empirical');


cll(i,ni)=c1;


errores=0;
grupo=g(indi);
for j=1:15
    
    indi2=ones(15,1);
    
    indi2(j)=0;
   indi2=logical(indi2);
[c1,e1]=classify(coica(j,:),coica(indi2,:),grupo(indi2),'linear','empirical');
errores=errores + double(c1~=grupo(j));

end

erl(i,ni)=errores;


end

 %

  end
  
 
     
      % plot(1:24,sum(er,1)) % 
figure
plot(1:12,sum(erl,1)) % mínimo en  3 

sum(erl,1)

ans =

    87    76    28    35    42    58    58    77    85    81    62    58
