Is this code for plotting linear regression in loglog scale and confidence intervals correct?
조회 수: 3 (최근 30일)
이전 댓글 표시
Dear MatLab users,
I have a bunch of data x and y, I want to do a linear regression of the natural logarithm of my data. Then, I want to plot the original data, the linear fit, and the linear fit plus the standard deviation (i.e. the condifence intervals). I tried several solutions that I found in other answers, but in the end I had to mix a bit of all of them. I THINK I did it correctly, but I would like your opinion. Is my code correct?![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/464835/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/464835/image.jpeg)
N = 1 ;
x = 100*rand(20,1);
y = 100*rand(20,1);
ln_x = log(x) ;
ln_y = log(y) ;
fit_ln_xy = fitlm( ln_x, ln_y ) ;
p = polyfit( ln_x, ln_y , 1) ;
y_fit = polyval( p , ln_x );
loglog( x , y , '*' )
hold on
loglog( x , exp(y_fit) , 'k' , 'linewidth', 1.5)
loglog( x , exp(y_fit + N*fit_ln_xy.RMSE ) , 'b--' , 'linewidth', 1.2)
loglog( x , exp(y_fit - N*fit_ln_xy.RMSE ) , 'r--' , 'linewidth', 1.2)
legend('Original data ' , 'linear fit' , 'linear fit + 1 std' , 'linear fit - 1 std')
댓글 수: 0
채택된 답변
Gaurav Garg
2020년 12월 29일
Hi Gianluca,
Yes, the code seems to be correct.
Moreover, you can look at the documentation on how to train/test data using linear regression and some other functions you can use here.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!