# Ch 7: Basic concepts of regression # R code ## Figure 7.1. Examples of interaction patterns par(mfrow=c(2,2)) x11 <- 10 x21 <- 20 y11 <- .05 y12 <- .10 y21 <- .10 y22 <- .15 plot(c(x11,x21),c(y11,y12),xlab=expression(x[1]),ylab="Mean", main="No Interaction", type="l", ylim=c(0,.20), lwd=2, cex=2) lines(c(x11,x21),c(y21,y22),type="l",lty=2, lwd=2, cex=2) #legend(16,.05, c("x2=0", "x2=1"), lty=c(1,2),bty="n") x11 <- 10 x21 <- 20 y11 <- .10 y12 <- .15 y21 <- .05 y22 <- .20 plot(c(x11,x21),c(y11,y12),xlab=expression(x[1]),ylab="Mean", main="Interaction", type="l", ylim=c(0,.20), lwd=2) lines(c(x11,x21),c(y21,y22),type="l",lty=2, lwd=2) #legend(16,.05, c("x2=0", "x2=1"), lty=c(1,3),bty="n") x11 <- 10 x21 <- 20 y11 <- .10 y12 <- .20 y21 <- .05 y22 <- .10 plot(c(x11,x21),c(y11,y12),xlab=expression(x[1]),ylab="Mean", main="Interaction", type="l", ylim=c(0,.20), lwd=2) lines(c(x11,x21),c(y21,y22),type="l",lty=2, lwd=2) #legend(16,.05, c("x2=0", "x2=1"), lty=c(1,3),bty="n") x11 <- 10 x21 <- 20 y11 <- .05 y12 <- .10 y21 <- .20 y22 <- .15 plot(c(x11,x21),c(y11,y12),xlab=expression(x[1]),ylab="Mean", main="Interaction", type="l", ylim=c(0,.20), lwd=2) lines(c(x11,x21),c(y21,y22),type="l",lty=2, lwd=2) #legend(16,.05, c("x2=0", "x2=1"), lty=c(1,3),bty="n") ## FEV data FEVdata <- read.table(file="F:\\MyDocuments\\BAYES\\LinearRegression\\CodeandData\\FEVdataAge10to19.txt",header=T, sep="\t") attach(FEVdata) options(contrasts=c("contr.treatment","contr.poly")) ## Interaction plot plot(Age,FEV,xlab="Age", ylab="FEV") lines(lowess(Age[Smoke==1],FEV[Smoke==1],f=.95),lwd=2,cex=1.2) lines(lowess(Age[Smoke==0],FEV[Smoke==0],f=.95),lty=3,lwd=2,cex=1.2) legend("bottomright", c("Smoker", "Nonsmoker"), lty=c(1,3)) ## Interaction plot. LOWESS plot for the FEV data plot(Age,FEV,xlab="Age", ylab="FEV") lines(lowess(Age[Smoke==1],FEV[Smoke==1],f=.95),lwd=2,cex=1.2) lines(lowess(Age[Smoke==0],FEV[Smoke==0],f=.95),lty=2,lwd=2,cex=1.2) legend("bottomright", c("Smoker", "Nonsmoker"), lty=c(1,2)) ## Figure 7.2. Plot of Bayesian posterior means from a linear regression with interaction term and diffuse prior for the FEV data BayesSmoker <- c(3.058,3.124,3.191,3.258,3.324,3.391,3.458,3.525,3.591,3.658) BayesNonsmoker <- c(2.772,2.982,3.192,3.402,3.612,3.821,4.031,4.241,4.451,4.661) Age1 <- seq(10,19,1) plot(Age,FEV,xlab="Age", ylab="FEV", lwd=1.5) lines(Age1,BayesSmoker,type='l', lty=1, lwd=2,cex=1.2) lines(Age1,BayesNonsmoker,type='l', lty=2, lwd=2,cex=1.2) legend("bottomright", c("Smoker", "Nonsmoker"), lty=c(1,2), lwd=2)