使用管道从fisher.test()中提取p值

时间:2018-03-08 18:35:25

标签: r dplyr tidyverse

我们有一个数据集,例如:

set.seed(123)
n <- 50
x <- sample(c(0, 1), replace = TRUE, size = n)
y <- sample(c(1, 2), replace = TRUE, size = n)

任务是创建交叉表,计算Fisher精确检验并提取相应的 p 值。这是我的管道:

library(tidyverse)
library(gmodels)
tibble(x, y) %>%
  table() %>% 
  CrossTable(prop.r = FALSE, prop.c = FALSE, prop.t = FALSE, prop.chisq = FALSE, fisher = TRUE)

给出以下输出:

   Cell Contents
|-------------------------|
|                       N |
|-------------------------|


Total Observations in Table:  50 


             | y 
           x |         1 |         2 | Row Total | 
-------------|-----------|-----------|-----------|
           0 |        15 |        10 |        25 | 
-------------|-----------|-----------|-----------|
           1 |        13 |        12 |        25 | 
-------------|-----------|-----------|-----------|
Column Total |        28 |        22 |        50 | 
-------------|-----------|-----------|-----------|


Fisher's Exact Test for Count Data
------------------------------------------------------------
Sample estimate odds ratio:  1.375572 

Alternative hypothesis: true odds ratio is not equal to 1
p =  0.7761301 
95% confidence interval:  0.3927115 4.916038 

Alternative hypothesis: true odds ratio is less than 1
p =  0.8034681 
95% confidence interval:  0 4.094106 

Alternative hypothesis: true odds ratio is greater than 1
p =  0.388065 
95% confidence interval:  0.4686692 Inf 

Any idea how to extract *p*-value ()

我需要提取第一个 p - 值(即0.7761301)。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

这个怎么样

.$fisher.ts

请注意,您可以为.$fisher.gt$.fisher.tl换出(queryDict[key] = queryDict[key] || []) ,具体取决于您想要的那个。

答案 1 :(得分:2)

你需要的只是

a<-tibble(x, y) %>%
  table() %>% 
  CrossTable(prop.r = FALSE, prop.c = FALSE, prop.t = FALSE, prop.chisq = FALSE, fisher = TRUE)

a$fisher.ts$p.value
相关问题