我想用(a = 0和b = 1)绘制一条回归线,并从中添加各个点的偏差,并用名称来标识数据点。

set.seed(123)
namelab <- paste ("ET", 1:10, sep = "")
xvar <- 1:10
yvar <- rnorm(10, 5, 5)
myd <- data.frame(namelab, xvar, yvar)
plot(xvar, yvar)
abline (a= 0, b = 1, col = "red", lty = 2)

只是我意图的手动草绘,例如,我只是标记了一个点。画出的线需要修长。

最佳答案

dev.new(width=4, height=4)
plot(xvar, yvar, asp=1)

a = 0
b = 1

abline (a, b, col = "red", lty = 2)

myd$xint = with(myd, (b*yvar + xvar - b*a) / (b^2 + 1))
myd$yint = with(myd, (b*yvar + b*xvar + a) / (b^2 + 1))

with(myd, segments(xvar, yvar, xint, yint))
with(myd, text(xvar, yvar, labels=namelab, pos=3, cex=0.5))

关于r - 绘制与回归线的偏差,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8075109/

10-12 17:32