Linear fit to log x axis scatter and correlation calculation

21 Ansichten (letzte 30 Tage)
Bhowmik.U
Bhowmik.U am 22 Jan. 2020
Kommentiert: dpb am 23 Jan. 2020
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

Antworten (1)

dpb
dpb am 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
untitled.jpg
Rsq from log is fraught with danger; beware.
  2 Kommentare
Bhowmik.U
Bhowmik.U am 23 Jan. 2020
Bearbeitet: dpb am 23 Jan. 2020
I enclose the data.
The issue is I wish to have 'b' column as x axis, change the x axis to log scale and y axis unchanged and then obtain a straight line fit onto this and the correlation coefficient.
scatter(b,a,10,[0.5 0.5 0.5],'filled')
hold on;
set(gca,'LineWidth',2)
set(gca,'FontSize',14,'FontWeight','bold')
set(gca,'XScale','log')
text(5,2000,str,'FontSize',14, 'FontWeight','bold');
box on;
hold on;
p=polyfit(b,a,1); f=polyval(p,b);
plot(b,f,'--')
even if I use log(b) instead of b in last two lines same issue.
dpb
dpb am 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
untitled.jpg
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...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Least Squares finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by