the*_*ail 14 plot r point line least-squares
我试图绘制一个最小二乘回归线使用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")
Run Code Online (Sandbox Code Playgroud)
这看起来像:

现在,我将如何强制线穿过标记的任意点,(x=10,y=50)同时仍然最小化到其他点的距离?
# force through [10,50] - red line
??
Run Code Online (Sandbox Code Playgroud)
mne*_*nel 14
一个粗略的解决方案是将模型的原点移动到该点,并创建一个没有截距的模型
nmod <- (lm(I(y-50)~I(x-10) +0, test))
abline(predict(nmod, newdata = list(x=0))+50, coef(nmod), col='red')
Run Code Online (Sandbox Code Playgroud)
