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.
A linear regression line has an equation of the form Y = a + bX, where X is the explanatory variable and Y is the dependent variable. The slope of the line is b, and a is the intercept (the value of y when x = 0).
The regression slope intercept is used in linear regression. The regression slope intercept formula, b0 = y – b1 * x is really just an algebraic variation of the regression equation, y' = b0 + b1x where “b0” is the y-intercept and b1x is the slope.
The slope of the line is a function of the correlation between the two variables, multiplied by the ratio of their standard deviations. ➢ Sy is the standard deviation of the y variable, or the response variable.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With