# 1 ii<-read.csv('nhanes.csv') plot(x=ii$lbdldl,y=ii$bpxsy1, xlab='LDL cholesterol (mg/dL)', ylab='Systolic blood pressure (mg)') # 2 library(ggplot2) gg<-ggplot(data=ii,mapping=aes(x=lbdldl,y=bpxsy1)) gg+ geom_point()+ geom_smooth(method='loess')+ xlab('LDL cholesterol (mg/dL)')+ ylab('Systolic blood pressure (mg)') # 3 gg+ geom_point()+ geom_smooth(method='lm')+ xlab('LDL cholesterol (mg/dL)')+ ylab('Systolic blood pressure (mg)') # 4 cor(x=ii$lbdldl,y=ii$bpxsy1,use='complete.obs') # 5 gg<-ggplot(data=ii,mapping=aes(x=hs,y=bpxsy1)) gg+ geom_boxplot()+ xlab('')+ ylab('Systolic blood pressure (mg)') # 6 t.test(ii$bpxsy1~ii$hs) # 7 (note that I modified the slides) ii$hs<-relevel(ii$hs,'No') # only necessary for consistency with slides paste(round(t.test(ii$bpxsy1~ii$hs)$conf.int,1),collapse=' to ') # 8 gg<-ggplot(data=ii,mapping=aes(x=ridageyr,y=lbdldl,color=gender)) gg+ geom_point()+ xlab('Age (years)')+ ylab('LDL cholesterol (mg/dL)') # 9 mm<-lm(lbdldl~ridageyr+gender,data=ii) mm # 10 nn<-data.frame( ridageyr=c(52,35,47,61), gender=c('Male','Female','Female','Male') ) predict(mm,nn)