#Program to analyze the data in Table 2.1 for Example 2.1
yd=read.table("C:\\Research\\Book\\data\\table21.txt")
id  = yd[[1]]
age = yd[[2]]
sbp = yd[[3]]
con = rep(1,length(age))
dma = cbind(con, age, sbp)
cprod = t(dma)%*%dma
cprod  #cross products X'X, X'Y, Y'Y with the intercept
av.age=mean(age)
av.sbp=mean(sbp)
sd.age=sqrt(var(age))
sd.sbp=sqrt(var(sbp))
nage = age - av.age
nsbp = sbp - av.sbp
ndma = cbind(nage, nsbp)
ncprod=t(ndma)%*%ndma
ncprod  #corrected cross products
desc.age=summary(age)
desc.sbp=summary(sbp)
rbind(desc.age, desc.sbp)  #Descriptives statistics for age and sbp
rbind(sd.age, sd.sbp)
#Regression analysis with sbp as response and age as predictor
simple = sbp ~ age
lreg = lm(simple)
lreg
summary.lm(lreg)
anova.lm(lreg)
confint(lreg, parm=c(1, 2), level=0.95)
confint(lreg, parm=c(1, 2), level=0.99)
fitted=predict.lm(lreg)
#Plot of regression line overlayed with the scatter plots
plot(age, sbp)
lines(age, fitted)