0001 function [sumY, varY] = pr_sum_func(y, sumfunc, wt)
0002
0003
0004
0005
0006
0007 if nargin < 2
0008 error('Need data matrix and summary function');
0009 end
0010 [m n] = size(y);
0011 if any([m n] == 0)
0012 error('Data vector is empty');
0013 end
0014 if nargin < 3
0015 wt = [];
0016 end
0017 if isempty(wt)
0018 wt = ones(n,1);
0019 end
0020 if size(wt,1)==1
0021 wt = wt';
0022 end
0023 if n == 1
0024 sumY = y; varY = y*Inf;
0025 return
0026 end
0027
0028 varY = ones(m,1) * Inf;
0029 switch sumfunc
0030 case 'mean'
0031 sumY = mean(y, 2);
0032 ssy = sum(y.^2, 2);
0033 varY = (ssy - n*sumY.^2)/(n-1);
0034 case 'wtmean'
0035
0036
0037
0038
0039 swt = sum(wt);
0040 sumY = y*wt/swt;
0041 varY = (swt/(swt.^2 - wt'*wt)) * ((y - (sumY * ones(1,n))).^2 * wt);
0042
0043
0044
0045
0046 case 'median'
0047 sumY = median(y, 2);
0048 case 'eigen1'
0049
0050
0051
0052
0053
0054 if m > n
0055 [v s v] = svd(spm_atranspa(y));
0056 s = diag(s);
0057 v = v(:,1);
0058 u = y*v/sqrt(s(1));
0059 else
0060 [u s u] = svd(spm_atranspa(y'));
0061 s = diag(s);
0062 u = u(:,1);
0063 v = y'*u/sqrt(s(1));
0064 end
0065 d = sign(sum(v));
0066 u = u*d;
0067 v = v*d;
0068 sumY = u*sqrt(s(1)/n);
0069
0070
0071
0072 otherwise
0073 error(['Do not recognize summary function ' sumfunc]);
0074 end