Wrong area filled with fitlm confidence interval

Hello,
I have extracted the confidence interval from some data interpolated with the linear interpolation function in Matlab through the following commands:
mdl = fitlm(x_data, y_data);
Unrecognized function or variable 'x_data'.
x_data and y_data are column vectors with shape 43x1.
From which I have extracted the confidence interval:
[yhat1,ci1] = predict(mdl,x_data)
I want then to fill the area within the confidence interval and I have tried the following commands:
xconf = [x_data x_data(end:-1:1)];
yconf = [ci1(:,2) ci1(end:-1:1,1)];
yconf2 = [ci1(:,2) ci1(:,1)]
%First temptative
fill(xconf, yconf, 'red')
%Second temptative
fill(xconf, yconf2, 'red')
In both cases, the area which is filled is wrong (what is displayed is the confidence interval only):
I would like to ask some suggestions how to solve this problem having the confidence interval extracted with the linear interpolation function.
Thank you!

 Accepted Answer

Use the patch function for best results —
x_data = linspace(-7, 5, 25).';
y_data = randn(size(x_data));
mdl = fitlm(x_data, y_data);
[yhat1,ci1] = predict(mdl,x_data);
figure
hold on
% plot(x_data, y_data)
plot(x_data, ci1, 'r')
patch([x_data; flip(x_data)], [ci1(:,1); flip(ci1(:,2))], '-r')
hold off
.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!