Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
生成两个变量的相关性并使用Bootstrap计算置信区间_R_Loops_Bootstrap 4_Bootstrap Modal_Confidence Interval - Fatal编程技术网

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

生成两个变量的相关性并使用Bootstrap计算置信区间,r,loops,bootstrap-4,bootstrap-modal,confidence-interval,R,Loops,Bootstrap 4,Bootstrap Modal,Confidence Interval,我正在用R编写循环或函数,但我还没有真正理解如何做到这一点。目前,我需要编写一个循环/函数(不确定哪一个更好)来在同一数据帧内创建几个引导结果 示例数据集如下所示: "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

我正在用R编写循环或函数,但我还没有真正理解如何做到这一点。目前,我需要编写一个循环/函数(不确定哪一个更好)来在同一数据帧内创建几个引导结果

示例数据集如下所示:

"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)
我写了一个函数来寻找斯皮尔曼关联

k=cor(df$A_d,df$E_D,method="spearman")
k
结果是 -0.325407

现在我必须运行bootstrap方法,通过对两个变量中的数据进行洗牌来获得5000次相关值 所以使用下面的代码 结果

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多个变量的数据集,所以每次都很难做到,所以我需要自动化部分来做到这一点。我们可以创建一个向量化函数并使用
outer()

corpij[1,1.0000000.289588955-0.480042672 0.22663483-0.32540701
#> [2,]  0.2895890  1.000000000 -0.006379918 0.53614458 -0.35928788
#> [3,] -0.4800427 -0.006379918  1.000000000 0.01913975 -0.13952023
#> [4,]  0.2266348  0.536144578  0.019139754 1.00000000  0.02395253
#> [5,] -0.3254070 -0.359287879 -0.139520230 0.02395253  1.00000000
#> [6,]  0.7680403 -0.120481928 -0.421074589 0.33734940  0.07185758
#>             [,6]
#> [1,]  0.76804027
#> [2,] -0.12048193
#> [3,] -0.42107459
#> [4,]  0.33734940
#> [5,]  0.07185758
#> [6,]  1.00000000
另一种方法是使用
psych::corr.test()

图书馆(心理学)
校正测试(df1[,-c(1,ncol(df1))],方法=“斯皮尔曼”)$r
数据:


df1这可能是一个复制品。我发现了这些:但它们和你的问题不完全一样,所以我没有标记结束。P.S.第一个链接提醒了我关于
psych
package的事。你完全误解了我的意思question@vka没问题,若你们能澄清,我或其他人可能会帮助你们。我已经清楚地解释过了,请再次通过问题
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