Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Linear regression in R

  1. Jul 14, 2008 #1
    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.
  2. jcsd
  3. Jul 14, 2008 #2
    So your trying to fit your data to the model

    [tex]y = \alpha + \beta \cdot x [/tex]

    where [itex]\beta[/itex] is a given (non-parameter) and [itex]\alpha[/itex] is a parameter.

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

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

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

    [tex]n \cdot \alpha = \sum y_i - \beta \cdot \sum x_i[/tex]


    [tex]\alpha = \bar y - \beta \cdot \bar x[/tex]

    where the bar values represent averages, eg, [itex]\bar x[/itex] = the averages of the x values, etc.
  4. Jul 14, 2008 #3
    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?
  5. Jul 15, 2008 #4
    Yes, that's an acceptable approach. Note that the solution for "A" is now

    [tex]A = \frac {\sum y_i \cdot x_i^b}{\sum (x_i^b)^2} [/tex]
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook