繁体   English   中英

是否可以使用 R 中的引导 package 计算二维置信区间?

[英]Is it possible to calculate 2-D confidence intervals with the boot package in R?

我正在为标量参数 e 的各种值制作两个统计数据 X(e) 和 Y(e) 的散点 plot。 X 和 Y 的采样分布都不是正态分布的。

现在我想计算散点图 plot 中每个点 (X(e),Y(e)) 的二维置信区间。 我怎么做?

由于采样分布不是正态分布,我在 R 中使用引导 package。 有了这个,我可以独立计算每个 X(e) 和 Y(e) 的置信区间。 这种方法在统计上是合理的还是我应该从二维采样分布中采样? 在那种情况下,我该怎么做?

这是一种使用基本 package boot来引导置信区间以同时获取两个向量的统计量(均值)的方法。

df1 <- iris[1:50, 1:2]
head(df1)
#>   Sepal.Length Sepal.Width
#> 1          5.1         3.5
#> 2          4.9         3.0
#> 3          4.7         3.2
#> 4          4.6         3.1
#> 5          5.0         3.6
#> 6          5.4         3.9

library(boot)

bootfun <- function(x, i) colMeans(x[i,])

R <- 1000L
set.seed(2022)

b <- boot(df1, bootfun, R)
colMeans(b$t)
#> [1] 5.010754 3.431042
boot.ci(b)
#> BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
#> Based on 1000 bootstrap replicates
#> 
#> CALL : 
#> boot.ci(boot.out = b)
#> 
#> Intervals : 
#> Level      Normal              Basic             Studentized     
#> 95%   ( 4.905,  5.097 )   ( 4.902,  5.092 )   ( 4.904,  5.093 )  
#> 
#> Level     Percentile            BCa          
#> 95%   ( 4.920,  5.110 )   ( 4.914,  5.096 )  
#> Calculations and Intervals on Original Scale

使用reprex v2.0.2创建于 2022-09-15


但是要计算两个不同的自举统计量,就更复杂了, boot一次只能自举一个统计量。
创建一个包含感兴趣的统计信息的列表和一个 function 一个一个引导列表中的统计信息的列表。 此 function 的返回值是 class "boot"的对象列表,并且lapply循环可以计算置信区间。

library(boot)

bootfun2 <- function(data, stat_list, R, ...) {
  stat <- function(x, i, f) {
    y <- x[i]
    f(y)
  }
  lapply(stat_list, \(f) {
    boot(data, stat, R = R, f = f)
  })
}

R <- 1000L
set.seed(2022)

e <- df1[[1]]

flist <- list(X = mean, Y = sd)
blist <- bootfun2(e, flist, R)
ci_list <- lapply(blist, boot.ci)
#> Warning in FUN(X[[i]], ...): bootstrap variances needed for studentized
#> intervals

#> Warning in FUN(X[[i]], ...): bootstrap variances needed for studentized
#> intervals

ci_list[[1]]$percent[4:5]
#> [1] 4.920000 5.109949
ci_list[[2]]$percent[4:5]
#> [1] 0.2801168 0.4103270

ci_list[[1]]$bca[4:5]
#> [1] 4.91400 5.09639
ci_list[[2]]$bca[4:5]
#> [1] 0.2956919 0.4308415

使用reprex v2.0.2创建于 2022-09-15

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM