我想让我的代码更优雅。我想针对3个不同标准,在2组中得到费雪精确检验(本质上是3个检验)我有一个解决方案,但很麻烦。我想知道是否有一种方法来编写一个函数来实现相同的…
我的解决方案:
df <- data.frame(group = c("A", "B", "A", "B", "A", "B"),
+ criteria = c("fever", "fever", "headache", "headache", "chills", "chills"),
+ absent = c(35, 31, 78, 163, 53, 33),
+ present = c(62, 154, 19, 22, 44, 152))
现在做Fisher检验来比较组A &;B发烧,然后头痛,然后发冷。
#Compare A & B on fever
fever <- df %>% filter(criteria=="fever") %>% select(-criteria)
fever <- column_to_rownames(fever, var = "group")
fisher.test(fever)
#Compare A & B on headache
headache <- df %>% filter(criteria=="headache") %>% select(-criteria)
headache <- column_to_rownames(headache, var = "group")
fisher.test(headache)
#Compare A & B on chills
chills <- df %>% filter(criteria=="chills") %>% select(-criteria)
chills <- column_to_rownames(chills, var = "group")
fisher.test(chills)
我希望能够打印出费雪在所有不同标准上的检验结果(记住,实际上我有超过3个标准),而不必单独打印出来。我想这是可能的函数,但我真的不知道从哪里开始…
如能提供帮助,我将不胜感激。请放轻松,我是临床医生,不是信息学家!您可以将group_by
criteria
和fisher.test
应用于每个组。
library(dplyr)
df %>%
select(-group) %>%
group_by(criteria) %>%
summarise(fisher_test = list(fisher.test(cur_data()))) -> result
result$fisher_test
#[[1]]
# Fisher's Exact Test for Count Data
#data: cur_data()
#p-value = 0.0000000004
#alternative hypothesis: true odds ratio is not equal to 1
#95 percent confidence interval:
# 3.09 9.97
#sample estimates:
#odds ratio
# 5.51
#[[2]]
# Fisher's Exact Test for Count Data
#data: cur_data()
#p-value = 0.0004
#alternative hypothesis: true odds ratio is not equal to 1
#95 percent confidence interval:
# 1.53 5.14
#sample estimates:
#odds ratio
# 2.79
#[[3]]
# Fisher's Exact Test for Count Data
#data: cur_data()
#p-value = 0.1
#alternative hypothesis: true odds ratio is not equal to 1
#95 percent confidence interval:
# 0.269 1.154
#sample estimates:
#odds ratio
# 0.555
在碱基R中可以使用by
或split
+lapply
:
by(df[3:4], df$criteria, fisher.test)
#OR
lapply(split(df[3:4], df$criteria), fisher.test)