Using polyfit for linear regression

12 views (last 30 days)
ggeneral
ggeneral on 12 Apr 2015
Edited: John D'Errico on 12 Apr 2015
I was trying to solve a matlab problem below (problem 3) using the built-in function polyfit. i have my code but but something is not right please goo through this;
x=1:10;
y=[0.7165 0.6703 0.6514 0.6412 0.6347 0.6303 0.6271 0.6246 0.6227 0.6211]
Y=log(x.*y)
p=polyfit(x,y,1)
please also how do i find alpha and beta
  2 Comments
Star Strider
Star Strider on 12 Apr 2015
That function cannot be linearised. It is ‘nonlinear in the parameters’, meaning that the partial derivative of the function with respect to any parameter is a function of that parameter or another parameter. You need to use a nonlinear regression routine to get an accurate estimate of alpha and beta.
John D'Errico
John D'Errico on 12 Apr 2015
Edited: John D'Errico on 12 Apr 2015
In this case, "linearization" should be taken to mean a transformation and/or an algebraic manipulation that allows the parameters to be estimated linearly, essentially with polyfit.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 12 Apr 2015
Edited: John D'Errico on 12 Apr 2015
Its just algebra. (I have no idea why you formed log(x*y) by the way.)
The model is
y = exp(-x/(alpha + beta*x))
You logged the model (which can cause problems there in terms of the error structure.) It results in
log(y) = -x/(alpha + beta*x)
or...
alpha + beta*x = -x/log(y)
Use polyfit to estimate the linear model for alpha and beta, where the independent variable will be x, and the dependent variable -x/log(y).
params = polyfit(x,-x./log(y),1);
alpha = params(2);
beta = params(1);
As I said before, logging and transforming the model now has polyfit making the implicit assumption that the error is additive with x/log(y). Unless that is ok, you MIGHT have an issue.
Typically when I use a transformation of this sort, I must decide if the error appears to be additive normal error in that transformed space. So I might plot the residuals. Do they appear to be similar in magnitude across the span of x? If not, then these coefficients for alpha and beta might be less well estimated than I wish. It might be more appropriate to use a weighted regression. Or I might use these as starting values for a nonlinear regression estimator.
The point is, suppose the true noise structure in this problem was:
y = exp(-x/(alpha + beta*x)) + E
where E is additive noise. See that when you transform the model, then there are issues, because log(u+e) is nothing simple.
The use of polyfit makes the implicit assumption that
-x/log(y) = alpha + beta*x + E
so E is additive, Normally distributed noise.

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!