Add trendline and calculate slope of trendline

Hi,
I have a set of X and Y data. I plotted a scatter plot for these 2 sets. I would like to add a trendline for the scatter plot and calculate the slope of this trendline while forcing the intercept to = 0. Basically this can be done in excel, but I would like to add this into my code and automate the process. Is this possible? I intend to use values from the trendline equation for other calculations. I have been looking at commands such as polyfit, but I am not sure this is the correct route
Thanks

Réponses (1)

dpb
dpb le 9 Juil 2014
For a zero-intercept model, simplest is just use the "backslash" function...
m=X\Y; % estimate slope w/ zero intercept
To plot
hold on
plot(X,m*X);
text(xpos,ypos,num2str(m,'y = %.3fx')) % write the trendline equation
xpos, ypos are coordinates to place at your desired location. A relatively low X and corresponding Y would be a good candidate w/ xpos adjusted just a little to left and the 'horizontalposition' property set to 'right'.

5 commentaires

Brian
Brian le 9 Juil 2014
Sorry I forgot to add that I'd like to add the trendline to scatter plot that is nonlinear. I basically want to add a best fit linear trendline to this scatter plot. Then do the slope calculation and forcing 0 of the trendline.
dpb
dpb le 9 Juil 2014
Modifié(e) : dpb le 9 Juil 2014
Not sure what a "a best fit linear trendline" is for a nonlinear data set?
Whatever your definition for that is, the model can be fit via mldivide (aka "*\*") as demonstrated. Whether you include the intercept or not depends on whether you include the column of ones in the design matrix or not.
x=b0 + b1*x1 + b2*x2 + ... + bN*xN;
b=y\X;
where the b_sub_n are coefficients to estimate and the x_sub_n are the various independent variables. They can be terms in a polynomial or any other linear combination. Again, whether you include an intercept is totally up to whether you include it or not in the above design matrix.
ADDENDUM
Or, of course, if you really do mean a linear trend line as is normally thought of, there's
b=polfit(X,Y,1); % first order polynomial
yhat=polyval(b,X); % fitted results
Of course, to simply draw the line on the plot you don't need to evaluate it over but two points at either end of the range desired if it really is the straight trend line. If it's a higher-order poly, then need the additional points to fill in the shape.
The same idea for displaying the fitted equation works w/ just modifying the format string suitably. Again, if you want to fit a zero-intercept model, that's simplest via backslash.
Brian
Brian le 9 Juil 2014
I want to do the second process you mentioned; however, if I use your method to have zero-intercept model, my slope remains unadjusted. I essentially want an equation with a zero-intercept and adjusted slope. For example, I used the polyfit function to receive an equation y=0.096x +6.462. I want that equation to be forced through zero which makes the equation for my new trendline y=0.102x. I believe the method you just stated just causes my original equation y=0.096x+6.462 to become y=0.096x+0. What code can I use to get my equation of y=0.102x?
Thanks
dpb
dpb le 9 Juil 2014
Modifié(e) : dpb le 10 Juil 2014
I don't follow...how did you "adjust" the slope to get the "adjusted" slope?
A least-squares response that looks like your coefficients of [0.096,6.462] if evaluated as least squares for a zero-intercept model the slope is going to be much, much greater 0.096 unless the range of x is very large, indeed.
For example, if I generate a set of data w/
>> x=[0:.1:5].'; % supposed range for x
>> y=polyval([0.096,6.462],x); % the data to fit
Then if I build the zero-intercept model instead I find
>> bz=x\y
bz =
2.0154
a (not surprisingly) much steeper slope to try to minimize SSE given the constraint of zero intercept.
If, otoh, I select
>> x=[1000:1005].';
>> y=polyval([0.096,6.462],x);
as the data set so missing zero by 6 isn't nearly as big a deal, then I find
>> bz=x\y
bz =
0.1024
>>
And amazingly enough, that just by pure happenstance turns out to look a lot like your result. That was purely luck, btw...
Anyways, if that doesn't answer the problem, post a small representative dataset that illustrates what you're up to.
dpb
dpb le 10 Juil 2014
...I believe the method you just stated just causes my original equation y=0.096x+6.462 to become y=0.096x+0.
What on earth makes you think believe that? The flatlanders have a belief as well, but it's kinda' been shown to not be so. "Backslash" solves the LSQ equations for the design as given--if one does/does not include the intercept in the model, the estimation will/will not include it and certainly the result will not be the same in general. NB, however, that the dataset obviously changes how different the result is as illustrated above.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Particle & Nuclear Physics dans Centre d'aide et File Exchange

Question posée :

le 9 Juil 2014

Modifié(e) :

dpb
le 10 Juil 2014

Community Treasure Hunt

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

Start Hunting!

Translated by