Finding Intercept in R with lm() for Specified Slope

Click For Summary

Discussion Overview

The discussion revolves around fitting a line to data using the R function lm() while specifying a slope. Participants explore methods for determining the best-fit intercept given a fixed slope, as well as alternative approaches for fitting data to a power-law model.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to fit data using the lm() function in R, specifically wanting to find the intercept for a given slope.
  • Another participant describes the mathematical formulation of the error function for linear least squares and provides a solution for the intercept based on the specified slope.
  • A different approach is proposed involving fitting data to a power-law model, where one participant discusses using logarithmic transformation and differentiation to solve for a parameter.
  • A subsequent reply confirms the alternative approach as acceptable and provides a formula for the parameter in the power-law model.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, with some agreeing on the validity of the power-law fitting method while others focus on the linear regression aspect. No consensus is reached on the best method overall.

Contextual Notes

The discussion includes various mathematical formulations and assumptions related to the fitting processes, but does not resolve the applicability of each method in different contexts.

Mosis
Messages
53
Reaction score
0
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.
 
Technology news on Phys.org
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]

or

[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.
 
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?
 
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]
 

Similar threads

Replies
11
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
4K