6

I want to fit a linear regression line with a specified slope to a data set. I read this thread about doing the same with an explicit intercept.

0+ suppresses the fitting of the intercept; what is the corresponding trick for the slope? For example, to fit a line with slope 1.5, I tried the following

set.seed(6)
x <- runif(100, -3, 3) 
y <- 2 + x + rnorm(100) 

model1<-lm(y ~ x) 
plot(x,y)
abline(model1,col="red")
abline(coef(model1),1.5,col="dark green")

but second abline function just takes the intercept from model1 and slope 1.5. Whereas I would like the regression line to have slope 1.5, find the best fit to the data points, and then compute intercept from that regression line.

Community
  • 1
  • 1
beeprogrammer
  • 581
  • 1
  • 7
  • 18
  • 4
    Ine way is to use the offset... `model2 <- lm(y ~ 1 + offset(1.5*x)) ; abline(coef(model2)[1], 1.5, col="red")` (where you still cant just pass the model object to abline) – user20650 Oct 23 '15 at 00:53

2 Answers2

11

To find the value of the intercept, you don't actually need a regression. Since Y = a + b * X + ϵ, then E[Y - b * X] = E[a] + E[ϵ], and by assumption E[a] = a and E[ϵ] = 0, where E[] is the expectation operator. Therefore, a = E[Y - b * X].

Translated into R, this means the intercept a is:

b1 <- 1.5
a <- mean(y - b1 * x)

This is inspired by the comments to this question.

cocquemas
  • 1,149
  • 8
  • 17
9

I suppose one approach would be to subtract 1.5*x from y and then fit y using only an intercept term:

mod2 <- lm(I(y-1.5*x)~1)
plot(x, y)
abline(mod2$coefficients, 1.5)

enter image description here

This represents the best linear fit with fixed slope 1.5. Of course, this fit is not very visually appealing because the simulated slope is 1 while the fixed slope is 1.5.

josliber
  • 43,891
  • 12
  • 98
  • 133