Plotting curved confidence intervals for linear regression
显示 更早的评论
I am using coefCI function to estimate the confidence intervals of a linear model. I can plot this and get linear confidence bands for the regression line, but, I've seen plots of linear regression where the bands are curved. How can I get those? Thanks.
采纳的回答
I did not see your post before.
Use the appropriate version of the predict (link) function for a compact linear model, predict (link) for a generalised linear model, or others, depending on what you are doing.
Those will give the confidence intervals. If you want to plot smooth curves, use:
xv = linspace(min(x), max(x), 1000);
for the independent variable ‘Xnew’ to use with predict.
8 个评论
Excellent. Not sure how I missed the predict function. Thanks!
Well, the regression line does not need interpolation. For the confidence intervals, using linspace will give me straight lines rather than curved, which is what I want. I would rather use a cubic interpolation.
yci = spline(x, yci, x(1):0.01:x(end));
My pleasure.
The confidence intervals do not need interpolation. Plot them as returned by predict.
If the confidence limits lines appear straight, that means there is not much variation in the confidence limits over the interval. This occurs if there is very little error in the dependent variable, especially with respect to its magnitude. (I opted for the 1000 points because I do not know your data set. That number should give a smooth approximation in any event. Change it as necessary.)
Without your data and relevant code, I cannot suggest any other reason for the confidence limit lines appearing not to vary over the limits of your independent variable. They should have a minimum difference at the mean of your independent variable, and a maximum (and approximately equivalent) difference at the extremes of your independent variable. Use that as a test to see if they are actually straight. (I suspect they are not.)
Hi Star, I think you misunderstood my comment. The confidence intervals are and indeed appear curved. But using linspace as you suggest will make them straight and therefore wrong. Whereas, using spline interpolation will make them smooth and preserve their curved shape. Maybe I misunderstood your suggestion or linspace in my MATLAB works differently!
Thanks anyways!
My pleasure.
They aren’t straight when I plot them, using essentially the same code as in my original Answer:
x = sort(rand(1,100));
y = rand(1,100);
mdl = fitlm(x,y);
Xnew = linspace(min(x), max(x), 1000)';
[ypred,yci] = predict(mdl, Xnew);
figure
plot(x, y, 'p')
hold on
plot(Xnew, ypred, '--g')
plot(Xnew, yci, '--r')
hold off
grid
Note that ‘Xnew’ must be a column vector.
Aaah Ok, I see. Use linspace before predict... got it! I thought you meant to use linspace with the predicted min and max ypred values, which didn't make much sense. The two approaches give me the same smooth confidence intervals, but yours is definitely more appropriate.
Thank you.
If my Answer helped you solve your problem, please Accept it!
This answer appears to work great for a model with a single predictor.
However, when trying to use it with a model in which I have multiple predictors (e.g. 5), I get the following error:
Error using classreg.regr.CompactTermsRegression/designMatrix (line 166)
X must have 5 columns.
Error in LinearModel/predict (line 337)
design = designMatrix(model,Xpred);
Any idea as to how to plot confidence intervals from a fitlm structure based on a multiple linear regression model?
The short answer is that you get this error because you have multiple predictors, so the values in Xnew (or xv in the example above) should have a column for each predictor. (ie. you need to make an xv for every predictor in your multiple regression).
This makes visualizing the data tricky, because for every additional predictor, you add another dimension to your plot, I believe. The question here I think was focused mostly on linear regression with one predictor.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Predictive Coding 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
