[英]Linear regression with specified slope
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; 0+
抑制了截距的拟合; what is the corresponding trick for the slope? 斜坡的相应技巧是什么? For example, to fit a line with slope 1.5, I tried the following
例如,为了拟合斜率为1.5的线,我尝试了以下方法
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. 但是第二个abline函数只接受了model1和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.
虽然我希望回归线的斜率为1.5,找到最适合数据点,然后从该回归线计算截距。
I suppose one approach would be to subtract 1.5*x
from y
and then fit y
using only an intercept term: 我想一种方法是从
y
减去1.5*x
,然后仅使用截距项拟合y
:
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. 这表示具有固定斜率1.5的最佳线性拟合。 Of course, this fit is not very visually appealing because the simulated slope is 1 while the fixed slope is 1.5.
当然,这种拟合在视觉上并不十分吸引人,因为模拟斜率为1而固定斜率为1.5。
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. 由于
Y = a + b * X + ϵ
,则E[Y - b * X] = E[a] + E[ϵ]
,并且假设E[a] = a
且E[ϵ] = 0
,其中E[]
是期望运营商。 Therefore, a = E[Y - b * X]
. 因此,
a = E[Y - b * X]
。
Translated into R, this means the intercept a
is: 翻译成R,这意味着拦截
a
是:
b1 <- 1.5
a <- mean(y - b1 * x)
This is inspired by the comments to this question . 这是受到这个问题的评论的启发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.