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

Extract p-value from fisher.test() using pipeline

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

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

任务是创建交叉table,计算 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)。有什么想法吗?

这个怎么样

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

p
[1] 0.7761301

请注意,您可以根据需要将 .$fisher.ts 换成 .$fisher.gt$.fisher.tl

你只需要

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