need help to plot confidence interval

10 visualizaciones (últimos 30 días)
Bence Attila Nagy
Bence Attila Nagy el 16 de Nov. de 2021
Respondida: Star Strider el 16 de Nov. de 2021
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)

Respuestas (1)

Star Strider
Star Strider el 16 de Nov. de 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(:))
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ ________ ______ __________ (Intercept) 0.48306 0.10099 4.7833 6.7949e-06 x1 1.5269 0.019382 78.782 4.165e-84 Number of observations: 91, Error degrees of freedom: 89 Root Mean Squared Error: 0.486 R-squared: 0.986, Adjusted R-Squared: 0.986 F-statistic vs. constant model: 6.21e+03, p-value = 4.16e-84
Cefs = mdl.Coefficients.Estimate % Recover Coefficient Values (If Desired)
Cefs = 2×1
0.4831 1.5269
[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.
.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by