[英]Constrained least-square regression - Matlab or R
I'm doing a least-square regression on some data, the function has the form 我正在对一些数据进行最小二乘回归,函数具有形式
y ~ a + b*x
and I want the regression line to pass through a specific point P(x,y) (which is not the origin). 我希望回归线通过一个特定的点P(x,y)(它不是原点)。 How can I do that? 我怎样才能做到这一点?
I'm using the lm command in R and the basic fitting GUI in Matlab. 我在R中使用lm命令,在Matlab中使用基本拟合GUI。 I think that I could use the constrOptim command (in R) or translate the origin into the point P, but I'm wondering if there's a specific command to do that. 我认为我可以使用constrOptim命令(在R中)或将原点转换为P点,但我想知道是否有特定的命令来执行此操作。
I only need the solution for one of these programs, then I can use the coefficients in the other one. 我只需要其中一个程序的解决方案,然后我就可以使用另一个程序中的系数。
Just center the data appropriately and force the regression through the 'origin': 只需适当地居中数据并通过“起源”强制回归:
lm(y ~ I(x-x0)-1, offset=rep(y0,nrow(dat)) data=dat)
You might then need to adjust the intercept coefficient accordingly. 然后,您可能需要相应地调整截距系数。
edited : offset
needs to be a vector of the correct length. 编辑 : offset
需要是正确长度的向量。 Another way to do this would be: 另一种方法是:
set.seed(1)
d <- data.frame(x=1:10,y=rnorm(10,mean=1:10,sd=0.1))
x0 <- 3
y0 <- 3
(lm1 <- lm(y ~ I(x-x0)-1, offset=y0, data=data.frame(d,y0)))
This gives a slope of 1.005. 这给出了1.005的斜率。 The intercept would be coef(lm1)*(-y0/x0)
, I think. 我认为拦截将是coef(lm1)*(-y0/x0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.