How to overlap a least-squares line and its scatter plot in a LOG scale ?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Maximilian Arpaio
el 12 de Nov. de 2018
Comentada: Fernanda Suarez Jaimes
el 12 de Mzo. de 2020
Dear community,
I have some path loss data (i.e attenuation [dbm] vs linear distance [m]) coming from a simulation tool. Once plotted in the semiLOG scale(dBm vs LOG of distance) the data trend is linear. I can do this either by mathematically converting the distance vector to LOG values or by using the 'semilogx' function within Matlab.
I have then used the "fit" (with "poly1") function to calculate the parameters of the line (i.e slope and intercept) by feeding the same function with the converted LOG values of the distance. In this manner I retrieved the equation of the line in the log scale.
Now I am not able to plot both the scatter data and its linear regression at the same time in a LOG scale....
The challenge I am facing is that if the distance values are converted into LOGs, then my X axis is shown as 2.1, 2.3, ecc which does not make any sense but the data and the line are both ok. On the contrary, if the distance values are kept linear and I use the semilogx function then the regression line looks orrible. See attached pics in the two cases.
I know there is something I am missing between the LOG and the linear scale but I am running out of ideas... What drives me crazy most is that I have also "backconverted" the linear regression line in an exponential so as to plot it in a linear scale (using the slope and intercept values retrieved in the LOG scale) together with my linear data but once converted everything in the log scale, data is ok but the linear line...is not linear anymore...
Any comment is welcome !
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194660/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194663/image.jpeg)
EDIT: long story short, I would like to plot my data on 0, 10^1, 10^2 axis form which is tipical of LOG scales AND having the regression line like the one in red and NOT like the one in yellow...
3 comentarios
dpb
el 12 de Nov. de 2018
Yes, show the pertinent pieces of the code as you used it so we can try to tell where you went wrong...otherwise, it's mostly just guessing.
You apparently have created a plot, you can retrieve the X|YData from it into an array and SAVE it to a .mat file (use 1:N subscript for reasonable-sized N so it's representative but not hugmongous) then attach the .mat file and somebody can have something meaningful to your specific problem to work with without trying to make something up. Make it easy for us to help you...
Respuesta aceptada
dpb
el 12 de Nov. de 2018
Editada: dpb
el 13 de Nov. de 2018
OK, while it would be more fun with real data and to see what, specifically, you had done to correct where there was the misconception/error, made up some data...
mu=-0.55; sigma=1.55; % arbitrary to get roughly same range
X=lognrnd(mu,sigma,1,1e3); % generate a bunch of X's on log scale
Y=Y+rand(size(Y))*10; % a noisy Y correlated w/ X; how, precisely, immaterial
scatter(X,Y,'.') % plot the data points
hAx=gca; hAx.XScale='log'; % on semilog x axis
Now to the guts of the problem...fit semilogx linear to the data and add regression line...
b=polyfit(log(X),Y,1); % fit response to log(independent)
Now to add regression line to plot...
yHat=polyval(b,log([min(X) max(X)])) % remember, it's log in X
yHat =
68.5393 216.9604
And add to the plot...
hold on
plot([min(X) max(X)],yHat,'r-') % no log here, axis scale is internal
resulted in the following figure
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194751/image.jpeg)
6 comentarios
Fernanda Suarez Jaimes
el 12 de Mzo. de 2020
Thank you!
So would you say this would be the right way to diferentiate them?
time_series2 = lognrnd(0,0.25,6000,1); %generating time series with mu set to zero and sigma 0.25
PL=PL+rand(size(PL))*10; % a noisy Y correlated w/ X; how, precisely, immaterial. Y is the PL value
scatter(time_series2,PL,'.') % plot the data points
hAx=gca; hAx.XScale='log'; % on semilog x axis
b=polyfit(log(time_series2),MeanLSQ,1); % fit response to log(independent)
yHat=polyval(b,log([min(time_series2) max(time_series2)])) % remember, it's log in X
hold on
plot([min(time_series2) max(time_series2)],yHat,'r-')
Is this code giving me the regression?
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!