14

我正在尝试使用它来绘制一条最小二乘回归线abline(lm(...)),它也被迫通过一个特定的点。我看到这个问题是相关的,但不是我想要的。这是一个例子:

test <- structure(list(x = c(0, 9, 27, 40, 52, 59, 76), y = c(50, 68, 
79, 186, 175, 271, 281)), .Names = c("x", "y"))

# set up an example plot
plot(test,pch=19,ylim=c(0,300),
     panel.first=abline(h=c(0,50),v=c(0,10),lty=3,col="gray"))

# standard line of best fit - black line
abline(lm(y ~ x, data=test))

# force through [0,0] - blue line
abline(lm(y ~ x + 0, data=test), col="blue")

这看起来像:

在此处输入图像描述

现在我将如何强制一条线穿过标记的任意点,(x=10,y=50)同时仍然最小化与其他点的距离?

# force through [10,50] - red line
??
4

2 回答 2

14

一个粗略的解决方案是将模型的原点移动到该点并创建一个没有截距的模型

nmod <- (lm(I(y-50)~I(x-10) +0, test))

abline(predict(nmod, newdata = list(x=0))+50, coef(nmod), col='red')

在此处输入图像描述

于 2013-04-22T06:28:08.723 回答
3

您可以修改公式lm()并偏移数据:

p=10
q=50

abline(lm(I(y-q) ~ I(x-p) + 0, data=test), col="red")
于 2013-04-22T06:36:55.323 回答