Drop lines from actual to modeled points in R
?segments
I'd provide an example, but I'm pretty busy today and it's not that complicated to pick the points. ;-)
Okay, so I'm not that busy...
n=58; segments(x[n],y[n],x[n],predict(yx.lm)[n])
n=65; segments(x[n],y[n],x[n],predict(yx.lm)[n])
As Joshua mentioned, segments()
is the way to go here. And as it is totally vectorised, we can add in all the errors at once, following on from your example
set.seed(2)
x <- 1:100
y <- 20 + 3 * x
e <- rnorm(100, 0, 60)
y <- 20 + 3 * x + e
plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")
## Add segments
segments(x, y, x, fitted(yx.lm), col = "blue")
If you only want to highlight a couple of the errors, then to modify the example Joshua gave:
n <- c(58,65)
segments(x[n], y[n], x[n], fitted(yx.lm)[n], col = "orange", lwd = 3)
HTH
G