R 中的多个卡方检验
假设我有以下数据:
ID。 | 药物1. | 药物2. | 药物3. | 药物4. |
---|---|---|---|---|
1. | 1. | 0. | 0. | 0. |
2. | 0. | 0. | 0. | 1. |
3. | 0. | 1. | 0. | 0. |
4. | 0. | 0. | 1. | 0. |
5. | 1. | 0. | 0. | 0. |
其中 ID 是给每个患者的编号,每个药物变量都是二进制变量,其中 1 表示患者对该药物有某种病症,0 表示他/她没有。
为了比较药物之间的病情发生率比例,我想进行卡方检验,例如:药物1与药物2、药物1与药物3、药物1与药物4、药物2与药物3、药物2与药物4等。
我该怎么办这在 R 中的一行代码中? 顺便说一句,是否有必要对多重比较进行校正(例如,Bonferroni)?
Imagine I have the following data:
ID. | Drug1. | Drug2. | Drug3. | Drug4. |
---|---|---|---|---|
1. | 1. | 0. | 0. | 0. |
2. | 0. | 0. | 0. | 1. |
3. | 0. | 1. | 0. | 0. |
4. | 0. | 0. | 1. | 0. |
5. | 1. | 0. | 0. | 0. |
Where ID is the number given to each patient and each Drug variable is a binary variable where 1 indicates that patient had a certain condition on that drug and 0 indicates he/she didn't.
In order to compare the proportion of the rate of condition between drugs, I want to perform chi-sqauare tests like: Drug1 vs Drug2, Drug1 vs Drug3, Drug1 vs Drug4, Drug2 vs Drug3, Drug2 vs Drug4, etc.
How can I do this in R in one line of code?
Btw, is it necessary to implement correction for multiple comparisons (e.g., Bonferroni)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是使用 {dplyr} 的 tidyverse 方法。
我首先生成一些数据来运行实际测试并获得有意义的结果。
然后我们可以使用
mydat
的colnames
和combn
来获取所有药物对。然后我们可以使用rowwise
和mutate
并将chisq.test()
应用于每一行。在这里,我们使用V1
和V2
中的字符串来对mydat
中的变量进行子集化。由于我们位于data.frame
中,如果结果是非原子向量,我们必须将结果包装在list
中。我们可以将chisq_test
与$p.value
进行子集化来获取 p 值。reprex 软件包 (v2.0.1)于 2022 年 4 月 4 日创建
由以下 是我的旧答案,它比较了每种药物中
0
和1
的分布,这不是OP所要求的,正如@KU99在中正确指出的那样的评论。由 reprex 软件包 (v0.3.0) 创建于 2022 年 3 月 29 日
Below is a tidyverse approach using {dplyr}.
I first generate some data to run real tests with meaningful results.
Then we can use the
colnames
ofmydat
withcombn
to get all pairs of drugs. Then we can userowwise
andmutate
and applychisq.test()
to each row. Here we use the strings inV1
andV2
to subset the variables inmydat
. Since we are in adata.frame
we have to wrap the result inlist
if its a non-atomic vector. We can subsetchisq_test
with$p.value
to get the p values.Created on 2022-04-04 by the reprex package (v2.0.1)
Below is my old answer, which compares the distribution of
0
and1
within each drug, which is not what the OP asked for, as @KU99 correctly pointed out in the comments.Created on 2022-03-29 by the reprex package (v0.3.0)