From 6122ba27b72a4b9c646c671c80f5da78b2becd72 Mon Sep 17 00:00:00 2001 From: Rob Campbell Date: Sun, 28 Aug 2016 21:03:06 +0200 Subject: [PATCH] replace tInterval_Calc. There is case-sensitive unhappiness happening. --- code/stats/tInterval_calc.m | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 code/stats/tInterval_calc.m diff --git a/code/stats/tInterval_calc.m b/code/stats/tInterval_calc.m new file mode 100644 index 0000000..e827ad6 --- /dev/null +++ b/code/stats/tInterval_calc.m @@ -0,0 +1,52 @@ +function tint=tInterval_Calc(vect, CI) +% tInterval_Calc - confidence interval based on the t-distribution +% +% function tint=tInterval_Calc(vect, CI) +% +% Purpose +% Calculate the t-interval about the mean to a given confidence +% level (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: Calculates the two-tailed 95% t confidence limits for the mean. +% +% - CI [optional]: a p value for a different 2-tailed interval. e.g. 0.01 +% +% Example - plot a 1% interval [rather than the default %5] +% r=randn(1,30); +% T=tInterval_calc(r,0.01); +% hist(r) +% hold on +% plot(mean(r), mean(ylim),'r*') +% plot([mean(r)-T,mean(r)+T], [mean(ylim),mean(ylim)],'r-') +% hold off +% +% Rob Campbell - 12/03/08 +% +% Also see - SEM_calc, tinv + +narginchk(1,2) + +if isvector(vect) + vect=vect(:); +end + + +if nargin==1 + CI = 0.025; %If no second argument, work out a 2-tailed 5% t-interval + stdCI=tinv(1-CI, length(vect)-1); +elseif nargin==2 + CI = CI/2 ; %Convert to 2-tail + stdCI=tinv(1-CI, length(vect)-1); %Based on the t distribution +end + +if stdCI==0 + error('Can''t find confidence iterval for 0 standard deviations!') +end + + +tint = ( (nanstd(vect)) ./ sqrt(sum(~isnan(vect))) ) * stdCI ; +