# Unit 6 for R # 030528 prepared by C. Andy Tsao # Topic : Simple Linear Regression # cf. Chapter 11 of Johnson and Bhattacharyya # We will use the data in Table 1 (cf. also Table 4) P 480,489 # to illustrate the steps, ideas and output of simple # linear regression allergy<-matrix(scan("C11T1.dat"),ncol=2,byrow=T) dose<-allergy[,1];dura<-allergy[,2]; # prelim analysis # Initiate graphic devices par(mfrow=c(2,2)); stem(dose);stem(dura); hist(dose);hist(dura); boxplot(dose);boxplot(dura) # Prelim Normal Check hist(dura); boxplot(dura); plot(density(dura)); qqnorm(dura); qqline(dura); # Plot "y" vs "x" plot(dura~dose) # Simple Linear Regression fm0<-lm(dura~1) # with intercept only summary(fm0) fm1<-lm(dura~dose) # Y = b_0 + b_1 X + epsilon summary(fm1) # Plot the fitted line overlayed over the y vs. x scatter plot plot(dose,dura,xlab="dose", ylab="dura", main="Simple Linear Regression"); abline(coef(fm1)) # Residual analysis: Outliers and influential points; Normality r<-fm1$res par(mfrow=c(2,2)); hist(r); boxplot(r); plot(density(r)); qqnorm(r); qqline(r); # An experiment of effects of outliers dura[1] dura[1]<-90 # Now redo the analysis and compare with previous output