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

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

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

问题

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

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

希望能提供帮助。

英文:

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.

cor.res&lt;- foreach(i=seq_len(ncol(prac3)) %dopar% {
 d &lt;- prac3[,i]
  correlation&lt;- cor.test(d$x, d$y, method = &quot;spearman&quot;)
  out &lt;- c(correlation$estimate,correlation$p.value)
 }


 cor.res&lt;- foreach(i=seq_len(ncol(prac3)) %dopar% {
  correlation&lt;- cor.test(prac3[,i], prac3, method = &quot;spearman&quot;)
  out &lt;- c(correlation$estimate,correlation$p.value)
     }


 cor.res&lt;- foreach(i=seq_len(ncol(prac3)) %dopar% {
 correlation&lt;- cor.test(prac3[[i[1]]], prac3[[i[2]]], method = &quot;spearman&quot;)
 out &lt;- c(correlation$estimate,correlation$p.value)
 }

Any help would be appreciated.

答案1

得分: 1

以下是代码的翻译部分:

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

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

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

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

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

cor.res<- foreach(i=1:nrow(eg), .combine = rbind) %dopar% {
  broom::tidy(cor.test(mtcars[, eg[i,1]], 
                       mtcars[, eg[i,2]], 
                       method="spearman")) %>%
    mutate(row=eg[i,1], col=eg[i,2])
}

cor.res
#> # A tibble: 21 × 7
#>    estimate statistic p.value method                        alternative row   col  
#>       <dbl>     <dbl>   <dbl> <chr>                         <chr>       <chr> <chr>
#>  1   -0.911    10425. 4.69e-13 Spearman's rank correlation … two.side mpg   cyl  
#>  2   -0.909    10415. 6.37e-13 Spearman's rank correlation … two.side mpg   disp 
#>  3    0.928      395. 2.28e-14 Spearman's rank correlation … two.side cyl   disp 
#>  4   -0.895    10337. 5.09e-12 Spearman's rank correlation … two.side mpg   hp   
#>  5    0.902      536. 1.87e-12 Spearman's rank correlation … two.side cyl   hp   
#>  6    0.851      813. 6.79e-10 Spearman's rank correlation … two.side disp  hp   
#>  7    0.651     1902. 5.38e- 5 Spearman's rank correlation … two.side mpg   drat 
#>  8   -0.679     9160. 1.94e- 5 Spearman's rank correlation … two.side cyl   drat 
#>  9   -0.684     9186. 1.61e- 5 Spearman's rank correlation … two.side disp  drat 
#> 10   -0.520     8294. 2.28e- 3 Spearman's rank correlation … two.side hp    drat 
#> # … 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:

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

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

eg$row &lt;- names(mtcars)[eg$row]
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:

cor.res&lt;- foreach(i=1:nrow(eg), .combine = rbind) %dopar% {
  broom::tidy(cor.test(mtcars[, eg[i,1]], 
                       mtcars[, eg[i,2]], 
                       method=&quot;spearman&quot;)) %&gt;% 
    mutate(row=eg[i,1], col=eg[i,2])
}

cor.res
#&gt; # A tibble: 21 &#215; 7
#&gt;    estimate statistic  p.value method                        alter…&#185; row   col  
#&gt;       &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt; &lt;chr&gt;                         &lt;chr&gt;   &lt;chr&gt; &lt;chr&gt;
#&gt;  1   -0.911    10425. 4.69e-13 Spearman&#39;s rank correlation … two.si… mpg   cyl  
#&gt;  2   -0.909    10415. 6.37e-13 Spearman&#39;s rank correlation … two.si… mpg   disp 
#&gt;  3    0.928      395. 2.28e-14 Spearman&#39;s rank correlation … two.si… cyl   disp 
#&gt;  4   -0.895    10337. 5.09e-12 Spearman&#39;s rank correlation … two.si… mpg   hp   
#&gt;  5    0.902      536. 1.87e-12 Spearman&#39;s rank correlation … two.si… cyl   hp   
#&gt;  6    0.851      813. 6.79e-10 Spearman&#39;s rank correlation … two.si… disp  hp   
#&gt;  7    0.651     1902. 5.38e- 5 Spearman&#39;s rank correlation … two.si… mpg   drat 
#&gt;  8   -0.679     9160. 1.94e- 5 Spearman&#39;s rank correlation … two.si… cyl   drat 
#&gt;  9   -0.684     9186. 1.61e- 5 Spearman&#39;s rank correlation … two.si… disp  drat 
#&gt; 10   -0.520     8294. 2.28e- 3 Spearman&#39;s rank correlation … two.si… hp    drat 
#&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:

确定