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

使用metafor包做亚组Meta分析遇escalc函数‘对象未找到’错误

亚组Meta分析escalc函数报错:'ci_upper'未找到的解决方法

问题场景

执行亚组Meta分析时,使用metafor包的escalc函数构建数据结构,出现如下错误:

Error: object 'ci_upper' not found

已在escalc的data参数中指定了包含ci_upper列的数据框,且m1i参数(调用数据框的mean列)未报错,疑惑该问题的原因。

原代码

## install.packages("metafor")
library(metafor)

# Create a data frame with study results and subgroup information
study_results <- data.frame(
  study = c("Study 1", "Study 2", "Study 3", "Study 4", "Study 5"),
  subgroup = c("A", "A", "B", "B", "B"),
  mean = c(0.45, 0.78, 0.60, 0.85, 0.72),
  ci_lower = c(0.35, 0.70, 0.50, 0.75, 0.62),
  ci_upper = c(0.55, 0.86, 0.70, 0.95, 0.82)
)

# Create a formula specifying the mean and CI
formula <- formula(mean ~ 1 | subgroup)

# Create the data structure for subgroup meta-analysis
dat <- escalc(measure = "MN", m1i = mean, m2i = NA, sd1i = NA, sd2i = NA,
           ci.ub = ci_upper, ci.lb = ci_lower, data = study_results)

# Perform the subgroup meta-analysis
meta_analysis <- rma.mv(formula, data = dat)

# Print the subgroup meta-analysis results
print(meta_analysis)

错误原因

核心问题是escalc函数针对**单组均数(measure="MN")**的CI参数命名错误:

  • 你使用了ci.ub和ci.lb,但这两个参数是针对其他效应量(如标准化均数差SMD)的参数;
  • 对于单组均数分析,正确的CI上下限参数应为ci.u1i(对应均数的95%CI上限)和ci.l1i(对应均数的95%CI下限);
  • 因为参数名错误,函数无法识别你传入的ci_upper是数据框内的列,转而到全局环境查找,因此抛出"object not found"错误。而m1i是单组均数分析的正确参数,所以能正常识别数据框内的mean列。

修正后的代码

## install.packages("metafor")
library(metafor)

# 创建包含研究结果和亚组信息的数据框
study_results <- data.frame(
  study = c("Study 1", "Study 2", "Study 3", "Study 4", "Study 5"),
  subgroup = c("A", "A", "B", "B", "B"),
  mean = c(0.45, 0.78, 0.60, 0.85, 0.72),
  ci_lower = c(0.35, 0.70, 0.50, 0.75, 0.62),
  ci_upper = c(0.55, 0.86, 0.70, 0.95, 0.82)
)

# 构建亚组分析的公式(随机效应模型,按亚组分层)
formula <- formula(yi ~ 1 | subgroup)

# 用正确的CI参数构建Meta分析数据结构
dat <- escalc(measure = "MN", 
              m1i = mean, 
              ci.u1i = ci_upper, 
              ci.l1i = ci_lower, 
              data = study_results)

# 执行亚组多水平Meta分析
meta_analysis <- rma.mv(formula, data = dat)

# 输出结果
print(meta_analysis)

额外说明

  1. 修正了escalc的CI参数为ci.u1i和ci.l1i,匹配measure="MN"的参数要求;
  2. 公式中的因变量改为yi——这是escalc输出数据框中默认的效应量列名,原代码用mean会导致rma.mv无法识别;
  3. 移除了不必要的m2i=NA、sd1i=NA等参数,escalc会自动处理未指定的无关参数。

内容的提问来源于stack exchange,提问作者Spencer K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 01:13:17