英文:
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<- 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)
}
cor.res<- foreach(i=seq_len(ncol(prac3)) %dopar% {
correlation<- cor.test(prac3[,i], prac3, method = "spearman")
out <- c(correlation$estimate,correlation$p.value)
}
cor.res<- foreach(i=seq_len(ncol(prac3)) %dopar% {
correlation<- cor.test(prac3[[i[1]]], prac3[[i[2]]], method = "spearman")
out <- 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 <- mtcars[,1:7]
eg <- expand.grid(row = 1:ncol(mtcars), col = 1:ncol(mtcars))
eg <- eg %>% filter(row < col)
Next, you can change the numbers to variable names in the following way:
eg$row <- names(mtcars)[eg$row]
eg$col <- 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<- 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 alter…¹ row col
#> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
#> 1 -0.911 10425. 4.69e-13 Spearman's rank correlation … two.si… mpg cyl
#> 2 -0.909 10415. 6.37e-13 Spearman's rank correlation … two.si… mpg disp
#> 3 0.928 395. 2.28e-14 Spearman's rank correlation … two.si… cyl disp
#> 4 -0.895 10337. 5.09e-12 Spearman's rank correlation … two.si… mpg hp
#> 5 0.902 536. 1.87e-12 Spearman's rank correlation … two.si… cyl hp
#> 6 0.851 813. 6.79e-10 Spearman's rank correlation … two.si… disp hp
#> 7 0.651 1902. 5.38e- 5 Spearman's rank correlation … two.si… mpg drat
#> 8 -0.679 9160. 1.94e- 5 Spearman's rank correlation … two.si… cyl drat
#> 9 -0.684 9186. 1.61e- 5 Spearman's rank correlation … two.si… disp drat
#> 10 -0.520 8294. 2.28e- 3 Spearman's rank correlation … two.si… hp drat
#> # … with 11 more rows, and abbreviated variable name ¹alternative
<sup>Created on 2023-02-23 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论