计算回归线和数据点之间的距离
我想知道是否有一种方法可以计算图中的 abline 和数据点之间的距离?例如,concentration == 40
和 signal == 643
(元素 5)与 abline 之间的距离是多少?
concentration <- c(1,10,20,30,40,50)
signal <- c(4, 22, 44, 244, 643, 1102)
plot(concentration, signal)
res <- lm(signal ~ concentration)
abline(res)
I wonder if there is a way to calculate the distance between a abline in a plot and a datapoint? For example, what is the distance between concentration == 40
with signal == 643
(element 5) and the abline?
concentration <- c(1,10,20,30,40,50)
signal <- c(4, 22, 44, 244, 643, 1102)
plot(concentration, signal)
res <- lm(signal ~ concentration)
abline(res)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您基本上是在要求
残差
。顺便说一句,当您拟合线性回归时,残差之和为 0:
如果模型正确,则应遵循正态分布 -
qqnorm(res)
。我发现使用标准化残差更容易。
这些残差已缩放至均值为零、方差(大约)等于 1 且呈正态分布。外围标准化残差是那些大于 +/- 2 的残差。
You are basically asking for the
residuals
.As an aside, when you fit a linear regression, the sum of the residuals is 0:
and if the model is correct, should follow a Normal distribution -
qqnorm(res)
.I find working with the standardised residuals easier.
These residuals have been scaled to have mean zero, variance (approximately) equal to one and have a Normal distribution. Outlying standardised residuals are those larger that +/- 2.
您可以使用以下函数:
http://paulbourke.net/geometry/pointlineplane/pointline.r
然后只需提取斜率和截距:
所以你的最终答案是:
如果你想要一个更通用的要找到特定点的解决方案,
concentration == 40
返回长度为length(concentration)
的布尔向量。您可以使用该向量来选择点。不幸的是,distancePointLine 似乎没有被矢量化(或者它确实被矢量化,但当您向它传递矢量时它会返回警告)。否则,只需将 [] 选择器从 x 和 y 参数中删除即可获得所有点的答案。
You can use the function below:
http://paulbourke.net/geometry/pointlineplane/pointline.r
Then just extract the slope and intercept:
So your final answer would be:
If you want a more general solution to finding a particular point,
concentration == 40
returns a Boolean vector of lengthlength(concentration)
. You can use that vector to select points.Unfortunately distancePointLine doesn't appear to be vectorized (or it does, but it returns a warning when you pass it a vector). Otherwise you could get answers for all points just by leaving the [] selector off the x and y arguments.