循环遍历R中列表中的唯一元素对 [英] Looping over unique pairs of elements in a list in R
问题描述
假设我有一个名为 bb
的对象列表.我想选择 bb
中的每个唯一对元素,并在
Suppose I have a list of objects called bb
. I want to pick each unique pair of elements in bb
and use them in some kind of function (called convolve
) as shown below:
## Below `bb` elements: `bma1` & `bma2` are used in the function:
con <- convolve(dens1= bb$bma1$dposterior,
dens2= function(x){bb$bma2$dposterior(-x)},
cdf1= bb$bma1$pposterior,
cdf2= function(x){1 - bb$bma2$pposterior(-x)})
con$quantile(c(0.025, 0.975))
问题:
bb
可以有任意数量的元素,但是convolve()
一次只能接受两个元素,我想知道如何循环覆盖bb
列表的元素,以使bb
中的所有唯一对(在此示例中:bma1
&bma2
;bma1
&bma3
;bma2& bma3
)输入到convolve()
?
Question:
bb
can have any number of elements butconvolve()
accepts only two elements at a time, I wonder how to loop over the elements of thebb
list such that all unique pairs inbb
(in this example:bma1
&bma2
;bma1
&bma3
;bma2 & bma3
) are entered intoconvolve()
?
以下是可复制的代码:
library("metafor")
library("bayesmeta")
dat <- escalc(measure="OR", ai=lh.le, bi=lh.re, ci=rh.le,
di=rh.re, data=dat.bourassa1996, add=1/2, to="all")
bma1 <- bayesmeta(dat[dat$sex=="combined",])
bma2 <- bayesmeta(dat[dat$sex=="male",])
bma3 <- bayesmeta(dat[dat$sex=="female",])
bb <- list(bma1 = bma1, bma2 = bma2, bma3 = bma3)
# The function (`source` if needed for full reproducibility)
source('https://raw.githubusercontent.com/rnorouzian/m/master/co.r')
con <- convolve(dens1= bb$bma1$dposterior,
dens2= function(x){bb$bma2$dposterior(-x)},
cdf1= bb$bma1$pposterior,
cdf2= function(x){1 - bb$bma2$pposterior(-x)})
con$quantile(c(0.025, 0.975))
推荐答案
如果 bb
表示必须成对组合的数据帧,则可以使用 gtools :: combinations()
生成成对矩阵,并使用它驱动对 convolve()
的调用.
if bb
represents the data frames that must be combined in pairs, we can use gtools::combinations()
to generate a matrix of pairs and use it to drive calls to convolve()
.
library("metafor")
library("bayesmeta")
dat <- escalc(measure="OR", ai=lh.le, bi=lh.re, ci=rh.le,
di=rh.re, data=dat.bourassa1996, add=1/2, to="all")
bma1 <- bayesmeta(dat[dat$sex=="combined",])
bma2 <- bayesmeta(dat[dat$sex=="male",])
bma3 <- bayesmeta(dat[dat$sex=="female",])
bb <- list(bma1 = bma1, bma2 = bma2, bma3 = bma3)
library(gtools)
comboMatrix <- combinations(length(bb),2) # unique pairs of elements in bb
comboMatrix
此时, comboMatrix
看起来像:
> comboMatrix
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 2 3
我们继续原始帖子中提供的代码,将 convolve()
函数包装在 lapply()
中.我们用表示 comboMatrix
的行尺寸的向量驱动 lapply()
,我们将使用该向量为列表中的成对元素索引到第1列和第2列> bb
进行两组比较.
We continue with code provided in the original post, wrapping the convolve()
function in lapply()
. We drive lapply()
with a vector representing the row dimension of comboMatrix
, which we will use to index into columns 1 and 2 for the pairs of elements from the list bb
for a comparison between two groups.
我们使用提取运算符的 [[
形式,而不是 $
形式,因为我们需要使用变量替换(即 comboMatrix [a,1]
或 comboMatrix [a,2]
)从列表 bb
中提取适当的元素.
We use the [[
form of the extract operator instead of the $
form because we need to use variable substitution (i.e. the result of comboMatrix[a,1]
or comboMatrix[a,2]
) to extract the appropriate elements from the list bb
.
# The function (source if needed for full reproducibility)
source('https://raw.githubusercontent.com/rnorouzian/m/master/co.r')
conList <- lapply(1:nrow(comboMatrix),function(a){
con <- convolve(dens1 = bb[[comboMatrix[a,1]]]$dposterior,
dens2 = function(x){bb[[comboMatrix[a,2]]]$dposterior(-x)},
cdf1 = bb[[comboMatrix[a,1]]]$pposterior,
cdf2 = function(x){1 - bb[[comboMatrix[a,2]]]$pposterior(-x)})
con$quantile(c(0.025, 0.975))
})
此时,与 convolve()
相比, conList
对象包含每对数据帧的置信区间.
At this point the conList
object contains the confidence intervals for each pair of data frames compared with convolve()
.
# name the output items
names(conList) <- unlist(lapply(1:nrow(comboMatrix),function(x){
paste("group",comboMatrix[x,1],"vs",comboMatrix[x,2])
}))
conList
当我们打印对象时,我们会看到三组置信区间:
When we print the object we see three sets of confidence intervals:
> conList
$`group 1 vs 2`
[1] -0.3564418 0.3685752
$`group 1 vs 3`
[1] -0.6560065 0.3069931
$`group 2 vs 3`
[1] -0.7298583 0.3589061
当我们通过运行原始文章中包含的第一个比较的代码来验证结果时,我们会看到置信区间匹配.
When we verify the results by running the code for the first comparison that was included with the original post, we see that the confidence intervals match.
con <- convolve(dens1= bb$bma1$dposterior,
dens2= function(x){bb$bma2$dposterior(-x)},
cdf1= bb$bma1$pposterior,
cdf2= function(x){1 - bb$bma2$pposterior(-x)})
con$quantile(c(0.025, 0.975))
...以及输出:
> con$quantile(c(0.025, 0.975))
[1] -0.3564418 0.3685752
第二次和第三次运行会生成与 lapply()
版本的结果相匹配的置信区间.
The second and third runs generate confidence intervals that match the results from the lapply()
version as well.
# group 1 vs. 3
con <- convolve(dens1= bb$bma1$dposterior,
dens2= function(x){bb$bma3$dposterior(-x)},
cdf1= bb$bma1$pposterior,
cdf2= function(x){1 - bb$bma3$pposterior(-x)})
con$quantile(c(0.025, 0.975))
# group 2 vs. 3
con <- convolve(dens1= bb$bma2$dposterior,
dens2= function(x){bb$bma3$dposterior(-x)},
cdf1= bb$bma2$pposterior,
cdf2= function(x){1 - bb$bma3$pposterior(-x)})
con$quantile(c(0.025, 0.975))
...以及输出:
> # group 1 vs. 3
> con <- convolve(dens1= bb$bma1$dposterior,
+ dens2= function(x){bb$bma3$dposterior(-x)},
+ cdf1= bb$bma1$pposterior,
+ cdf2= function(x){1 - bb$bma3$pposterior(-x)})
>
> con$quantile(c(0.025, 0.975))
[1] -0.6560065 0.3069931
> # group 2 vs. 3
> con <- convolve(dens1= bb$bma2$dposterior,
+ dens2= function(x){bb$bma3$dposterior(-x)},
+ cdf1= bb$bma2$pposterior,
+ cdf2= function(x){1 - bb$bma3$pposterior(-x)})
>
> con$quantile(c(0.025, 0.975))
[1] -0.7298583 0.3589061
这篇关于循环遍历R中列表中的唯一元素对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!