Quantcast Linear regression in R Text - Physics Forums Library

PDA

View Full Version : Linear regression in R


Mosis
Jul14-08, 05:28 PM
I'm interested in fitting a line to some data. There is a built-in function in R lm() that gives me both the best-fit slope and intercept, however, I would like to determine the best fit intercept GIVEN a specified value of the slope. Is there an easy way to do this?

I apologize if this is in the wrong forum. I know it's not exactly "programming" but I don't know a more appropriate place to post this.

TheoMcCloskey
Jul14-08, 06:32 PM
So your trying to fit your data to the model

y = \alpha + \beta \cdot x

where \beta is a given (non-parameter) and \alpha is a parameter.

If that's the case, then the Error function (for linear least squares) is

E = \sum^n_i (y_i - (\alpha+\beta \cdot x_i))^2

Since the model only has one free parameter, the solution is rather easy. Take the derivative of E with respect to this parameter \alpha and set it equal to zero. The following results:

n \cdot \alpha = \sum y_i - \beta \cdot \sum x_i

or

\alpha = \bar y - \beta \cdot \bar x

where the bar values represent averages, eg, \bar x = the averages of the x values, etc.

Mosis
Jul14-08, 06:39 PM
Thanks for the reply!

actually, I've tried something different. I'm ultimately interested in fitting some data with a power-law of the form ax^b, where b is the known parameter. One approach is to consider linear regression on the log transform, whereby b will be the known slope and loga will be the unknown parameter. Instead I considered S = sum (y_i - a(x_i)^b)^2. Differentiating wrt a and setting the expression equal to 0 gives me sum((x_i)^b(y_i - A(x_i)^b) = 0, and Maple can easily solve for A given the data.

Is this the correct approach?

TheoMcCloskey
Jul15-08, 12:16 PM
Yes, that's an acceptable approach. Note that the solution for "A" is now

A = \frac {\sum y_i \cdot x_i^b}{\sum (x_i^b)^2}