[英]Residuals from 1:1 line
我想测量一组点和1:1线之间的距离。 我可以构建一个线性模型并从最佳拟合中获得残差,但我无法从1:1线获得该度量。 任何有用的提示?
#build a df of random numbers
x=runif(100, 0, 100)
y=runif (100, 0, 100)
df=cbind (x,y)
df=as.data.frame(df)
#build a linear model
lm1<-lm(y~x, data=df)
summary (lm1)
#plot the data, lm best fit and 1:1 (red) line)
plot (y~x, data=df, pch=16)
line (lm1)
abline abline(0,1, col="red")
#get residulas for the linear model
y.resid= resid (lm1)
我建议使用yx
,就像@vpipkt建议的那样。 只是为了完整性:您还可以创建一个固定系数为yx ~ 0
的线性模型,并在那里获取残差。
resid(lm(y-x ~ 0))
当然这只是更复杂并且给出与yx
相同的结果,但是它明确地表明你正在使用残差而不是计算到该线的最小距离(cf @ user3969377的答案)。
要确定一组点与1:1线之间的距离,请使用
dist[x-y=0; (x0,y0)] = abs(x0 - y0) / sqrt(2)
参考http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
以你为例,
par(pty="s")
plot (y~x, data=df, pch=16)
line (lm1)
abline(0,1, col="red")
#get residulas for the linear model
y.resid= resid (lm1)
a=1;b=-1;c=0
xi = (b*(b*x-a*y)-a*c) / (a^2+b^2)
yi = (a*(-b*x+a*y)-b*c) / (a^2+b^2)
segments(x,y,xi,yi,col="blue")
yr = abs(a*x+b*y+c)/sqrt(a^2+b^2)
hist(yr)
在模型y = x的残差意义上,距离只是“y-x”。
r = y-x
plot(r~x)
abline(h=0)
您可以将其扩展为更一般的线性模型y = ax + b。 剩余物是
r = y - ax - b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.