# KeepNotes blog

Stay hungry, Stay Foolish.

0%

http://www.sthda.com/english/wiki/comparing-proportions-in-r

#### One-Proportion Z-Test

One-Proportion Z-Test用于比较观测到的比例和理论比例是否有差别（具体看零假设）

Z统计量：

$z = \frac{p-p0}{\sqrt{p0(1-p0)/n}}$

$p \pm 1.96\sqrt{\frac{pq}{n}}$

Z统计量只有当样本数足够大（np/nq大于5，即当p为0.1时，n应当大于50）时才有效；因此当小样本时可以考虑使用exact binomial test

• binom.test(): compute exact binomial test. Recommended when sample size is small
• prop.test(): can be used when sample size is large ( N > 30). It uses a normal approximation to binomial

n <- 160
p0 <- 0.5
p <- 95 / 160

z <- (p - p0) / sqrt(p0 * (1 - p0) / n)
2 * pnorm(-abs(z))

binom.test(x = 95, n = 160, p = 0.5, alternative = "two.sided")

prop.test(x = 95, n = 160, p = 0.5, correct = F, alternative = "two.sided")

#### Two-Proportions Z-Test

Two-Proportions Z-Test 用于比较两个观测比例是否有差别

Z统计量：

$z = \frac{p_A-p_B}{\sqrt{pq/n_A+pq/n_B}}$

n1 <- 500
p1 <- 400 / 500
n2 <- 500
p2 <- 490 / 500
p <- (p1 * n1 + p2 * n2) / (n1 + n2)

z <- (p1 - p2) / sqrt(p * (1 - p) * (1 / n1 + 1 / n2))
2 * pnorm(-abs(z))

prop.test(x = c(400, 490), n = c(500, 500), correct = F, alternative = "two.sided")

fisher.test(matrix(c(490, 10, 400, 100), byrow = T, nrow = 2))

#### Chi-square Goodness of Fit Test

Chi-square Goodness of Fit Test就我而言用的比较少，其主要用于比较多个观测比例与预期（理论）比例是否存在差异

chisq.test(c(81, 50, 27), p = c(1/2, 1/3, 1/6))

#### Chi-Square Test of Independence

Chi-Square Test of Independence常用于分析列联表（contengency table），在临床上尤为常见，一般叫做卡方分析，用来判断两个分类变量之间是否有显著相关性（比如某基因突变是否与癌症是否相关等等），可用于计算OR值

Chi-square test should only be applied when the expected frequency of any cell is at least 5.

gplots包的balloonplot()函数以气泡矩阵图形式展示数据，每个点代表数值的大小

library(gplots)

balloonplot(t(dt), main ="housetasks", xlab ="", ylab="",
label = FALSE, show.margins = FALSE)

garphics包的mosaicplot()函数以堆砌直方图的形式展示数据

library(graphics)
mosaicplot(dt, shade = TRUE, las=2,
main = "housetasks")

vcd包的assoc()函数以马赛克图的形式展示数据

library(vcd)
# plot just a subset of the table
assoc(head(dt, 5), shade = TRUE, las=3)

#### 参考资料

http://www.sthda.com/english/wiki/comparing-proportions-in-r