library(ggplot2) #Example 1: Is the proportion of baby boys born different than 0.5? # Is it different from the proportion of baby girls? #Visualize/Format Data total<-1000 boys <-584 girls<-total-boys ds1.1<-data.frame(category=c("boys", "constant"), proportion=c(boys/total, 0.5)) barplot(proportion~category, col=c("blue","grey"), data=ds1, xlab="Boys versus Constant")+box() ds1.2<-data.frame(category=c("boys", "girls"), proportion=c(boys/total, girls/total)) barplot(proportion~category, col=c("blue","pink"), data=ds1.2, xlab="Boys versus Girls")+box() #One-Sample Test f1.1<-prop.test(x=584, n=1000, p=0.50, alternative="two.sided") f1.1 #Two-Sample Test f1.2<-prop.test(c(boys,girls), c(total,total), alternative ="two.sided") f1.2 #Graphs plot(1, f1.1$estimate, ylim=c(0,1), axes=F, xlab="Boys", ylab="Proportion") axis(side=1, at=1, cex.lab=1.5, cex.axis=1.2, labels="") axis(2, at=c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), las=2, cex.lab=1.5, cex.axis=0.80) arrows(1, f1.1$conf.int[1], 1, f1.1$conf.int[2],length=0.05, angle=90, code=3) abline(h=0.5, col="red") box() diff<-f1.2$estimate[1]-f1.2$estimate[2] plot(1, diff, ylim=c(0,1), axes=F, xlab="Difference between Boys and Girls", ylab="Proportion") axis(side=1, at=1, cex.lab=1.5, cex.axis=1.2, labels="") axis(2, at=c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), las=2, cex.lab=1.5, cex.axis=0.80) arrows(1, f1.2$conf.int[1], 1, f1.2$conf.int[2],length=0.05, angle=90, code=3) abline(h=0.0, col="red") box() ################################################################################ #Example 2: Is there a significant association between Treatment Status (Treatment=T, Control=C) # and Disease Outcome (Cured=1, Not-cured=0)? #Visualize/Format Data ds2 <- matrix(c(15,3,5,19), nrow = 2) colnames(ds2) <- c("Treat", "Control") rownames(ds2) <- c("Cured", "Not") ds2 #Chi-Square test of Independence f2.1<-chisq.test(ds2) #some cells <5 f2.1 #Fisher's Exact Test f2.2<-fisher.test(ds2) f2.2 #Graph mosaicplot(ds2, color=T) ################################################################################ #Example 3: Are the phenotype ratios for a sample of pea plants equal to the hypothesized 9:3:3:1? #Visualize/Format Data observed <-c(56,13,15,7) expected <-c(9/16,3/16,3/16,1/16) ds3 <- matrix(c(observed,expected), nrow = 4) colnames(ds3) <- c("Observed", "Expected") rownames(ds3) <- c("AB", "Ab", "aB", "aa") ds3 #Chi-square goodness of fit f3.1<-chisq.test(x=observed, p=expected) f3.1 #Graph ds3.2 <-data.frame(type=c(rep("Observed",4),rep("Expected",4)), genotype=c(rep(c("AB","Ab","aB","ab"),2)), proportion=c(observed/sum(observed),expected)) ggplot(data=ds3.2, aes(x=genotype, y=proportion)) + geom_bar(aes(fill=type), stat="identity", position=position_dodge())