[422372]: / functions / miscfunc / means.m

Download this file

69 lines (60 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function [M,stderr,V,grpids] = means(X,grps)
% MEANS: Means, standard errors and variances. For column vectors, means(x)
% returns the mean value. For matrices or row vectors, means(x) is a
% row vector containing the mean value of each column. The basic
% difference from the Matlab functions MEAN and VAR is for a row vector,
% where MEANS returns the row vector instead of the mean value of the
% elements of the row. Also allows for missing data, passed as NaNs.
%
% If an optional grouping vector is supplied, returns a vector of means
% for each group in collating sequence.
%
% Usage: [M,stderr,V,grpids] = means(X,{grps})
%
% X = [n x p] data matrix.
% grps = optional [n x 1] grouping vector for k groups.
% -----------------------------------------------------------------------
% M = [k x p] matrix of group means.
% stderr = corresponding [k x 1] vector of standard errors of the means.
% V = corresponding vector of variances.
% grpids = corresponding vector of group identifiers, for multiple
% groups.
%
% RE Strauss, 2/2/99
% 12/26/99 - added standard errors.
% 10/19/00 - sort group-identifiers into collating sequence.
% 12/21/00 - corrected problem with uninitialized 'fg' for single group.
% 2/24/02 - added estimation of variances.
% 10/15/02 - corrected documentation.
if nargin == 0, help means; return; end
if (nargin < 2) grps = []; end
[n,p] = size(X);
if (isempty(grps))
grps = ones(n,1);
ug = 1;
fg = n;
k = 1;
else
[ug,fg] = uniquef(grps,1);
k = length(ug);
end
grpids = ug;
M = zeros(k,p);
stderr = zeros(k,p);
for ik = 1:k
ir = find(grps==ug(ik));
for c = 1:p
x = X(ir,c);
ic = find(isfinite(x));
if (isempty(ic))
M(ik,c) = NaN;
stderr(ik,c) = NaN;
else
M(ik,c) = mean(x(ic));
s = std(x(ic));
stderr(ik,c) = s./sqrt(fg(ik));
V(ik,c) = s.^2;
end
end
end
return;