簡體   English   中英

執行 Fisher 測試,將多個數據框列與同一向量 R 進行比較

[英]perform Fisher test comparing multiple dataframe columns to the same vector R

我有一個數據框:

frequencies <- data.frame(row.names = c("a", "b", "c")
                          ,response = c(10, 7, 4)
                          ,no_response = c(12, 12, 7))

> frequencies
  response no_response
a       10          12
b        7          12
c        4           7

我想執行費舍爾的精確檢驗,將每一行與該實驗的觀察值總和進行比較(即與整個實驗觀察到的頻率 - 我想知道在任何 a/b/c 數據子集中觀察到的頻率是否是與對整個數據集觀察到的不同)。

要“手動”執行此操作,我會計算每列中有多少觀察結果:

total <- colSums(frequencies) %>% 
  t() %>% 
  as.data.frame() %>% 
  `rownames<-`("total")

> total
      response no_response
total       21          31

然后我運行fisher.test() (我只需要 p 值),將每一列與total[1,]進行比較

ap <- fisher.test(rbind(total[1,], frequencies[1,]))$p.value
bp <- fisher.test(rbind(total[1,], frequencies[2,]))$p.value

等等。

必須有一個更整潔的方法。 在最終輸出中,我想在frequencies數據框中有一列包含 p 值,如下所示:

  response no_response  pval
a       10          12   0.8
b        7          12     1
c        4           7     1

我添加了一個purrr標簽,因為我覺得我應該在這里使用map但我不知道該怎么做。

您可以使用 dplyr 嘗試類似這樣的簡單操作:

library(dplyr)

total <- frequencies %>%
  summarise(across(everything(), sum))

frequencies %>%
  rowwise() %>%
  mutate(pval = stats::fisher.test(rbind(total, c(response, no_response)))$p.value) %>%
  ungroup()

根據:

使用 for 循環 ::

frequencies$p.value<-0
for(i in 1:nrow(frequencies)){
  frequencies$p.value[i]<- fisher.test(rbind(total[1,], frequencies[i,1:2]))$p.value
}
或使用應用::
rbindtest <- function(x) {
  fisher.test(rbind(total[1,], x))$p.value
}
frequencies$p.value<-apply(frequencies, 1, rbindtest)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM