107 views (last 30 days)

Adam Danz
on 15 Nov 2019

Edited: Adam Danz
on 15 Nov 2019

Here's an anonymous function that computes the 95% CI based on the tinv method which requires that your data approximately form a normal distirbution. See this link for more information on this function.

% x is a vector, matrix, or any numeric array of data. NaNs are ignored.

% p is a the confident level (ie, 95 for 95% CI)

% The output is 1x2 vector showing the [lower,upper] interval values.

CIFcn = @(x,p)std(x(:),'omitnan')/sqrt(sum(~isnan(x(:)))) * tinv(abs([0,1]-(1-p/100)/2),sum(~isnan(x(:)))-1) + mean(x(:),'omitnan');

% Demo

% x = randn(100,1) + 5;

% p = 95;

% CI = CIFcn(x,p)

Here's a demo using your code

EE = [0.0363 0.0312 0.0274 0.0244 0.0220 0.0200 0.0183 0.0168 0.0155 0.0143];

CIFcn = @(x,p)std(x(:),'omitnan')/sqrt(sum(~isnan(x(:)))) * tinv(abs([0,1]-(1-p/100)/2),sum(~isnan(x(:)))-1) + mean(x(:),'omitnan');

CI = CIFcn(EE,96);

% Compute the distance of the upper and lower bounds

CIdist = abs(CI-mean(EE));

% plot

plot(1, mean(EE), 'bo')

hold on

errorbar(1, mean(EE), CIdist(1), CIdist(2))

Adam Danz
on 15 Nov 2019

In my answer, EE is a vector of values. The center point is the mean of EE which you can clearly see in my code.

Confidence intervals generally show the range of possible mean values from a distribution. In my answer, I'm only dealing with 1 vector, 1 distribution, 1 mean value, 1 confidence interval.

Now you can apply that to your data but first, I recommend taking a few minutes to understand what's going on in my answer.

In your data, EE is probably the means from several distribtions. You'll want to provide the raw data in the CIFcn function in order to compute the CI.

Adam Danz
on 15 Nov 2019

I couldn't possibly answer that without knowing what inputs you're providing.

I have no idea what your data look like. Are you provding the CIFcn() function a matrix? a vector? If you're providing a matrix and you'd like to compute the CIs for each column, you'll need to provide each column as input individually or rewrite the function.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767526

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767526

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767529

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767529

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767539

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767539

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767541

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767541

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767543

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767543

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767544

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/491212-error-bar-with-ci-95-on-bar-graph#comment_767544

Sign in to comment.