代码之家  ›  专栏  ›  技术社区  ›  Krantz

如何使用bayesboot()计算95%置信区间

  •  0
  • Krantz  · 技术社区  · 6 年前

    我需要帮助计算 bootstrap-based credible intervals 数量的 qtt.ci 从我的回归 coef.def .

    到目前为止,我的努力已经导致:

    分位数错误。默认值(S,C(0.025,0.25,0.5,0.75,0.975)): 如果“na.rm”为false,则不允许缺少值和NaN

    前面:

    警告消息:in bayesboot(dat,boot_fn):示例 bayesboot包含nas、nans或nulls。确保您的 统计函数只返回实际值。

    以下是我的示例数据:

    dat <- data.frame(
      A = c(1, 1, 0, 0), B = c(1, 0, 1, 0),
      Pass = c(278, 100, 153, 79), Fail = c(743, 581, 1232, 1731)
    

    下面是我的回归。我想得到基于引导的95%可信区间的数量是 QT.CI :

    boot_fn <- function(dat) {
               coef.def = unname(coef(glm(cbind(Pass, Fail) ~ A * B, binomial, 
               dat)))
                              }
    qtt.ci <- exp(sum(coef.def[2:4])) - exp(coef.def[2]) - exp(coef.def[3]) + 1
    

    以下是我的尝试:

    bb_ci <- bayesboot(dat, boot_fn)
    summary(bb_ci)
    

    不确定如何获取 QT.CI .

    提前谢谢你。

    编辑:

    根据@ruibarradas的回答,我尝试执行bootstrap以获取qtt.ci数量的95%ci(这是我要获取bootstrapped ci的数量),但没有成功:

    library(bayesboot)
    
    boot_fn <- function(dat) {
          coef.def <- unname(coef(glm(cbind(Pass, Fail) ~ A * B, binomial, dat)))
          qtt<- (exp(sum(coef.def[2:4])) - exp(coef.def[2]) - exp(coef.def[3]) + 1) 
          if(all(!is.na(qtt))) qtt else NULL
        }
    
    Runs <- 1e2
    qtt.ci <- bayesboot(dat, boot_fn, R = Runs, R2 = Runs)
    summary(qtt.ci)
    
    Quantiles:
     statistic    q2.5%     q25%   median     q75%   q97.5%
            V1 2.705878 2.705878 2.705878 2.705878 2.705878
    

    因此,这并不表示 QT.CI . 输出只是 qtt :

    qtt<-(exp(sum(coef.def[2:4])) - exp(coef.def[2]) - exp(coef.def[3]) + 1) 
    qtt
    [1] 2.705878
    

    任何帮助都将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Rui Barradas    6 年前

    以下解决了警告问题。我已经用更少的跑步测试了它,而不是4000只100次。

    library(bayesboot)
    
    boot_fn <- function(dat) {
      fit <- glm(cbind(Pass, Fail) ~ A * B, binomial, dat)
      coef.def <- unname(coef(fit))
      if(all(!is.na(coef.def))) coef.def else NULL
    }
    
    Runs <- 1e2
    bb_ci <- bayesboot(dat, boot_fn, R = Runs, R2 = Runs)
    summary(bb_ci)
    

    编辑。

    根据问题中的公式和op注释中的对话框,要运行基于引导的ci:

    qtt <- exp(sum(bb_ci[2:4])) - exp(bb_ci[2]) - exp(bb_ci[3]) + 1
    
    推荐文章