# Generate data
dat <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100))
# Build model
model <- lm(y ~ x1 + x2, dat)
# Compute partial residuals of y ~ x1
yresid <- resid(lm(y ~ x2, dat))
xresid <- resid(lm(x1 ~ x2, dat))
plot(xresid, yresid)
# Use partialResid
presid <- partialResid(y ~ x1, model)
with(presid, plot(xresid, yresid)) # identical plot!
Run the code above in your browser using DataLab