R 通过任意点绘制(直线和长线)最佳拟合线
我正在尝试使用R 通过任意点绘制(直线和长线)最佳拟合线,r,plot,line,point,least-squares,R,Plot,Line,Point,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),
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")
一个粗略的解决方案是将模型的原点移到该点,并创建一个没有截距的模型
nmod <- (lm(I(y-50)~I(x-10) +0, test))
abline(predict(nmod, newdata = list(x=0))+50, coef(nmod), col='red')
nmod您可以修改lm()
的公式并偏移数据:
p=10
q=50
abline(lm(I(y-q) ~ I(x-p) + 0, data=test), col="red")
美好的这似乎不是一个简单的解决方案,简单?不是从计算的角度,而是从统计的角度。你会发现Bill Venables(来自Venables/Ripley MASS)在这个主题上的一些咆哮,没关系,我没有把它用于任何严肃的事情,它更多的是一个思考项目。我要读一读:统计问题。
p=10
q=50
abline(lm(I(y-q) ~ I(x-p) + 0, data=test), col="red")