- Homework 3 is out
- For the next two Fridays, the first hour of class will be a lecture
- Read Introduction to Statistics with R (Chapter 5: T- and Wilcoxon tests)
Patrick D. Schloss, PhD (microbialinformatics.github.io)
Department of Microbiology & Immunology
xbinom
functions in Rpbinom(2, 10, 0.5)
2 * pbinom(2, 10, 0.5)
binom.test(2, 10, 0.5)
binom.test(2, 10, 0.5, alternative = "less")
metadata <- read.table(file = "wild.metadata.txt", header = T)
rownames(metadata) <- metadata$Group
metadata <- metadata[, -1]
table(metadata$Sex)
table(metadata$Sex, metadata$SP)
table(metadata$Sex, metadata$SP, metadata$Repro)
sex.sp <- table(metadata$Sex, metadata$SP)
margin.table(sex.sp)
margin.table(sex.sp, 1)
margin.table(sex.sp, 2)
prop.table(sex.sp)
prop.table(sex.sp, 1)
prop.table(sex.sp, 2)
##
## PL PMG
## F 24 36
## M 34 17
species.sums <- margin.table(sex.sp, 2)
binom.test(species.sums)
##
## Exact binomial test
##
## data: species.sums
## number of successes = 58, number of trials = 111, p-value = 0.7044
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
## 0.4256 0.6182
## sample estimates:
## probability of success
## 0.5225
sex.sums <- margin.table(sex.sp, 1)
binom.test(sex.sums)
##
## Exact binomial test
##
## data: sex.sums
## number of successes = 60, number of trials = 111, p-value = 0.4478
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
## 0.4433 0.6355
## sample estimates:
## probability of success
## 0.5405
frac.male <- sex.sums["M"]/sum(sex.sums)
frac.female <- 1 - frac.male
frac.sex <- c(F = frac.female, M = frac.male)
frac.pl <- species.sums["PL"]/sum(species.sums)
frac.pmg <- 1 - frac.pl
frac.species <- c(PL = frac.pl, PMG = frac.pmg)
expected <- frac.sex %*% t(frac.species)
expected <- expected * sum(sex.sp)
expected
## PL.PL PMG.PL
## [1,] 31.35 28.65
## [2,] 26.65 24.35
\[\chi^2 =\sum \frac{(O_{ij}-E_{ij})^2}{E_{ij}}\]
chi.sq <- sum((expected - sex.sp)^2/expected)
df <- (nrow(sex.sp) - 1) * (ncol(sex.sp) - 1)
plot(seq(0, 20, 0.05), dchisq(seq(0, 20, 0.05), df = df), type = "l", xlab = "ChiSquared Statistic",
ylab = "Probability with 1 degree of freedom")
arrows(x0 = chi.sq, x1 = chi.sq, y0 = 0.4, y1 = 0.05, lwd = 2, col = "red")
chisq.test(sex.sp)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: sex.sp
## X-squared = 6.825, df = 1, p-value = 0.00899
\[P=\frac{(a+b)!(c+d)!(a+c)!(b+d)!}{a!b!c!d!}\]
fisher.test(sex.sp)
##
## Fisher's Exact Test for Count Data
##
## data: sex.sp
## p-value = 0.007411
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.1418 0.7765
## sample estimates:
## odds ratio
## 0.3368
males <- sex.sp["M", ]
prop.test(males, species.sums)
##
## 2-sample test for equality of proportions with continuity
## correction
##
## data: males out of species.sums
## X-squared = 6.825, df = 1, p-value = 0.00899
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## 0.06891 0.46199
## sample estimates:
## prop 1 prop 2
## 0.5862 0.3208