Home > SurfStat > redmod.m

redmod

PURPOSE ^

Reduces a linear mixed effects model by removing redundant variables.

SYNOPSIS ^

function mr = redmod( m, meanorvariance );

DESCRIPTION ^

Reduces a linear mixed effects model by removing redundant variables. 

 Usage: mr = redmod( m [,meanorvariance] );

 m = model, either term or random or anything that can be so converted.
 meanorvariance = 'm' to reduce just the mean, 'v' to reduce just the
                  variance, or both if absent. 

 mr = reduced model. Variables with a non-zero coefficient in the null  
      space of the design matrix, and with the largest number of non-zero 
      entries are eliminated first, until the design matrix has full rank.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mr = redmod( m, meanorvariance );
0002 
0003 %Reduces a linear mixed effects model by removing redundant variables.
0004 %
0005 % Usage: mr = redmod( m [,meanorvariance] );
0006 %
0007 % m = model, either term or random or anything that can be so converted.
0008 % meanorvariance = 'm' to reduce just the mean, 'v' to reduce just the
0009 %                  variance, or both if absent.
0010 %
0011 % mr = reduced model. Variables with a non-zero coefficient in the null
0012 %      space of the design matrix, and with the largest number of non-zero
0013 %      entries are eliminated first, until the design matrix has full rank.
0014 
0015 if ~isa(m,'term') && ~isa(m,'random') && numel(m)>1
0016     warning('If you don''t convert vectors to terms you can get unexpected results :-(') 
0017 end
0018 if ~isa(m,'random')
0019     m=random([],m,[],inputname(1));
0020 end
0021 [X,V]=double(m);
0022 [C,D]=char(m);
0023 
0024 fprintf(1,'%s','#variables remaining in model for:');
0025 if nargin<2 | lower(meanorvariance(1))=='m'
0026     p=size(X,2);
0027     fprintf(1,'%s',[' Mean ' num2str(p)]);
0028     nX=null(X,'r')';
0029     while ~isempty(nX)
0030         nn=size(nX,1);
0031         a=repmat(mean(abs(X))./max(abs(X)),nn,1).*(abs(nX)>eps);
0032         [y,i]=max(a');
0033         keep=ones(1,p)>0;
0034         keep(i)=0;
0035         X=X(:,keep);
0036         C=C(keep);
0037         p=size(X,2);
0038         fprintf(1,'%s',[' ' num2str(p)]);
0039         nX=null(X,'r')';
0040     end
0041     fprintf(1,'%s',' Done.');
0042 end
0043 
0044 if nargin<2 | lower(meanorvariance(1))=='v'
0045     q=size(V,2);
0046     fprintf(1,'%s',[' Variance: ' num2str(q)]);
0047     nV=null(V,'r')';
0048     while ~isempty(nV)
0049         nn=size(nV,1);
0050         a=repmat(mean(abs(V))./max(abs(V)),nn,1).*(abs(nV)>eps);
0051         [y,i]=max(a');
0052         keep=ones(1,q)>0;
0053         keep(i)=0;
0054         V=V(:,keep);
0055         D=D(keep);
0056         q=size(V,2);
0057         fprintf(1,'%s',[' ' num2str(q)]);
0058         nV=null(V,'r')';
0059     end
0060     fprintf(1,'%s',' Done.');
0061 end
0062 
0063 if isempty(V)
0064     mr=term(X,C);
0065 else
0066     mr=random(term(V,D),term(X,C),[],[],1);
0067 end
0068 fprintf(1,'\n');
0069 
0070 return
0071 end

Generated on Fri 26-Sep-2008 14:05:29 by m2html © 2003