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?

 Réponse acceptée

the cyclist
the cyclist le 7 Mai 2014
Modifié(e) : the cyclist le 7 Mai 2014
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);
figure
plot(x,y, 'black')
% Lo black
xlo = [x(x<=cutoff1) cutoff1];
ylo = [y(x<=cutoff1) 0];
patch(xlo, ylo, 'black')
% Mid red
xmid = [x(x>=cutoff1 & x<=cutoff2) cutoff2 cutoff1];
ymid = [y(x>=cutoff1 & x<=cutoff2) 0 0];
patch(xmid, ymid, 'red')
% High black
xhi = [cutoff2 x(x>=cutoff2)];
yhi = [0 y(x>=cutoff2)];
patch(xhi, yhi, 'black')

Plus de réponses (2)

Star Strider
Star Strider le 7 Mai 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])

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by