-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSEM_calc.m
52 lines (47 loc) · 1.3 KB
/
SEM_calc.m
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
function sem=SEM_calc(vect, CI)
% SEM_calc - standard error of the mean, confidence interval
%
% function sem=SEM_calc(vect, CI)
%
% Purpose
% Calculate the standard error the mean to a given confidence
% interval (CI). Note that nans do not contribute to the
% calculation of the sample size and are ignored for the SD
% calculation. Output of this function has been checked against
% known working code written in R.
%
% Inputs
% - vect: A vector upon which the SEM will be calculated. Note that
% if vect is a matrix then we calculate one SEM for each
% column.
%
% - CI [optional]: a p value for a different 2-tailed interval. e.g. 0.01
% This is a 2-tailed interval.
%
% Outputs
% sem - the standard error of the mean. So to plot the interval it's mu-sem
% to mu+sem.
%
% Example - plot a 1% interval [rather than the default %5]
% r=randn(1,30);
% S=SEM_calc(r,0.01);
% hist(r)
% hold on
% plot(mean(r), mean(ylim),'r*')
% plot([mean(r)-S,mean(r)+S], [mean(ylim),mean(ylim)],'r-')
% hold off
%
% Rob Campbell
%
% Also see - tInterval_Calc, norminv
error(nargchk(1,2,nargin))
if isvector(vect)
vect=vect(:);
end
if nargin==1
stdCI = 1.96 ;
elseif nargin==2
CI = CI/2 ; %Convert to 2-tail
stdCI = abs(norminv(CI,0,1)) ;
end
sem = ( (nanstd(vect)) ./ sqrt(sum(~isnan(vect))) ) * stdCI ;