source("http://www.uvm.edu/~rsingle/stat221/data/scripts-221.R") dat <- bookdata("ch05q02.txt") head(dat) y.x1 <- lm(SBP~QUET, data=dat) y.x2 <- lm(SBP~AGE, data=dat) y.x1x2 <- lm(SBP~QUET+AGE, data=dat) y.x2x1 <- lm(SBP~AGE+QUET, data=dat) #Recode the data in terms of Y, X1, X2, & X3 Y <- dat$SBP X1 <- dat$QUET X2 <- dat$AGE X3 <- dat$SMK X0 <- rep(1, times=length(X1)) dat$PERSON <- NULL cor(dat) pairs(dat) #R.y|x1,x2 vs R.squared cor(Y, y.x1x2$fit) summary(y.x1x2)$r.squared #Partial Correlation bet Y & X1 controlling for X2 r.yx1 <- cor(Y,X1) r.yx2 <- cor(Y,X2) r.x1x2 <- cor(X1,X2) r.yx1.x2 <- (r.yx1 - r.yx2*r.x1x2)/sqrt((1-r.yx2^2)*(1-r.x1x2^2)) r.yx1.x2 x1.x2 <- lm(X1~X2) cor(y.x2$resid, x1.x2$resid) #Partial Correlation bet Y & X2 controlling for X1 x2.x1 <- lm(X2~X1) r.yx2.x1 <- cor(y.x1$resid, x2.x1$resid) r.yx2.x1 anova(y.x1x2) anova(y.x2x1)