Bootstrap Confidence Intervals通过boot.ci函数获得多个统计信息 [英] Bootstrap Confidence Intervals for more than one statistics through boot.ci function

查看:538
本文介绍了Bootstrap Confidence Intervals通过boot.ci函数获得多个统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过boot.ci函数获取多个统计信息的引导置信区间.这是我的MWE.

I want to get bootstrap confidence intervals for more than one statistics through boot.ci function. Here is my MWE.

我在out中有两个统计信息,并希望找到这两个统计信息的引导置信区间.但是,boot.ci函数仅为第一个统计信息(t1 *)提供引导自置时间间隔,而没有为第二个统计信息(t2 *)提供引导自信心间隔.

I've two statistics in out and want to find the bootstrap confidence intervals for these two statistics. However, boot.ci function is providing the bootstrap confidence intervals for only first statistic (t1*) but not for the second statistic (t2*).

set.seed(12345)
df <- rnorm(n=10, mean = 0, sd = 1)


Boot.fun <- 
  function(data, idx) {
    data1 <- sample(data[idx], replace=TRUE)
    m1 <- mean(data1)
    sd1 <- sd(data1)
    out <- cbind(m1, sd1)
    return(out)
  }

Boot.fun(data = df)

library(boot)
boot.out <- boot(df, Boot.fun, R = 20)
boot.out

RDINARY NONPARAMETRIC BOOTSTRAP


Call:
  boot(data = df, statistic = Boot.fun, R = 20)


Bootstrap Statistics :
  original     bias    std. error
t1* -0.4815861  0.3190424   0.2309631
t2*  0.9189246 -0.1998455   0.2499412

boot.ci(boot.out=boot.out, conf = 0.95, type = c("norm", "basic", "perc", "bca"))

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 20 bootstrap replicates

CALL : 
  boot.ci(boot.out = boot.out, conf = 0.95, type = c("norm", "basic", 
                                                     "perc", "bca"))

Intervals : 
  Level      Normal              Basic         
95%   (-1.2533, -0.3479 )   (-1.1547, -0.4790 )  

Level     Percentile            BCa          
95%   (-0.4842,  0.1916 )   (-0.4842, -0.4629 )  
Calculations and Intervals on Original Scale
Warning : Basic Intervals used Extreme Quantiles
Some basic intervals may be unstable
Warning : Percentile Intervals used Extreme Quantiles
Some percentile intervals may be unstable
Warning : BCa Intervals used Extreme Quantiles
Some BCa intervals may be unstable
Warning messages:
  1: In norm.inter(t, (1 + c(conf, -conf))/2) :
  extreme order statistics used as endpoints
2: In norm.inter(t, alpha) : extreme order statistics used as endpoints
3: In norm.inter(t, adj.alpha) :
  extreme order statistics used as endpoints

推荐答案

boot程序包(IMO)有点笨拙,无法正常使用.简短的答案是您需要将index(默认值为1)指定为boot.ci,例如boot.ci(boot.out,index=2).长答案是,立即获取所有引导程序统计信息的引导程序CI当然很方便!

The boot package is (IMO) a little clunky for regular use. The short answer is that you need to specify index (default value is 1) to boot.ci, e.g. boot.ci(boot.out,index=2). The long answer is that it would certainly be convenient to get the bootstrap CIs for all of the bootstrap statistics at once!

获取指定结果位的所有配置项:

Get all CI for a specified result slot:

getCI <- function(x,w) {
   b1 <- boot.ci(x,index=w)
   ## extract info for all CI types
   tab <- t(sapply(b1[-(1:3)],function(x) tail(c(x),2)))
   ## combine with metadata: CI method, index
   tab <- cbind(w,rownames(tab),as.data.frame(tab))
   colnames(tab) <- c("index","method","lwr","upr")
   tab
}
## do it for both parameters
do.call(rbind,lapply(1:2,getCI,x=boot.out))

结果(也许不是您想要的,但易于重塑):

Results (maybe not what you want, but easy to reshape):

         index  method        lwr        upr
normal       1  normal -1.2533079 -0.3479490
basic        1   basic -1.1547310 -0.4789996
percent      1 percent -0.4841726  0.1915588
bca          1     bca -0.4841726 -0.4628899
normal1      2  normal  0.6288945  1.6086459
basic1       2   basic  0.5727462  1.4789105
percent1     2 percent  0.3589388  1.2651031
bca1         2     bca  0.6819394  1.2651031

或者,如果您一次只能使用一种引导程序,那么我在Github上的broom软件包版本具有此功能(我已经提交了pull请求)

Alternatively, if you can live with getting one bootstrap method at a time, my version of the broom package on Github has this capability (I've submitted a pull request)

## devtools::install_github("bbolker/broom")
library(broom)
tidy(boot.out,conf.int=TRUE,conf.method="perc")

这篇关于Bootstrap Confidence Intervals通过boot.ci函数获得多个统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆