You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

分组后变量Bootstrap置信区间求解报错及需求咨询

Solution

First, adjust your approach to use a helper function that computes the median and BCA bootstrap CI for a single vector, then apply this function to each group using summarise (not mutate):

# Load required packages
library(boot)
library(dplyr)

# Define bootstrap statistic function
mediana_boot <- function(x, i) median(x[i])

# Helper function to calculate median and BCA confidence interval
compute_boot_ci <- function(data_vector) {
  # Run bootstrap with 1000 replicates (increase for more reliable results)
  boot_result <- boot(data_vector, mediana_boot, R = 1000)
  # Compute BCA confidence interval
  ci_result <- boot.ci(boot_result, conf = 0.95, type = "bca")
  # Extract values and return as a tibble
  tibble(
    mediana = median(data_vector),
    CI.L = ci_result$bca[4],
    CI.U = ci_result$bca[5]
  )
}

# Apply to grouped data
result <- lr2_analysis_CO_FIL %>%
  filter(BARRIER_POS != 'no_street') %>%
  group_by(BARRIER_POS) %>%
  summarise(compute_boot_ci(R_CO_GC)) %>%
  ungroup()

# View the final result (includes BARRIER_POS, median, and CI bounds)
print(result)

Why Your Original Code Failed

  1. Using mutate instead of summarise:
    mutate tries to add a column to every row in the group, meaning the bootstrap runs once per row (redundant and inefficient). This triggers the recursive promise evaluation error because the code repeatedly references group-level data for each row. summarise is designed to produce one result per group, which matches your goal.

  2. Incorrect CI extraction:
    The boot.ci output is a list structure. Your original code used boot_CO[4] and boot_CO[5], which doesn't correctly target the BCA interval values. You need to access the bca element of the boot.ci result—a matrix where the 4th and 5th entries are the 95% lower and upper bounds.

  3. Unnecessary data.frame() call:
    The final data.frame(mediana, CI.L, CI.U) incorrectly referenced variables outside the summarise environment, contributing to the recursive error. summarise already retains the BARRIER_POS grouping column, so this step was redundant.

内容的提问来源于stack exchange,提问作者Heidel Moronta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 02:37:37