Home > SurfStat > SurfStatPlot.m

SurfStatPlot

PURPOSE ^

Numeric or string variables against numeric, adjusted and/or by groups.

SYNOPSIS ^

function [ t, df, pval ] = SurfStatPlot( x, y, M, g, varargin );

DESCRIPTION ^

Numeric or string variables against numeric, adjusted and/or by groups.
 
 Usage: [ t, df, pval ] = SurfStatPlot( x, y [,M [,g [,varargin]]] );
 
 x = n x 1 vector of numbers or cell array of strings or a term of these.
 y = n x 1 vector of numbers.
 M = term or anything that can be converted to a term, for adjusting y, 
     default is M=1.
 g = n x 1 cell array of strings or a term of this. Separate plots are 
     made for each different string (group) in g. Default is g=1.
 varargin = extra arguments for the plot, as for matlab's plot, e.g.
     SurfStatPlot( x, y,  M,  g, 'LineWidth',2, 'MarkerSize',12)
 or  SurfStatPlot( x, y, [], [], 'LineWidth',2, 'MarkerSize',12)
 if M and g are not defined.

   If g=1, y is adjusted for M in the model M+X, where M=1+term(M), 
 X=term(x), and tested against the null model M. 
 If g=/=1, y is adjusted for M in the model M+X+G+X*G, where 
 G=term(g), and tested against the null model M+X+G.  
 t    = T statistic if x is one variable and g=1, otherwise F statistic. 
 df   = degrees of freedom.
 pval = P-value.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ t, df, pval ] = SurfStatPlot( x, y, M, g, varargin );
0002 
0003 %Numeric or string variables against numeric, adjusted and/or by groups.
0004 %
0005 % Usage: [ t, df, pval ] = SurfStatPlot( x, y [,M [,g [,varargin]]] );
0006 %
0007 % x = n x 1 vector of numbers or cell array of strings or a term of these.
0008 % y = n x 1 vector of numbers.
0009 % M = term or anything that can be converted to a term, for adjusting y,
0010 %     default is M=1.
0011 % g = n x 1 cell array of strings or a term of this. Separate plots are
0012 %     made for each different string (group) in g. Default is g=1.
0013 % varargin = extra arguments for the plot, as for matlab's plot, e.g.
0014 %     SurfStatPlot( x, y,  M,  g, 'LineWidth',2, 'MarkerSize',12)
0015 % or  SurfStatPlot( x, y, [], [], 'LineWidth',2, 'MarkerSize',12)
0016 % if M and g are not defined.
0017 %
0018 %   If g=1, y is adjusted for M in the model M+X, where M=1+term(M),
0019 % X=term(x), and tested against the null model M.
0020 % If g=/=1, y is adjusted for M in the model M+X+G+X*G, where
0021 % G=term(g), and tested against the null model M+X+G.
0022 % t    = T statistic if x is one variable and g=1, otherwise F statistic.
0023 % df   = degrees of freedom.
0024 % pval = P-value.
0025 
0026 y=double(y);
0027 
0028 if nargin<3 | isempty(M)
0029     M=1;
0030 else
0031     if isnumeric(M)
0032         M=1+term(M);
0033     end
0034 end
0035 if nargin<4 | isempty(g)
0036     G=1;
0037 else
0038     G=term(g);
0039 end
0040 
0041 X=term(x);
0042 XG=X+G+X*G;
0043 slm=SurfStatLinMod(y,M+XG);
0044 Yhat=slm.X*slm.coef;
0045 XGmat=double(XG);
0046 Yhatadj=XGmat*pinv(XGmat)*Yhat;
0047 c=mean(y)-mean(Yhatadj);
0048 Yhatadj=Yhatadj+c;
0049 Yadj=Yhatadj+y-Yhat;
0050 
0051 nx=size(X,2);
0052 ng=size(G,2);
0053 x=double(X)*((1:nx)');
0054 g=double(G)*((1:ng)');
0055 
0056 if nx==1
0057     if ng==1
0058         iu=[find(x==min(x)); find(x==max(x))];
0059         plot(x(iu),Yhatadj(iu),'-r',x,Yadj,'.b',varargin{:});
0060         slm=SurfStatT(slm,x);
0061         if isfield(slm,'dfs')
0062             slm.df=slm.dfs;
0063         end
0064         pval=stat_threshold(0,1,0,slm.df,[10 abs(slm.t)],[],[],[],[],[],[],0);
0065         pval=pval(2)*2;
0066         xlabel([inputname(1) ...
0067             ': T = ' num2str(round(slm.t*100)/100) ...
0068             ', df = ' num2str(round(slm.df*10)/10) ...
0069             ', P = ' num2str(round(pval*1000)/1000)]);
0070     else 
0071         hs=zeros(1,ng);
0072         for j=1:ng
0073             i=(g==j);
0074             xi=x(i);
0075             Yhatadji=Yhatadj(i);
0076             iu=[find(xi==min(xi)); find(xi==max(xi))];
0077             h=plot(xi(iu),Yhatadji(iu),'-',xi,Yadj(i),'.',varargin{:});
0078             set(h(2),'Color',get(h(1),'Color'));
0079             hs(j)=h(1);
0080             if j==1
0081                 hold all;
0082             end
0083         end
0084         hold off;
0085         legend(hs,char(G));
0086         slm0=SurfStatLinMod(y,M+X+G);
0087         slm=SurfStatF(slm,slm0);
0088         pval=stat_threshold(0,1,0,slm.df,[10 slm.t],[],[],[],[],[],[],0);
0089         pval=pval(2);
0090         xlabel([inputname(1) ' * ' inputname(4) ...
0091             ': F = ' num2str(round(slm.t*100)/100) ...
0092             ', df = ' num2str(slm.df(1)) ',' num2str(slm.df(2)) ...
0093             ', P = ' num2str(round(pval*1000)/1000)]);
0094     end 
0095 else
0096     if ng==1
0097         [xu,iu]=unique(x);
0098         plot(xu,Yhatadj(iu),'-r',x,Yadj,'.b',varargin{:});
0099         slm0=SurfStatLinMod(y,M);
0100         str='';
0101     else
0102         hs=zeros(1,ng);
0103         for j=1:ng
0104             i=(g==j);
0105             xi=x(i);
0106             Yhatadji=Yhatadj(i);
0107             [xu,iu]=unique(xi);
0108             h=plot(xu,Yhatadji(iu),'-',xi,Yadj(i),'.',varargin{:});
0109             set(h(2),'Color',get(h(1),'Color'));
0110             hs(j)=h(1);
0111             if j==1
0112                 hold all;
0113             end
0114         end
0115         hold off;
0116         legend(hs,char(G));
0117         slm0=SurfStatLinMod(y,M+X+G);
0118         str=[' * ' inputname(4)];
0119     end
0120     xlim([0.5 nx+0.5]);
0121     set(gca,'XTick',(1:nx)');
0122     set(gca,'XTickLabel',strvcat(char(X)));
0123     slm=SurfStatF(slm,slm0);
0124     pval=stat_threshold(0,1,0,slm.df,[10 slm.t],[],[],[],[],[],[],0);
0125     pval=pval(2);
0126     xlabel([inputname(1) str ...
0127         ': F = ' num2str(round(slm.t*100)/100) ...
0128       ', df = ' num2str(slm.df(1)) ',' num2str(slm.df(2)) ...
0129       ', P = ' num2str(round(pval*1000)/1000)]);    
0130 end    
0131 if nargin<3 | prod(size(M))==1
0132     ylabel(inputname(2));
0133 else
0134     ylabel([inputname(2) ' adjusted for ' inputname(3)]);
0135 end
0136 t=slm.t;
0137 df=slm.df;
0138 set(gcf,'PaperPosition',[0.25 2.5 6 4.5]);
0139 
0140 return
0141 end

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