P值调整(用于一起调整所有测试的R代码)

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

Pvalue adjustment (Rcode to adjust all tests together)

问题

我想知道如何调整所有比较的P值。我想一起对这两列p-value1和p-value2运行FDR(这样我会有6个测试),并将结果放入新列(Adjustment_Pvalues)。

类似于:

p.adjust(c(p-value1,p-value2), method = "fdr")

特征 p-value1 p-value2 调整后的P值
1     0.5       0.01 
2     NA         NA
3     0.2       0.45 
4     0.2       0.02
英文:

I was wondering how I can adjust the P-value for all comparisons together. I want to run FDR for these two columns p-value1 and p-value2 together (So I will have 6 tests) and bring the results into a new column (Adjustment_Pvalues).

Something like:

p.adjust (c(p-value1,p-value2), method = "fdr")

Trait p-value1 p-value2    Adjustment_Pvalues
1     0.5       0.01 
2     NA         NA
3     0.2       0.45 
4     0.2       0.02 

答案1

得分: 1

如果我理解正确(即您希望它们都一起调整),您将需要使用 pivot_longerpivot_wider

require(tidyverse)

df.test.long <- tibble(
  Trait = 1:4, 
  p_value1 = c(0.5, NA, 0.2, 0.2), 
  p_value2 = c(0.01, NA, 0.45, 0.02)
) %>%
  pivot_longer(cols = -Trait, names_to = "pvalue_column", values_to = "pvalue")

df.test.long$adjusted <- p.adjust(df.test.long$pvalue, method = "fdr")

df.test.final <- df.test.long %>%
  pivot_wider(names_from = "pvalue_column", values_from = c(pvalue, adjusted))

输出

A tibble: 4 × 5

Trait pvalue_p_value1 pvalue_p_value2 adjusted_p_value1 adjusted_p_value2

1 1 0.5 0.01 0.5 0.06
2 2 NA NA NA NA
3 3 0.2 0.45 0.3 0.5
4 4 0.2 0.02 0.3 0.06


<details>
<summary>英文:</summary>

If I understand correctly (i.e. you want all of them adjusted together), you&#39;ll want to use `pivot_longer` and `pivot_wider`: 

    require(tidyverse)

    df.test.long &lt;- tibble(
      Trait = 1:4, 
      p_value1 = c(0.5, NA, 0.2, 0.2), 
      p_value2 = c(0.01, NA, 0.45, 0.02)
    ) %&gt;% 
      pivot_longer(cols = -Trait, names_to = &quot;pvalue_column&quot;, values_to = &quot;pvalue&quot;)
    
    df.test.long$adjusted &lt;- p.adjust(df.test.long$pvalue, method = &quot;fdr&quot;)
    
    df.test.final &lt;- df.test.long %&gt;% pivot_wider(names_from = &quot;pvalue_column&quot;, values_from = c(pvalue, adjusted))

    # output
    # A tibble: 4 &#215; 5
      Trait pvalue_p_value1 pvalue_p_value2 adjusted_p_value1 adjusted_p_value2
      &lt;int&gt;           &lt;dbl&gt;           &lt;dbl&gt;             &lt;dbl&gt;             &lt;dbl&gt;
    1     1             0.5            0.01               0.5              0.06
    2     2            NA             NA                 NA               NA   
    3     3             0.2            0.45               0.3              0.5 
    4     4             0.2            0.02               0.3              0.06

</details>



huangapple
  • 本文由 发表于 2023年5月22日 04:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301762.html
匿名

发表评论

匿名网友

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

确定