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.
댓글 수: 1
Anyone?
채택된 답변
Star Strider
2018년 7월 22일
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?
Chris Angeloni
2020년 7월 20일
편집: Chris Angeloni
2020년 7월 20일
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)
