Linear fit to log x axis scatter and correlation calculation
4 views (last 30 days)
Show older comments
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 Comments
Answers (1)
dpb
on 22 Jan 2020
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
Rsq from log is fraught with danger; beware.
2 Comments
dpb
on 23 Jan 2020
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
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...
See Also
Categories
Find more on Least Squares 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!