生成两个变量的相关性,并使用Bootstrap计算置信区间

问题描述 投票:2回答:1

我正在用R编写循环或函数,但我仍然还不太了解如何做到这一点。当前,我需要编写一个循环/函数(不确定哪个更好),以在同一数据帧内创建多个Bootstrap结果。

样本数据集看起来像:

"ID A_d B_d C_d D_d E_D f_D chkgp
M1  10  20  60  30  54  33  Treatment
M1  20  50  40  33  31  44  Placebo
M2  40  80  40  23  15  66  Placebo
M2  30  90  40  67  67  66  Treatment
M3  30  10  20  22  89  77  Treatment
M3  40  50  30  44  50  88  Placebo
M4  40  30  40  42  34  99  Treatment
M4  30  40  50  33  60  80  Placebo",header = TRUE, stringsAsFactors = FALSE)

我已经编写了一个函数来查找spearman相关性

k=cor(df$A_d,df$E_D,method="spearman")
k

结果是-0.325407

现在我必须运行bootstrap方法以通过将两个变量的数据进行混洗来获得相关值5000次

因此使用的以下代码

fc <- function(d, i){
    d2 <- d[i,]
    return(cor(df$A_d,df$E_D,method="spearman"))
}

在定义了函数fc之后,我们可以使用boot命令,提供我们的数据集名称,我们的函数以及要绘制的引导程序样本数。

计算的BOOTSTRAP置信区间计算基于5000个引导程序副本。

#turn off set.seed() if you want the results to vary
set.seed(626)
bootcorr <- boot(hsb2, fc, R=500)
bootcorr

我从5000个重复中找出置信区间

boot.ci(boot.out = bootcorr, type =c( "perc"))

结果

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 500 bootstrap replicates

CALL : 
boot.ci(boot.out = bootcorr, type = c("perc"))

Intervals : 
Level     Percentile     
95%   (-0.3254, -0.3254 )  
Calculations and Intervals on Original Scale

我需要编写一个循环条件以获取结果,如下所示

Variable1 Variable2 confidence interval
A_d       E_D        (-0.3254, -0.3254 )  
A_d       f_D
B_d       E_D
B_d       f_D
C_d       E_D
C_d       f_D
D_d       E_D
d_d       f_D                              

因为我的数据集包含100多个变量,所以每次都很难做到,所以我需要自动化部分来完成。

r loops bootstrap-4 bootstrap-modal confidence-interval
1个回答
1
投票

我们可以创建向量化函数并使用outer()

© www.soinside.com 2019 - 2024. All rights reserved.