Reputation: 1312
I've been having trouble getting MATLAB to divulge the slope and intercept of a least-squares regression line, based on a 2-D scatterplot. This seems like it should be easier than it's turning out to be, but all the existing tools MATLAB provides for regression tend to assume that I'm doing something more complicated than I want to do. I should be able to get it from a facility like lsline
, but the IDE is playing hard-to-get with the source code. Does anyone know a quick and dirty way to do this?
Upvotes: 1
Views: 4522
Reputation: 77454
Are you only trying to estimate the regression visually, from the scatterplot? If not, you can use the polyfit()
function to get your estimates. Or even better, simply write your own function. If you make a column of 1's, and then place your independent axis variables into adjacent columns, and call that matrix X, and you store your dependent variable in a column vector called Y, then just compute beta = (X'*X)\(X'*Y)
. The first entry of beta
gives the intercept, and the subsequent entries give the coefficients of your regression variables.
Upvotes: 1