I'm trying to understand how multiple regression statistically controls for the effects of other predictor variables when calculating partial regression slopes. In a multiple regression of Y~X1+X2, would the partial regression slope of X1 be given by [Y]~[residuals of X1~X2], or by [residuals of Y~X2] ~ [residuals of X1~X2]? Different pages on the internet tell me different things.
I've done some simulations to try and figure this out (see below), and it seems that both methods give the same estimates of slopes as multiple regression, but only the latter method has similar standard errors around those estimates. This makes me think the latter method is the one that multiple regression uses, but it would be nice to know for sure.
Similarly, if I wanted to plot Y against X1 so that I could visualise how strongly the two were related while also controlling for any confounding with X2, would I plot [Y]~[residuals of X1~X2], or [residuals of Y~X2] ~ [residuals of X1~X2]? These two plots in the code below look very different in terms of the strength of the relationship.
Thanks for your help,
Jay
#1. simulate data, where x1 and x2 are correlated due to lurking variable,
#...and y is explained by both.
lurker <- rnorm(n=100)
x1 <- rnorm(n=100, mean=lurker*2, sd=1)
x2 <- rnorm(n=100, mean=lurker*5, sd=1)
y <- rnorm(n=100, mean=x1*2 + x2*5, sd=1)
#2. multiple regn model to estimate partial slopes:
summary(lm(y~x1+x2)) #partial slopes pretty close to simulated values
#3. calculate partial slopes manually, using either
#....(1) Y~[resids of X1~X2] OR (2) [resids of Y~X2]~[resids of X1~X2]
#3.a. based on (1) Y~[resids of X1~X2]
m.x1x2 <- lm(x1~x2)
resids.x1x2 <- m.x1x2$residuals
summary(lm(y ~resids.x1x2)) #slope pretty close to true value, but conf intervals MUCH larger than those for MR estimate
#3.b. based on (2) [resids of Y ~ X2]~[resids of X1~X2]
m.y1x2 <- lm(y~x2)
resids.y1x2 <- m.y1x2$residuals
summary(lm(resids.y1x2 ~resids.x1x2)) #also very close to true value, but conf intervals now similar scale to those from MR.
# plot the relationship between y and x1 after controlling for x2, based on the different methods:
op <- par(mfrow=c(2,1))
plot(y ~resids.x1x2)
plot(resids.y1x2 ~resids.x1x2)
par(op)