# # File name: lec09.R # # data example: drug drug = read.table("drug.dat",header=T); drug; # simple linear regression for drug A drug1.lm = lm(y1~x1, data=drug); summary(drug1.lm); anova(drug1.lm); # simple linear regression for drug B drug2.lm = lm(y2~x2, data=drug); summary(drug2.lm); anova(drug2.lm); # re-arrange data newdrug = data.frame(c(drug$x1,drug$x2)); names(newdrug) = "x"; newdrug[,"indicate"] = c(rep(1,length(drug$x1)),rep(0,length(drug$x2))); newdrug[,"interact"] = newdrug$x * newdrug$indicate; newdrug[,"y"] = c(drug$y1,drug$y2); newdrug; # scatter plot for drug A and B plot(newdrug$x, newdrug$y, type="n"); points(newdrug$x[1:10], newdrug$y[1:10], pch=1); points(newdrug$x[11:20], newdrug$y[11:20], pch=2); legend(6,6,c("Line 1","Line 2"),pch=1:2); # fit full model y~w1+w2+w3 newdrug.lm123 = lm(y~x+indicate+interact, data=newdrug); summary(newdrug.lm123); anova(newdrug.lm123); # residual plot plot(newdrug.lm123, which=1); text(fitted(newdrug.lm123)[1:10], resid(newdrug.lm123)[1:10],1,adj=2); text(fitted(newdrug.lm123)[11:20], resid(newdrug.lm123)[11:20],2,adj=2); # fit y~w1 newdrug.lm1 = lm(y~x, data=newdrug); summary(newdrug.lm1); anova(newdrug.lm1); anova(newdrug.lm1, newdrug.lm123); # residual plot plot(newdrug.lm1, which=1); text(fitted(newdrug.lm1)[1:10], resid(newdrug.lm1)[1:10],1,adj=2); text(fitted(newdrug.lm1)[11:20], resid(newdrug.lm1)[11:20],2,adj=2); # fit y~w1+w2 newdrug.lm12 = lm(y~x+indicate, data=newdrug); summary(newdrug.lm12); anova(newdrug.lm12); anova(newdrug.lm12, newdrug.lm123); # fit y~w1+w3 newdrug.lm13 = lm(y~x+interact, data=newdrug); summary(newdrug.lm13); anova(newdrug.lm13); anova(newdrug.lm13, newdrug.lm123);