问题描述
我正在使用多个变量,我想在其中运行稳固的相关性,然后提取95%的置信区间。我可以使用pbcor
包中的WRS2
来做到这一点。
但是,当我想绘制这些值时,我使用ggcorrmat
包中的ggstratsplot
。在检查两个输出之间的置信区间时,我发现它们不匹配。
这是我的数据集的一个示例:
Individual vara varB
1 2.9380842 0.09896456
2 2.9380842 -1.38772037
3 -0.6879859 -2.41310243
4 -0.6879859 0.55722346
5 -2.3129564 -1.34140699
6 -2.3129564 -1.75604301
7 -0.4937431 0.78381085
8 -0.4937431 0.38320385
9 -0.8558126 0.82125672
10 -0.8558126 0.06346062
11 -0.9211026 -1.67170174
WRS2::pbcor(data$vara,data$varB,ci=TRUE,nboot=1000,beta=0.1)
> robust correlation coefficient: 0.275
> test statistic: 0.8582
> p-value:0.41307
> bootstrap CI: [-0.3564; 0.7792]
ggstatsplot::ggcorrmat(data,cor.vars = c(OFT1,PC1),output = "dataframe",matrix.type = "lower",type = "robust",beta = 0.1,sig.level = 0.05,conf.level = 0.95,nboot = 1000)
>robust correlation: 0.275
>test statistic: 0.858
>p-value: 0.413
>CI: [-0.389,0.751]
为什么置信区间不同,但是相关值相同?
解决方法
您是正确的,WRS2
和ggstatsplot
之间的配置项有所不同,因为ggstatsplot
在内部不使用自举(速度较慢且计算成本较高)来计算CI。
Input <- ("
Individual varA varB
1 2.9380842 0.09896456
2 2.9380842 -1.38772037
3 -0.6879859 -2.41310243
4 -0.6879859 0.55722346
5 -2.3129564 -1.34140699
6 -2.3129564 -1.75604301
7 -0.4937431 0.78381085
8 -0.4937431 0.38320385
9 -0.8558126 0.82125672
10 -0.8558126 0.06346062
11 -0.9211026 -1.67170174
")
# creating a dataframe
df <- read.table(textConnection(Input),header = TRUE)
set.seed(123)
WRS2::pbcor(df$varA,df$varB,ci = TRUE,nboot = 1000,beta = 0.1)
#> Call:
#> WRS2::pbcor(x = df$varA,y = df$varB,beta = 0.1,#> nboot = 1000)
#>
#> Robust correlation coefficient: 0.275
#> Test statistic: 0.8582
#> p-value: 0.41307
#>
#> Bootstrap CI: [-0.4476; 0.8223]
set.seed(123)
ggstatsplot::ggcorrmat(
data = dplyr::select(df,-Individual),type = "robust",output = "dataframe",beta = 0.1
)
#> # A tibble: 1 x 10
#> parameter1 parameter2 r ci_low ci_high t df p method nobs
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <int>
#> 1 varA varB 0.275 -0.389 0.751 0.809 9 0.439 Percentage~ 11
它会尽可能返回效果大小的非中心置信区间。
如果您感到好奇,则用于计算CI的相关代码如下: https://github.com/easystats/correlation/blob/ddd105da55c8b5a81e4ce97b8938f5f00e6e968b/R/cor_to_ci.R#L70-L85