need help to plot confidence interval
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi I'm a beginner in matlab.
I had to calibrate a sensor at the university. from the experiment I got [x,y] pairs which I plotted and fitted it with the lsline command. My job is to plot the confidence interval. But unfortunately my code is not working, I would ask for your help in this. What i need is on the linked picture. 

avek=[a1 a2 a3 a4 a5 a6 a7 a75 a65 a55 a45 a35 a25 a15].';
pvek=[p1 p2 p3 p4 p5 p6 p7 p75 p65 p55 p45 p35 p25 p15].';
x=pvek; % datas on x axis
y=avek; %data on y axis
N = 14; % Number of ‘Experiments’ In Data Set
yMean = mean(y) % Mean Of All Experiments At Each Value Of ‘x’
ySEM = std(y)/sqrt(N) % Compute ‘Standard Error Of The Mean’ Of All Experiments At Each Value Of ‘x’
CI95 = tinv((0.95+1)/2, N-2) % Calculate 95% Probability Intervals Of t-Distribution
yCI95 = bsxfun(@times, ySEM, CI95(:)) % Calculate 95% Confidence Intervals Of All Experiments At Each Value Of ‘x’
figure
plot(pvek,avek,'r+');
h=lsline
hold on
plot(x, yMean,'p') % Plot Mean Of All Experiments
hold on
patch([x, fliplr(x)], [yCI95(1,:) fliplr(yCI95(2,:))], 'b', 'EdgeColor','none', 'FaceAlpha',0.25)
0 Kommentare
Antworten (1)
Star Strider
am 16 Nov. 2021
That looks like my code from a very long time ago!
If the Statistics and Machine Learning Toolbox is available, use fitlm and predict for this (the regress function is also an option, so consider it as well) —
x = 0:0.1:9; % Create Data
y = 1.5*x+0.5 + randn(size(x))*0.5; % Create Data
mdl = fitlm(x(:),y(:))
Cefs = mdl.Coefficients.Estimate % Recover Coefficient Values (If Desired)
[ypred,yci] = predict(mdl,x(:), 'Prediction','observation');
figure
plot(x, y, '.g')
hold on
plot(x, ypred, '-r')
plot(x, yci, '--r')
hold off
grid
legend('Data','Linear Fit','95% Confidence Intervals', 'Location','best')
Remember to use the 'Prediction','observation' name-value pair to get the correct confidence intervals.
.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
