Edit the limits in least squares line.

16 visualizaciones (últimos 30 días)
Dimitrios Bentis
Dimitrios Bentis el 31 de Mzo. de 2018
Editada: Adam Danz el 30 de Abr. de 2021
Hi everyone!
Plotting the lsline in a scatter plot I get the least squares line but it is stretched out of the period with the data as can be seen below. Is there anyway I can have it only above the period of interest (the period that I have data, 1976-2016)?
Thanks in advance!
  4 comentarios
dpb
dpb el 31 de Mzo. de 2018
Well, unfortunately, lsline doesn't help much for anything other than the default case; there's no way to find out what the coefficients are so nothing you can do with it.
That kind of thing is why asked about how you'd created the line...
Adam Danz
Adam Danz el 30 de Abr. de 2021
Editada: Adam Danz el 30 de Abr. de 2021
Using polyfit or fitlm is the way to go but because I'm drawn to finding alternatives, here's a workaround that only relies on lsline.
Before calling lsline, set axis tight or set xlim to the range of your data, then call lsline, then set your desired axis limits.
x = linspace(1975,2017,50);
y = rand(size(x))*50+130;
plot(x, y, 'o')
axis tight
lsline
xlim([1970,2020])
ylim([0,350])

Iniciar sesión para comentar.

Respuestas (2)

dpb
dpb el 31 de Mzo. de 2018
Editada: dpb el 31 de Mzo. de 2018
If you need to do more than what lsline does which is to draw the line from the axis limits but not give anything else useful for modifying it; then you have to fit the line itself. I really don't understand TMW's thinking on a bunch of this stuff... :(
You've got a time axis; none of the fitting routines I tried here with R2016b are datetime aware so will illustrate with datenum; same idea if you have a more recent release and does work with datetime
>> dn=datenum(1975,1:3:128,1).'; % a stretch of datenums
>> w=50*rand(size(dn))+140; % some data to go with it
>> plot(dn,w,'0')
>> ylim([0 350])
>> datetick('x','keeplimits')
>> lsline % put the default lsline on for comparison later...
>> b=fitlm(dn,w,'linear') % do a linear fit...
b =
Linear regression model:
y ~ 1 + x1
Estimated Coefficients:
Estimate SE tStat pValue
_________ _________ ________ _______
(Intercept) -1333.7 1376.8 -0.96873 0.33836
x1 0.0020712 0.0019036 1.0881 0.28291
Number of observations: 43, Error degrees of freedom: 41
Root Mean Squared Error: 14.1
R-squared: 0.0281, Adjusted R-Squared 0.00436
F-statistic vs. constant model: 1.18, p-value = 0.283
>> hold on % to add to the plot
>> hLS=plot([dn(1);dn(end)],b.predict([dn(1);dn(end)]),'r-'); % add the LS fit over data range
This gives:
so you can see is the same line just limited to the range of the data.

Scott MacKenzie
Scott MacKenzie el 17 de Abr. de 2021
As noted, lsline isn't very useful beyond its default behaviour. To constrain the limits of the regression line, a simple option is to use polyfit and line, as below.
x = [5 7 3 8 6 9];
y = [4 5 3 6 5 7];
scatter(x,y);
axis([0 10 0 10]);
p = polyfit(x, y, 1);
m = p(1); % slope
b = p(2); % intercept
line([min(x) max(x)], [m*min(x)+b m*max(x)+b], 'color', 'r');
This yields

Community Treasure Hunt

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

Start Hunting!

Translated by