R数据操纵和卡方测试

发布于 2025-01-23 01:06:29 字数 284 浏览 0 评论 0原文

我有一个数据框架:

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

如何处理数据并为性别之间的每个组应用卡方检验?

I have a data frame:

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

How should I process data and apply the chi-square test for each group between genders?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2025-01-30 01:06:29

首先使用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函数,

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.9155

dat$group: B

Pearson's Chi-squared test

data:  data.frame(x$count, x$total)
X-squared = 0, df = 1, p-value = 1

dat$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      28

First its handy to use dput function to make it easy for your data to be reproduced

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)).   

then you can use the by function by group (or gender) like this

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.9155

dat$group: B

Pearson's Chi-squared test

data:  data.frame(x$count, x$total)
X-squared = 0, df = 1, p-value = 1

dat$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

Its worth making sure its testing exactly what you want though by looking at the 2*2 matrix this method creates:

eg

> by(dat, dat$group, function(x)data.frame(x$count, x$total))[1]
$A
  x.count x.total
1       8      10
2      18      28
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文