############################################ #Aiken, L. S., & West, S. G. (1991). #Interactions between categorical and continuous variables #In Multiple Regression: Testing and interpreting interactions (pp.116-138). #Thousand Oaks: Sage. ############################################ ##(a) データの読み込み (Table 7.2) dat <- read.csv("http://blue.zero.jp/yokumura/R/reg/Aiken.csv",header=T) dat dat$college <- factor(dat$college,levels=c("LA","E","BUS")) attach(dat) tapply(salary,college,mean) tapply(GPA,college,mean) ##(b) Categorical Variable Only p.118-119 reg1 <- lm(salary~college) summary(reg1) #Joint test of b_1, b_2 anova(reg1) #SSreg, SSres par(mfrow=c(2,2)) plot(salary~GPA,type="n",ylim=c(0,max(salary)), main="Figure 7.1a") abline(h=c(tapply(salary,college,mean)),lty=1:3,col=2:4) legend(3,20000,levels(college),lty=1:3,col=2:4) ##(c) Categorical and Continuous Variable p.119-123 cGPA <- GPA-mean(GPA) #centering reg2 <- lm(salary~college+cGPA) #eq7.2 summary(reg2) anova(reg1, reg2) anova(reg2) coef1 <- coef(reg2) plot(salary~cGPA,type="n",xlim=c(-2.78,1.22),ylim=c(0,max(salary)*1.1)) abline(c(coef1[1], coef1[4]), lty=1,col=2) abline(c(coef1[1]+coef1[2], coef1[4]), lty=2,col=3) abline(c(coef1[1]+coef1[3], coef1[4]), lty=3,col=4) legend(0,20000,levels(college),lty=1:3,col=2:4) ##(d) Categorical and Continuous Variables and their Interaction p.123-126 reg3 <- lm(salary~college*cGPA) summary(reg3) anova(reg2, reg3) anova(reg3) coef2 <- coef(reg3) plot(salary~cGPA,type="n",xlim=c(-2.78,1.22),ylim=c(0,max(salary)*1.1)) abline(c(coef2[1], coef2[4] ),lty=1,col=2) abline(c(coef2[1]+coef2[2], coef2[4]+coef2[5]),lty=2,col=3) abline(c(coef2[1]+coef2[3], coef2[4]+coef2[6]),lty=3,col=4) legend(0,20000,levels(college),lty=1:3,col=2:4) graphics.off() detach(dat)