Description
The following MATLAB codes will remove the effect of age in two sample group comparison. It will construct F statistic at a given voxel. I did not write it as a single function since people may need to modify the codes to suit their applications.
MATLAB Codes (MATLAB 6.1. implementation)
% n.sample1 = sample size for sample 1
% n.sample2 = sample size for sample 2
% age.sample1 = age for sample 1; column vector
% age.sample2 = age for sample 2; column vector
% obs1 = observation for sample 1
% obs2 = observation for sample 2
% group = 0 if sample 1 or 1 if sample 2
%total sample size
n = n.sample1 + n.sample2;
% combined age data. this should give a single column vector.
age=[age.sample1; age.sample2];
% combined observations. this should give a single column vector.
obs = [obs1; obs2];
% fitting null model of no group difference
% H_0: group = lambda1 + lambda2 * age
const=ones(n,1)
%design matrix
X=[const,age]
%estimate lambda's
lambda=zeros(2,1);
lambda=pinv(X)*obs;
%sum of squared errors of null model. denominator for F stat
SSE0 = sum((obs - (X*lambda)).^2);
% fitting an alternate model of group difference
% H_0: group = lambda1 + lambda2 * age + \lambda3 * group
% group = binary indexing for groups
% 0 if sample 1 or 1 if sample 2
group = [zeros(n.sample1,1);ones(n.sample2,1)];
lambda=zeros(3,1);
X=[const,age,group]
lambda=pinv(X)*obs;
% sum of squared errors of alternate model. numerator for F stat
SSE1 = sum((obs - (X*lambda)).^2);
% F statistic = equvalent to ANOVA F statistic output
F=(SSE0-SSE1)/(SSE0/(n-1-2))
Documentation
The following papers have used the above MATLAB code and contains some explanation.
Last update 02/25/2005.