优化应用函数输出

发布于 2024-10-08 20:19:21 字数 631 浏览 0 评论 0原文

我有以下功能:

library (reshape)
phenotype <- rnorm (100)
data <- matrix(rnorm(1000), nrow = 10, ncol=100)

spearman.p <-
             reshape(
                     melt(
                          apply(data, 1, function(y){
                                        cor.test(y,phenotype,method="spearman")
                                                    }[c("p.value", "estimate")]
                               )
                          ), timevar="L2", idvar="L1", direction="wide"
                       )

我想知道是否有一种更有效的方法可以从“apply”ed cor.test 中获取 p.value 和估计

有人可以提供一些建议吗?

I have the following function:

library (reshape)
phenotype <- rnorm (100)
data <- matrix(rnorm(1000), nrow = 10, ncol=100)

spearman.p <-
             reshape(
                     melt(
                          apply(data, 1, function(y){
                                        cor.test(y,phenotype,method="spearman")
                                                    }[c("p.value", "estimate")]
                               )
                          ), timevar="L2", idvar="L1", direction="wide"
                       )

that I would like to know if there is a more efficent way of getting out the p.value and estimate from a "apply"ed cor.test

Can anyone provide some suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎夜雨 2024-10-15 20:19:21

这是我目前能想到的最好的办法。

FUN <- function(y) {
  test <- cor.test(y,phenotype,method="spearman")
  out <- unlist(test[c("p.value", "estimate")])
}
t(apply(data, 1, FUN))

This is the best I can come up with at the moment.

FUN <- function(y) {
  test <- cor.test(y,phenotype,method="spearman")
  out <- unlist(test[c("p.value", "estimate")])
}
t(apply(data, 1, FUN))
童话 2024-10-15 20:19:21

这将更加紧凑,并提供来自重复数据的 p.values。这是您想要的吗?:

 dtt <- do.call(rbind, apply(data, 1, function(y){
                            cor.test(y,phenotype,method="spearman")
                                  }[c("p.value", "estimate")]
                                 ) )
  dtt
 ###      p.value   estimate 
     [1,] 0.2305644 0.1208641
     [2,] 0.2305644 0.1208641
     [3,] 0.2305644 0.1208641
     [4,] 0.2305644 0.1208641
     [5,] 0.2305644 0.1208641
     [6,] 0.2305644 0.1208641
     [7,] 0.2305644 0.1208641
     [8,] 0.2305644 0.1208641
     [9,] 0.2305644 0.1208641
    [10,] 0.2305644 0.1208641

编辑:如果您正在寻找速度和/或轻松传输到面向并行的平台的可能性,请将其添加到候选列表中:

 pmtx <- matrix(NA, nrow=nrow(data), ncol=2)
 for( i in 1:nrow(data) ) {
  pmtx[i, 1:2 ] <- unlist(cor.test(data[i, ], 
                                   phenotype, 
                                   method="spearman")[c("p.value", "estimate")] ) }
 pmtx

This would be more compact and delivers the p.values from the duplicated data. Is that what you wanted?:

 dtt <- do.call(rbind, apply(data, 1, function(y){
                            cor.test(y,phenotype,method="spearman")
                                  }[c("p.value", "estimate")]
                                 ) )
  dtt
 ###      p.value   estimate 
     [1,] 0.2305644 0.1208641
     [2,] 0.2305644 0.1208641
     [3,] 0.2305644 0.1208641
     [4,] 0.2305644 0.1208641
     [5,] 0.2305644 0.1208641
     [6,] 0.2305644 0.1208641
     [7,] 0.2305644 0.1208641
     [8,] 0.2305644 0.1208641
     [9,] 0.2305644 0.1208641
    [10,] 0.2305644 0.1208641

Edit: If you are looking for speed and/or the possibility of easily transporting to parallel-oriented platforms then add this to the list of candidates:

 pmtx <- matrix(NA, nrow=nrow(data), ncol=2)
 for( i in 1:nrow(data) ) {
  pmtx[i, 1:2 ] <- unlist(cor.test(data[i, ], 
                                   phenotype, 
                                   method="spearman")[c("p.value", "estimate")] ) }
 pmtx
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文