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

2 次查看(过去 30 天)
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 个评论
Maximilian Arpaio
Maximilian Arpaio 2018-11-12
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 2018-11-12
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...

请先登录,再进行评论。

采纳的回答

dpb
dpb 2018-11-12
编辑:dpb 2018-11-13
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 个评论
Maximilian Arpaio
Maximilian Arpaio 2020-3-12
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
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by