x <- c(173, 169, 176, 166, 161, 164, 160, 158, 180, 187)
y <- c(80, 68, 72, 75, 70, 65, 62, 60, 85, 92)
# plot scatterplot and the regression line
mod1 <- lm(y ~ x)
plot(x, y, xlim=c(min(x)-5, max(x)+5), ylim=c(min(y)-10, max(y)+10))
abline(mod1, lwd=2)
# calculate residuals and predicted values
res <- signif(residuals(mod1), 5)
pre <- predict(mod1)
# plot distances between points and the regression line
segments(x, y, x, pre, col="red")
# add labels (res values) to points
library(calibrate)
textxy(x, y, res, cx=0.7)
Thursday, June 16, 2011
How to plot points, regression line and residuals
Subscribe to:
Post Comments (Atom)
Note you can use the function extendrange to avoid hardcoding specific x and y values.
ReplyDeleteplot(x, y, xlim=extendrange(x), ylim=extendrange(y))
Thanks for this useful tips, Steven! :)
ReplyDelete