How to overlap a least-squares line and its scatter plot in a LOG scale ?

2 views (last 30 days)
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 !
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 Comments
Maximilian Arpaio
Maximilian Arpaio on 12 Nov 2018
You are right but it is not that easy since there are many iterations and calls in the code... Any specific part I can maybe further explain ?
dpb
dpb on 12 Nov 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...

Sign in to comment.

Accepted Answer

dpb
dpb on 12 Nov 2018
Edited: dpb on 13 Nov 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
  6 Comments
Maximilian Arpaio
Maximilian Arpaio on 12 Mar 2020
Hi Fernanda, in my dataset Y is the PL value.
In the linear fit, Y is the meanleast square value given by the fit for the above specific dataset.
Regards
Fernanda Suarez Jaimes
Fernanda Suarez Jaimes on 12 Mar 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?

Sign in to comment.

More Answers (0)

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!