使用tidyr或dplyr方法的Fisher精确检验

huangapple go评论124阅读模式
英文:

fisher exact using tidyr or dplyr approach

问题

我有一个包含两组数据的数据框,分别是肿瘤组和正常组。对于每个位点/行,我想要计算在肿瘤组和正常组之间使用Methyl和UnMethy的Fisher精确度。

我正在寻找如何将数据转换为使用dplyr方法计算每个位点的Fisher精确度。

  1. methyl_dat <- data.frame(loci = c("site1", "site2", "site3", "site4"),
  2. Methy.tumor = c(50, 5, 60, 12),
  3. UnMethy.tumor = c(60, 0, 65, 5),
  4. Methy.Normal = c(13, 5, 22, 3),
  5. UnMethy.Normal = c(86, 0, 35, 3))

这里是Fisher精确度的计算策略
对于位点1

  1. 正常组
  2. 肿瘤组 Methyl UnMethy
  3. Methy 50 13
  4. UnMethy 60 86
英文:

I have data dataframe with two groups, Tumor and Normal. for each site/row i want calculate fischer exact for using Methyl UnMethy between Tumor and Normal

I'm looking for how to transform data to calculate fisher exact for each site using dplyr approach.

  1. methyl_dat &lt;- data.frame(loci = c(&quot;site1&quot;, &quot;site2&quot;, &quot;site3&quot;, &quot;site4&quot;),
  2. Methy.tumor = c(50, 5, 60, 12),
  3. UnMethy.tumor = c(60, 0, 65, 5),
  4. Methy.Normal = c(13, 5, 22, 3),
  5. UnMethy.Normal = c(86, 0, 35, 3) )

Here is Fischer exact strategy
for site 1

  1. Normal
  2. Tumor Methyl UnMethy
  3. Methy 50 13
  4. UnMethy 60 86
  5. </details>
  6. # 答案1
  7. **得分**: 5
  8. apply(methyl_dat[-1], 1, \(x)fisher.test(matrix(x,2)), simplify = F)
  9. <details>
  10. <summary>英文:</summary>
  11. consider doing:
  12. apply(methyl_dat[-1], 1, \(x)fisher.test(matrix(x,2)), simplify = F)
  13. </details>
  14. # 答案2
  15. **得分**: 2
  16. 添加一个`dplyr`解决方案:
  17. ```r
  18. library(dplyr, warn.conflicts = FALSE)
  19. data.frame(loci = c("site1", "site2", "site3", "site4"),
  20. Methy.tumor = c(50, 5, 60, 12),
  21. UnMethy.tumor = c(60, 0, 65, 5),
  22. Methy.Normal = c(13, 5, 22, 3),
  23. UnMethy.Normal = c(86, 0, 35, 3) ) %>%
  24. group_by(loci) %>%
  25. summarise(
  26. p_val = fisher.test(matrix(c_across(everything()), 2))$p.val
  27. )
  28. #> # A tibble: 4 × 2
  29. #> loci p_val
  30. #> <chr> <dbl>
  31. #> 1 site1 0.000000392
  32. #> 2 site2 1
  33. #> 3 site3 0.263
  34. #> 4 site4 0.621

创建于2023-06-01,使用reprex v2.0.2

英文:

Adding a dplyr solution:

  1. library(dplyr, warn.conflicts = FALSE)
  2. data.frame(loci = c(&quot;site1&quot;, &quot;site2&quot;, &quot;site3&quot;, &quot;site4&quot;),
  3. Methy.tumor = c(50, 5, 60, 12),
  4. UnMethy.tumor = c(60, 0, 65, 5),
  5. Methy.Normal = c(13, 5, 22, 3),
  6. UnMethy.Normal = c(86, 0, 35, 3) ) %&gt;%
  7. group_by(loci) %&gt;%
  8. summarise(
  9. p_val = fisher.test(matrix(c_across(everything()), 2))$p.val
  10. )
  11. #&gt; # A tibble: 4 &#215; 2
  12. #&gt; loci p_val
  13. #&gt; &lt;chr&gt; &lt;dbl&gt;
  14. #&gt; 1 site1 0.000000392
  15. #&gt; 2 site2 1
  16. #&gt; 3 site3 0.263
  17. #&gt; 4 site4 0.621

<sup>Created on 2023-06-01 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月2日 09:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定