![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/265499/untitled.jpeg)
Linear fit to log x axis scatter and correlation calculation
조회 수: 4 (최근 30일)
이전 댓글 표시
I have two data streams 618*1 doubles; say 'a' and 'b'
I want to plot scatter of 'a' versus 'b', 'b' in x axis in log scale
I also need a best fit line and a correlation coefficient
My code runs
scatter (a,b,10,[0.5,0.5,0.5],'filled')
set(gca,'XScale','log')
hold on;
set(gca,'Linewidth,2, FontSize',14,'FontWeight','bold');
box on;
p=polyfit(a,b,1); f=polyval(p,a);
plot(a,f,'--')
I am getting multiple fitting lines..one straight..rest curved...in same plot...I am looking for a single line. Also I require to find correlation coefficient.
PS: I even tried to replace a by log10(a) in the following lines as
p=polyfit(log10(a),b,1); f=polyval(p,log10(a));
plot(log10(a),f,'--')
NOw I am getting the multiple fitting lines away from my scatter...kindly help...I also shall require the correlation value
댓글 수: 0
답변 (1개)
dpb
2020년 1월 22일
You forgot to attach the data so made something up...
x=1:10;
y=sort(lognrnd(10,1,1,10));
b=polyfit(x,log10(y),1);
yf=10.^polyval(b,[x(1) x(end)])
figure
scatter(1:10,y)
hLF=plot([x(1) x(end)],yf,'--');
xlim([0.5 10.5])
hAx=gca;
hAx.YScale='log';
gives
![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/265499/untitled.jpeg)
Rsq from log is fraught with danger; beware.
댓글 수: 2
dpb
2020년 1월 23일
Just reverse x,y above, excepting since it is the independent variable in log scale not the dependent (far less common); hence my misread earlier). Using the same data as previous x,y that happen to still be in my workspace:
figure
scatter(y,x)
hAx=gca;
hAx.XScale='log';
xlim([0.035 1]*1E5)
b=polyfit(y,log10(xx),1)
yf=polyval(b,log10([y(1) y(end)]))
hLF=plot([y(1) y(end)],yf,'--');
results in
![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/267507/untitled.jpeg)
Rsq is just
Rsq = 1 - SSres/SStot
SSres => Sum of Squares Residuals
SStot => Total Sum of Squares
Again I'll caution against Rsq on logged variables; the value is generally heavily overestimated altho on semilogx with y in linear scale, not so much as other-way-round.
If have Statistics TB, the curve fitting routines in it have the calculation returned...
참고 항목
카테고리
Help Center 및 File Exchange에서 Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!