Graphing Confidence Intervals with upper and lower bounds
조회 수: 13 (최근 30일)
이전 댓글 표시
I want to plot an upper bound and a lower bound confidence interval like this one with a mean line. How can I do this? Right now mine is making about 495 different figures. How would I be able to make this
this is my code below:
close all; clearvars
mx1= 146.4
sx1= sqrt(12.1)
for n = 5:500
n2 = n-1
CI95_Lower = [mx1-(tinv(0.975,n2)*((sx1)/sqrt(n)))]
CI95_Upper = [mx1+(tinv(0.975,n2)*((sx1)/sqrt(n)))]% separate these so that you can graph it on one graph
t95 = (tinv(0.975,n2)*((sx1)/sqrt(n)))
x_1 = [n];
y1 = [CI95_Lower];
y2 = [CI95_Upper];
hold on
figure ()
plot(x_1,y1,'-', 'MarkerFaceColor', 'b') %plots the confidence intervals
line([0,500],[mx1,mx1])
plot(x_1,y1,'-', 'MarkerFaceColor', 'm')
hold off
end
댓글 수: 0
답변 (1개)
Thiago Henrique Gomes Lobato
2019년 12월 8일
You were creating a picture for every loop iteration, and the plot function can't interpolate between points if you give them one at the time. Saving all the intervals in the looping and only them plot solves your problem
close all; clearvars
mx1= 146.4;
sx1= sqrt(12.1);
y1 = zeros(496,1);
y2 = zeros(496,1);
x_1 = zeros(496,1);
for n = 5:500
n2 = n-1;
CI95_Lower = [mx1-(tinv(0.975,n2)*((sx1)/sqrt(n)))];
CI95_Upper = [mx1+(tinv(0.975,n2)*((sx1)/sqrt(n)))];% separate these so that you can graph it on one graph
t95 = (tinv(0.975,n2)*((sx1)/sqrt(n)));
x_1(n-4) = n;
y1(n-4) = CI95_Lower;
y2(n-4) = CI95_Upper;
end
figure ()
hold on
plot(x_1,y1,'-', 'color', 'r') %plots the confidence intervals
line([0,500],[mx1,mx1],'color','y')
plot(x_1,y2,'-', 'color', 'b')
hold off
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!