我有一个数据框架:
gender group count total
female A 8 10
female B 23 30
female C 22 25
male A 18 28
male B 23 30
male C 40 70我应该如何处理数据并应用于性别之间的每一组的卡方检验?
发布于 2022-04-21 14:38:04
首先,使用dput功能可以方便地复制数据。
dput(dat)
structure(list(gender = c("female", "female", "female", "male",
"male", "male"), group = c("A", "B", "C", "A", "B", "C"), count = c(8L,
23L, 22L, 18L, 23L, 40L), total = c(10L, 30L, 25L, 28L, 30L,
70L)), class = "data.frame", row.names = c(NA, -6L)). 然后可以按组(或性别)使用by函数,如下所示
by(dat, dat$group, function(x) chisq.test(data.frame(x$count, x$total)))
dat$group: A
Pearson's Chi-squared test with Yates' continuity correction
data: data.frame(x$count, x$total)
X-squared = 0.011266, df = 1, p-value = 0.9155dat$group: B
Pearson's Chi-squared test
data: data.frame(x$count, x$total)
X-squared = 0, df = 1, p-value = 1dat$group: C
Pearson's Chi-squared test with Yates' continuity correction
data: data.frame(x$count, x$total)
X-squared = 1.0981, df = 1, p-value = 0.2947但是,通过查看该方法创建的2*2矩阵,确保其测试完全符合您的要求是值得的:
例如
> by(dat, dat$group, function(x)data.frame(x$count, x$total))[1]
$A
x.count x.total
1 8 10
2 18 28https://stackoverflow.com/questions/71950128
复制相似问题