# R commands for Chapter 2

 

# Section 2.3

 

# Read the WCGS data for from the web-page of the book into a data frame (needs only to be done once if the workspace is saved)

wcgs=read.table("http://www.uio.no/studier/emner/matnat/math/STK4900/v11/wcgs.txt",sep="\t",header=T,na.strings=".")

 

 

# Attach the data frame (remember to detach when the data frame is no longer in use):

attach(wcgs)

 

# Summary of systolic blood pressure (cf Table 2.1, page 9):

summary(sbp)

sd(sbp)

var(sbp)

 

# Histogram of systolic blood pressure (cf Fig 2.2, page 11):

hist(sbp)    # Basic frequency histogram

hist(sbp,freq=F,col="grey",xlab="Systolic Blood Pressure",main=" ")    # Density histogram resembling Fig 2.2

 

# Boxplot of systolic blood pressure (cf Fig 2.3, page 12):

boxplot(sbp,ylab="Systolic Blood Pressure")

 

# Normal Q-Q plot of systolic blood pressure (cf Fig 2.4, page 13):

qqnorm(sbp)       # Basic plot

qqnorm(sbp,ylim=c(50,250),ylab="Systolic Blood Pressure"); qqline(sbp)      #Plot resembling Fig 2.4

 

# Histogram of systolic blood pressure and its natural logaritm (cf Fig 2.7, page 16):

par(mfrow=c(1,2))

hist(sbp,freq=F,col="grey",xlab="Systolic Blood Pressure",main=" ") 

hist(log(sbp),freq=F,col="grey",xlab="Ln of Systolic Blood Pressure",main=" ")

par(mfrow=c(1,1))

 

# Frequencies of behaviour patterns (cf Table 2.3, page 17):

table(behpat)

 

 

# Section 2.4

 

# Correlation coefficient for systolic blood pressure and weight (cf Table 2.5, page 18):

cor(sbp,weight)

 

# Scatterplot of systolic blood pressure versus weight (cf Fig 2.8, page 19):

plot(weight,sbp)    # Basic plot

plot(weight,sbp,pch=20,ylim=c(100,250),xlab="Weight (lbs)",ylab="Systolic Blood Pressure")    # Plot resembling Fig 2.8

 

# LOWESS smooth of systolic blood pressure versus weight (cf Fig 2.9, page 20):

plot(weight,sbp); lines(lowess(weight,sbp))    # Basic plot

plot(weight,sbp,pch=20,ylim=c(100,250),xlab="Weight (lbs)",ylab="Systolic Blood Pressure")    # Plot resembling Fig 2.9

lines(lowess(weight,sbp,iter=0,f=0.25),lwd=2,col="red")

 

# Summary of systolic blood pressure by behavior pattern (cf Tables 2.6 and 2.7, page 21):

summary(sbp[behpat==1])

summary(sbp[behpat==2])

summary(sbp[behpat==3])

summary(sbp[behpat==4])

 

# Boxplot of systolic blood pressure by behavior pattern(cf Fig 2.10, page 22):

boxplot(sbp~behpat,xlab="Bahavior Pattern",ylab="Systolic Blood Pressure")

 

# Behavior pattern by weight category (cf Table 2.8, page 22):

table(behpat,wghtcat) 

 

 

# Section 2.5

 

# Correlation matrix for systolic blood pressure, age, weight and height (cf Table 2.9, page 23):

cor(wcgs[,c("sbp","age","weight","height")]) 

 

# Scatterplot matrix of systolic blood pressure, age, weight and height (cf Fig 2.11, page 24):

plot(wcgs[,c("sbp","age","height", "weight")])     # Basic plot

plot(wcgs[,c("sbp","age","height", "weight")],labels=c("Systolic \n blood \n pressure","Age","Height \n (inches)","Weight \n (lbs)"))      # Plot with nice labels

 

# CHD events and behavior pattern by weight category (cf Table 2.10, page 24):

table(chd69,behpat,wghtcat) 

 

# Scatterplot of systolic blood pressure versus weight by behavior pattern (cf Fig 2.12, page 25):

par(mfrow=c(2,2))

plot(weight[behpat==1],sbp[behpat==1], xlim=c(80,320),ylim=c(100,250),xlab="Weight (lbs)",ylab="Systolic Blood Pressure",main="A1")

plot(weight[behpat==2],sbp[behpat==2], xlim=c(80,320),ylim=c(100,250),xlab="Weight (lbs)",ylab="Systolic Blood Pressure",main="A2")

plot(weight[behpat==3],sbp[behpat==3], xlim=c(80,320),ylim=c(100,250),xlab="Weight (lbs)",ylab="Systolic Blood Pressure",main="B1")

plot(weight[behpat==4],sbp[behpat==4], xlim=c(80,320),ylim=c(100,250),xlab="Weight (lbs)",ylab="Systolic Blood Pressure",main="B2")

par(mfrow=c(1,1))