# Chapter 19, Exercises 8 and 9 # Input lowbwt data lowbwt <- read.table("lowbwt.txt",header=T, sep="\t",quote="", skip=1, row.names=NULL) ####################### sbp <- lowbwt[[1]] sex <- lowbwt[[2]] gestage <- lowbwt[[5]] apgar5 <- lowbwt[[6]] #Exercise 8 ################################## #(a) Scatter plot plot(gestage, sbp, xlab="Gestational age", ylab="SBP") plot(apgar5, sbp) #(b) out1 <- lm(sbp ~ gestage) summary(out1) out2 <- lm(sbp ~ gestage+apgar5) summary(out2) #(c) The fitted model is y.hat = 9.8034 + 1.1848 * gestage + 0.4875 * apgar5 # For the population of infants with gestage=31 and apgar5=7, the estimated mean systolic blood pressure is y.hat = 9.8034 + 1.1848 * 31 + 0.4875 * 7 = 49.945 mmHg #(d) Confidence interval for the mean sbp of infants whose # gestage=31 and apgar5=7 new <- data.frame(gestage = 31, apgar5=7) predict(lm(sbp ~ gestage+apgar5), newdata=new, int="confidence", se.fit=T ) #Prediction interval for an infant with gestage=31 and apgar5=7 new <- data.frame(gestage = 31, apgar5=7) predict(lm(sbp ~ gestage+apgar5), newdata=new, interval="prediction", se.fit=T) #(g) Model evaluation res <- out2$residuals fitted <- out$fitted.values plot(fitted, res) plot(gestage, res) ####################################################################### #Exercise 9 out3 <- lm(sbp ~ gestage+sex) summary(out3) out4 <- lm(sbp ~ gestage+sex+sex*gestage) summary(out4)