Need help plotting confidence intervals
Show older comments
Hi,
I'm trying to plot a 95% confidence interval in matlab but I can't get it the way I want.
What I'm trying to plot is something like this: https://explorable.com/images/statistics-confidence-interval.png
I've found a script which creates the polynomial but fills the area opposite:
alpha = 0.05; % significance level
mu = 82.9; % mean
sigma = 8.698; % std
cutoff1 = norminv(alpha, mu, sigma);
cutoff2 = norminv(1-alpha, mu, sigma);
x = [linspace(mu-4*sigma,cutoff1), ...
linspace(cutoff1,cutoff2), ...
linspace(cutoff2,mu+4*sigma)];
y = normpdf(x, mu, sigma);
plot(x,y, 'black')
xlo = [x(x<=cutoff1) cutoff1];
ylo = [y(x<=cutoff1) 0];
patch(xlo, ylo, 'black')
x = linespace(68,0.01,70;
y = linspace(0,0.01,0.045;
patch(x, y, 'r')
xhi = [cutoff2 x(x>=cutoff2)];
yhi = [0 y(x>=cutoff2)];
patch(xhi, yhi, 'black')
Can you guys help me make it the way I want?
Accepted Answer
More Answers (2)
Star Strider
on 7 May 2014
alpha = 0.05; % significance level
mu = 82.9; % mean
sigma = 8.698; % std
x = linspace(mu-5*sigma, mu+5*sigma, 500);
cutoff1 = norminv(alpha/2, mu, sigma); % Lower 95% CI is p = 0.025
cutoff2 = norminv(1-alpha/2, mu, sigma); % Upper 95% CI is p = 0.975
y = normpdf(x, mu, sigma);
xci = [linspace(mu-5*sigma, cutoff1); linspace(cutoff2, mu+5*sigma)];
yci = normpdf(xci, mu, sigma);
figure(1)
plot(x, y, '-k', 'LineWidth', 1.5)
patch(x, y, [0.5 0.5 0.5])
patch([xci(1,:) cutoff1], [yci(1,:) 0], [1 1 1])
patch([cutoff2 xci(2,:)], [0 yci(2,:)], [1 1 1])
Andreas
on 8 May 2014
0 votes
Categories
Find more on Uniform Distribution (Continuous) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!