循环遍历行以在R中运行 meta分析

gudnpqoy  于 2023-05-11  发布在  其他
关注(0)|答案(2)|浏览(175)

我有不同研究[不同暴露]的汇总统计结果[B/se/p]的数据框

创建示例 Dataframe

dat <- data.frame(
  Study = rep(c("Study 1", "Study 2", "Study 3", "Study 4", "Study 5"), each = 2),
  Stage = rep(c("Discovery", "Replication"), 5),
  b = c(0.1, 0.2, 0.15, 0.3, 0.05, 0.1, 0.25, 0.4, 0.08, 0.15),
  se = c(0.05, 0.06, 0.07, 0.08, 0.09, 0.06, 0.07, 0.08, 0.1, 0.12))

打印 Dataframe

print(dat)

我想对每对研究进行 meta分析-因此对于“研究1”或“研究2”,对两组结果进行荟萃分析-一个结果是发现,另一个是复制。
我试过:

# Create an empty list to store the meta-analysis results
meta_results <- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (stage in c("Discovery", "Replication")) {
  # Filter the data for the current stage
  stage_data <- dat[dat$Stage == stage, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result <- meta::metagen(
    estimate = stage_data$b,
    se = stage_data$se,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[stage]] <- meta_result
}

# Print the meta-analysis results
print(meta_results)

但是我得到了错误

Error in meta::metagen(estimate = stage_data$b, se = stage_data$se, data = stage_data) : 
  argument 2 matches multiple formal arguments

因此,我认为我没有正确地指定每个研究的每个阶段都有一组结果。
有人帮忙吗?谢谢!

goqiplq2

goqiplq21#

您还可以使用purrr进行简化,例如

dat |> 
  split(dat$Study) |> 
  purrr::map(~meta::metagen(
    TE = b,
    seTE = se,
    data = .x
  ))
efzxgjgh

efzxgjgh2#

如果检查?meta::metagen,您将看到没有estimatese参数。正确的参数名称是TEseTE

# Create an empty list to store the meta-analysis results
meta_results <- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (stage in c("Discovery", "Replication")) {
  # Filter the data for the current stage
  stage_data <- dat[dat$Stage == stage, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result <- meta::metagen(
    TE = Effect,
    seTE = Std_Error,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[stage]] <- meta_result
}

结果:

$Discovery
Number of studies combined: k = 5

                                      95%-CI    z  p-value
Common effect model  0.1316 [0.0706; 0.1927] 4.23 < 0.0001
Random effects model 0.1321 [0.0661; 0.1981] 3.92 < 0.0001

Quantifying heterogeneity:
 tau^2 = 0.0007 [0.0000; 0.0447]; tau = 0.0260 [0.0000; 0.2113]
 I^2 = 9.5% [0.0%; 81.2%]; H = 1.05 [1.00; 2.30]

Test of heterogeneity:
    Q d.f. p-value
 4.42    4  0.3524

Details on meta-analytical method:
- Inverse variance method
- Restricted maximum-likelihood estimator for tau^2
- Q-Profile method for confidence interval of tau^2 and tau

$Replication
Number of studies combined: k = 5

                                      95%-CI    z  p-value
Common effect model  0.2167 [0.1527; 0.2807] 6.63 < 0.0001
Random effects model 0.2285 [0.1201; 0.3368] 4.13 < 0.0001

Quantifying heterogeneity:
 tau^2 = 0.0092 [0.0000; 0.1134]; tau = 0.0959 [0.0000; 0.3368]
 I^2 = 61.9% [0.0%; 85.7%]; H = 1.62 [1.00; 2.64]

Test of heterogeneity:
     Q d.f. p-value
 10.50    4  0.0327

Details on meta-analytical method:
- Inverse variance method
- Restricted maximum-likelihood estimator for tau^2
- Q-Profile method for confidence interval of tau^2 and tau

Bonus:错误是什么意思?

R对函数参数有部分匹配。这就是为什么可以使用seq(1, by = 1, length = 10),即使seq没有length参数,只有length.out参数。R已将length匹配到length.out。在您的例子中,由于R找不到se参数,它尝试匹配,但找到了该单词的两个可能匹配项,这就是错误的含义。
我建议避免部分匹配。检查此SO问题:Prevent partial argument matching

编辑

改变循环顺序:

# Create an empty list to store the meta-analysis results
meta_results <- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (study in unique(dat$Study)) {
  # Filter the data for the current stage
  stage_data <- dat[dat$Study == study, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result <- meta::metagen(
    TE = Effect,
    seTE = Std_Error,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[study]] <- meta_result
}

# Print the meta-analysis results
print(meta_results)

相关问题