Finding Intercept in R with lm() for Specified Slope

In summary, the conversation is about fitting data to a model using a given value for the slope. The suggested approach is to use the Error function and take the derivative to solve for the parameter \alpha. Another approach is to use linear regression on the log transform of the data. Both approaches result in an equation to solve for the unknown parameter.
  • #1
Mosis
55
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
  • #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]

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.
 
  • #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?
 
  • #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]
 

1. What is the "intercept" in linear regression?

The intercept in linear regression is the value of the dependent variable when all independent variables are set to zero. It represents the point where the regression line crosses the y-axis.

2. What is the role of the "lm()" function in finding intercept in R?

The "lm()" function in R is used to fit linear regression models to data. It calculates the intercept and slope of the regression line using the least squares method.

3. How do you specify the slope when using the "lm()" function in R?

The slope can be specified in the "formula" argument of the "lm()" function. For example, to specify a slope of 2, the formula would be "y ~ 2x + intercept".

4. Can the "lm()" function in R be used for non-linear relationships?

No, the "lm()" function is specifically used for fitting linear regression models. For non-linear relationships, other regression methods such as polynomial regression can be used.

5. How can I interpret the intercept value in R's "lm()" output?

The intercept value in the "lm()" output represents the estimated value of the dependent variable when all independent variables are set to zero. It can be interpreted as the baseline or starting point of the regression line.

Similar threads

  • Programming and Computer Science
Replies
11
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
864
  • Set Theory, Logic, Probability, Statistics
Replies
30
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
11
Views
729
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
972
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
468
  • Set Theory, Logic, Probability, Statistics
Replies
23
Views
2K
  • STEM Educators and Teaching
Replies
11
Views
2K
Back
Top