## BERD R Short Course ## Session 3 Homework Key ###Q1 library(MASS) attach(cats) #a par(mfrow=c(1,2)) hist(Bwt,breaks=20,prob=T,main='hist of Bwt',xlab='Bwt(kg)',col='Red') d1<-density(Bwt) lines(d1) hist(Hwt,breaks=20,prob=T,main='hist of Hwt',xlab='Hwt(g)',col='Blue') d2<-density(Hwt) lines(d2) #b par(mfrow=c(1,2)) qqnorm(Bwt) qqline(Bwt) qqnorm(Hwt) qqline(Hwt) #c par(mfrow=c(1,3)) plot(Bwt,Hwt,col='Red',xlim=c(0,4),ylim=c(6,22),main='Overall Plot',xlab='Bwt',ylab='Hwt') l1<-lm(Hwt~Bwt) abline(l1) plot(Bwt[Sex=='F'],Hwt[Sex=='F'],col='Blue',xlim=c(0,4),ylim=c(6,22),main='Plot for Female',xlab='Bwt',ylab='Hwt') l2<-lm(Hwt[Sex=='F']~Bwt[Sex=='F']) abline(l2) plot(Bwt[Sex=='M'],Hwt[Sex=='M'],col='Green',xlim=c(0,4),ylim=c(6,22),main='Plot for male',xlab='Bwt',ylab='Hwt') l3<-lm(Hwt[Sex=='M']~Bwt[Sex=='M']) abline(l3) detach(cats) ###Q2 attach(ToothGrowth) #a par(mfrow=c(1,3)) boxplot(len~supp,main='len by supp',xlab='supp',ylab='length of odontoblasts') boxplot(len~dose,main='len by dose',xlab='dose',ylab='length of odontoblasts') boxplot(len~supp+dose,main='len by supp and dose',xlab='supp by dose',ylab='length of odontoblasts',col='red') #b len_R<-len len_R[len>=4 & len<6]<-'Tiny' len_R[len>=6 & len<12]<-'Small' len_R[len>=12 & len<18]<-'Medium' len_R[len>=18 & len<26]<-'Big' len_R[len>=26]<-'Huge' slices <- table(len_R) lbls <- c("Big", "Huge", "Medium", "Small", "Tiny") pct <- round(slices/sum(slices)*100) lbls <- paste(lbls, pct) lbls <- paste(lbls,"%",sep="") pie(slices,labels = lbls, col=rainbow(length(lbls)),main="Pie Chart of Tooth")