如何在R中在foreach中使用cor.test?

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

How do I use cor.test within foreach in R?

问题

我不确定如何更正代码以在foreach中运行cor.test。我尝试过的代码如下(prac3是我正在处理的数据框的名称)。我想要相关的变量在列中。

  1. cor.res <- foreach(i = seq_len(ncol(prac3)) %dopar% {
  2. d <- prac3[, i]
  3. correlation <- cor.test(d$x, d$y, method = "spearman")
  4. out <- c(correlation$estimate, correlation$p.value)
  5. })

希望能提供帮助。

英文:

I'm not sure how to correct the code to run a cor.test within foreach. The code I have tried is below (prac3 is the name of the data frame I am working with). The variables I want to correlate are in columns.

  1. cor.res&lt;- foreach(i=seq_len(ncol(prac3)) %dopar% {
  2. d &lt;- prac3[,i]
  3. correlation&lt;- cor.test(d$x, d$y, method = &quot;spearman&quot;)
  4. out &lt;- c(correlation$estimate,correlation$p.value)
  5. }
  6. cor.res&lt;- foreach(i=seq_len(ncol(prac3)) %dopar% {
  7. correlation&lt;- cor.test(prac3[,i], prac3, method = &quot;spearman&quot;)
  8. out &lt;- c(correlation$estimate,correlation$p.value)
  9. }
  10. cor.res&lt;- foreach(i=seq_len(ncol(prac3)) %dopar% {
  11. correlation&lt;- cor.test(prac3[[i[1]]], prac3[[i[2]]], method = &quot;spearman&quot;)
  12. out &lt;- c(correlation$estimate,correlation$p.value)
  13. }

Any help would be appreciated.

答案1

得分: 1

以下是代码的翻译部分:

这是我将如何执行它。问题在于cor.test()需要两个向量。因此,与其循环遍历单个变量(I),你可能想要获取所有变量的组合。话虽如此,foreach循环只遍历单个索引。我首先要做的是创建一个包含所有组合的数据框:

  1. library(dplyr)
  2. library(foreach)
  3. data(mtcars)
  4. mtcars <- mtcars[,1:7]
  5. eg <- expand.grid(row = 1:ncol(mtcars), col = 1:ncol(mtcars))
  6. eg <- eg %>% filter(row < col)

接下来,你可以通过以下方式将数字更改为变量名称:

  1. eg$row <- names(mtcars)[eg$row]
  2. eg$col <- names(mtcars)[eg$col]

现在,你需要循环遍历eg的所有行,其中eg[i,1]将代表第一个变量名,而eg[i,2]将代表第二个变量名。我们可以使用broom包的tidy()函数对结果进行清理,然后使用rbind将它们组合在一起:

  1. cor.res<- foreach(i=1:nrow(eg), .combine = rbind) %dopar% {
  2. broom::tidy(cor.test(mtcars[, eg[i,1]],
  3. mtcars[, eg[i,2]],
  4. method="spearman")) %>%
  5. mutate(row=eg[i,1], col=eg[i,2])
  6. }
  7. cor.res
  8. #> # A tibble: 21 × 7
  9. #> estimate statistic p.value method alternative row col
  10. #> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
  11. #> 1 -0.911 10425. 4.69e-13 Spearman's rank correlation … two.side mpg cyl
  12. #> 2 -0.909 10415. 6.37e-13 Spearman's rank correlation … two.side mpg disp
  13. #> 3 0.928 395. 2.28e-14 Spearman's rank correlation … two.side cyl disp
  14. #> 4 -0.895 10337. 5.09e-12 Spearman's rank correlation … two.side mpg hp
  15. #> 5 0.902 536. 1.87e-12 Spearman's rank correlation … two.side cyl hp
  16. #> 6 0.851 813. 6.79e-10 Spearman's rank correlation … two.side disp hp
  17. #> 7 0.651 1902. 5.38e- 5 Spearman's rank correlation … two.side mpg drat
  18. #> 8 -0.679 9160. 1.94e- 5 Spearman's rank correlation … two.side cyl drat
  19. #> 9 -0.684 9186. 1.61e- 5 Spearman's rank correlation … two.side disp drat
  20. #> 10 -0.520 8294. 2.28e- 3 Spearman's rank correlation … two.side hp drat
  21. #> # … with 11 more rows, and abbreviated variable name ªalternative

2023-02-23创建,使用reprex v2.0.2

英文:

This is how I would do it. The issue is that cor.test() wants two vectors. So, rather than looping over a single variable (I) you probably want to get all pairs of variables. That said, foreach loops over a single index. What I would do is to first make a data frame that has all the combinations first:

  1. library(dplyr)
  2. library(foreach)
  3. data(mtcars)
  4. mtcars &lt;- mtcars[,1:7]
  5. eg &lt;- expand.grid(row = 1:ncol(mtcars), col = 1:ncol(mtcars))
  6. eg &lt;- eg %&gt;% filter(row &lt; col)

Next, you can change the numbers to variable names in the following way:

  1. eg$row &lt;- names(mtcars)[eg$row]
  2. eg$col &lt;- names(mtcars)[eg$col]

Now, you need to loop over all the rows of eg, where eg[i,1] will stand in for the first variable name and eg[i,2] will stand in for the second variable name. We can use tidy() from the broom package to clean the results up a bit and then combine them with rbind:

  1. cor.res&lt;- foreach(i=1:nrow(eg), .combine = rbind) %dopar% {
  2. broom::tidy(cor.test(mtcars[, eg[i,1]],
  3. mtcars[, eg[i,2]],
  4. method=&quot;spearman&quot;)) %&gt;%
  5. mutate(row=eg[i,1], col=eg[i,2])
  6. }
  7. cor.res
  8. #&gt; # A tibble: 21 &#215; 7
  9. #&gt; estimate statistic p.value method alter…&#185; row col
  10. #&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  11. #&gt; 1 -0.911 10425. 4.69e-13 Spearman&#39;s rank correlation … two.si… mpg cyl
  12. #&gt; 2 -0.909 10415. 6.37e-13 Spearman&#39;s rank correlation … two.si… mpg disp
  13. #&gt; 3 0.928 395. 2.28e-14 Spearman&#39;s rank correlation … two.si… cyl disp
  14. #&gt; 4 -0.895 10337. 5.09e-12 Spearman&#39;s rank correlation … two.si… mpg hp
  15. #&gt; 5 0.902 536. 1.87e-12 Spearman&#39;s rank correlation … two.si… cyl hp
  16. #&gt; 6 0.851 813. 6.79e-10 Spearman&#39;s rank correlation … two.si… disp hp
  17. #&gt; 7 0.651 1902. 5.38e- 5 Spearman&#39;s rank correlation … two.si… mpg drat
  18. #&gt; 8 -0.679 9160. 1.94e- 5 Spearman&#39;s rank correlation … two.si… cyl drat
  19. #&gt; 9 -0.684 9186. 1.61e- 5 Spearman&#39;s rank correlation … two.si… disp drat
  20. #&gt; 10 -0.520 8294. 2.28e- 3 Spearman&#39;s rank correlation … two.si… hp drat
  21. #&gt; # … with 11 more rows, and abbreviated variable name &#185;​alternative

<sup>Created on 2023-02-23 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年2月24日 06:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551168.html
匿名

发表评论

匿名网友

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

确定