计算回归线与数据点之间的距离 [英] calculate distance between regression line and datapoint
问题描述
我想知道是否有一种方法可以计算出绘图中的斜线与数据点之间的距离?例如,concentration == 40
与signal == 643
(元素5)之间的距离是多少?
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)
推荐答案
您基本上是在要求residuals
.
R> residuals(res)
1 2 3 4 5 6
192.61 12.57 -185.48 -205.52 -26.57 212.39
顺便说一句,当您拟合线性回归时,残差之和为0:
As an aside, when you fit a linear regression, the sum of the residuals is 0:
R> sum(residuals(res))
[1] 8.882e-15
,如果模型正确,则应遵循正态分布-qqnorm(res)
.
and if the model is correct, should follow a Normal distribution - qqnorm(res)
.
我发现使用标准化残差更容易.
I find working with the standardised residuals easier.
> rstandard(res)
1 2 3 4 5 6
1.37707 0.07527 -1.02653 -1.13610 -0.15845 1.54918
这些残差已按比例缩放为均值零,方差(大约)等于1并具有正态分布.离群的标准残差是那些大于+/- 2的残差.
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.
这篇关于计算回归线与数据点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!