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

如何用R的report包批量报告列表中的配对t检验结果?

问题:批量使用report包的report_text()生成多组配对t检验报告

我想用R语言的report包中report_text()函数批量报告多组配对t检验的结果,这些检验结果存储在一个列表里。单个检验能正常生成报告,但批量处理时多种方法均失败:

单个检验正常运行

a <- t.test(data$ARG_L1, data$ARG_L2, data = data, paired = T)
report_text(a)

输出:

Effect sizes were labelled following Cohen's (1988) recommendations.
The Paired t-test testing the difference between data$ARG_L1 and data$ARG_L2 (mean of the differences = 6.35) suggests that the effect is positive, statistically significant, and large (difference = 6.35, 95% CI [4.42, 8.27], t(44) = 6.65, p < .001; Cohen's d = 0.99, 95% CI [0.63,1.35])

批量报告失败的代码

# 初始化列表
tests <- list()

# 循环运行检验
for (zz in seq(from = 1, to = 4, by = 2)) {
  
  PairedVar1 <- data[zz+1]     # 取第一个配对变量
  PairednVar1 <- names(PairedVar1)
  data$PairedVar1Unlist <- unlist(PairedVar1)
  
  PairedVar2 <- data[zz+2]     # 取第二个配对变量
  PairednVar2 <- names(PairedVar2)
  data$PairedVar2Unlist <- unlist(PairedVar2)
  
  # 运行t检验
  tests[[zz]] <- t.test(data$PairedVar1Unlist, data$PairedVar2Unlist,
                                         paired = T, data =  data, exact = F)
  
  # 修改检验结果的变量名
  tests[[zz]]$data.name <- str_glue("{PairednVar1} and {PairednVar2}")    
}

# 尝试批量生成报告
report_text(tests)

报错:

Error: Oops, objects of class [list] are not supported (yet) by report_text() :(
Want to help? Check out https://easystats.github.io/report/articles/new_models.html

尝试过的无效方法

report_text(unlist(tests))
report_text(tests[[1]])  # 仅能生成单个报告,非批量
report_text(bind_rows(tests))
tests <- tests %>% discard(is.null)
report_text(tests)

数据

> dput(data)
structure(list(ID = structure(c("PART_1", "PART_2", "PART_3", 
"PART_4", "PART_5", "PART_6", "PART_7", "PART_8", "PART_9", "PART_10", 
"PART_11", "PART_12", "PART_13", "PART_14", "PART_15", "PART_16", 
"PART_17", "PART_18", "PART_19", "PART_20", "PART_21", "PART_22", 
"PART_23", "PART_24", "PART_25", "PART_26", "PART_27", "PART_28", 
"PART_29", "PART_30", "PART_31", "PART_32", "PART_33", "PART_34", 
"PART_35", "PART_36", "PART_37", "PART_38", "PART_39", "PART_40", 
"PART_41", "PART_42", "PART_43", "PART_44", "PART_45", "PART_46", 
"PART_47", "PART_48", "PART_49", "PART_50", "PART_51", "PART_52", 
"PART_53", "PART_54", "PART_55", "PART_56", "PART_57", "PART_58", 
"PART_59", "PART_60", "PART_61", "PART_62", "PART_63", "PART_64", 
"PART_65", "PART_66", "PART_67", "PART_68", "PART_69", "PART_70", 
"PART_71"), class = c("glue", "character")), ARG_L1 = c(70.18, 
67.65, 71.89, 70.42, NaN, 72.38, 69.67, 75.63, 76.7, 76.21, 66.5, 
70.57, 76.72, 66.4, 74.75, 79.17, 70.84, NA, 67.82, 70, 71.88, 
74.55, 69.33, 69.5, 65.25, 75.05, 75.44, 64.56, 74.88, 74.29, 
72.4, 71.93, NA, 69.12, 71.43, 77.53, NA, 71.93, 70.4, 60.25, 
NA, NA, 64.8, 69, NA, 71.19, 71.12, 75.04, 68.89, 68.26, 75.81, 
NA, NA, NA, 75.89, 68.82, 77.35, 68.38, 76.71, 79.12, 78.89, 
73.5, NA, 69.7, 69.82, 70.91, NaN, 72, 71.17, 71.85, 69.7), ARG_L2 = c(65.7, 
65.8, 74.45, 68, NA, NA, 53.75, 73.94, 67.24, 58.22, NA, NaN, 
71.07, 68.07, NaN, 69.88, 71.32, 62.18, 58.65, 76.45, 71.13, 
67.25, NaN, 51.76, 69.33, 68.17, 58, 54.27, 68.05, NaN, NA, 61, 
61.67, NA, 67.79, 65.93, NA, NA, 59.27, 69.67, 71.38, 70, NaN, 
64.88, 68.19, 62.06, 61, 55.48, 65.67, 67.72, 68.47, 64, 65.11, 
66, 67.5, 66.33, NA, 69.61, 69.33, 75.67, 68.17, 63, NA, 58.81, 
NA, NA, NA, 66.5, 62.33, 65, NA), NARR_L1 = c(74.26, NA, NA, 
70.94, NaN, 75, 66.14, 74.48, 77.07, 73.47, 76, 60.44, 73.92, 
77.19, 71.4, 77.59, 72, NA, 70.38, 65.47, 70.54, NA, 68.09, 64.61, 
66.5, 72.52, 62.59, 69.25, 71.48, 71.88, 74.4, 70.1, NA, 70, 
69.6, 78.04, 62.3, 68.79, 73.44, 72.25, NA, NA, 67, 68.25, NA, 
NA, 65.94, 75.71, 72.43, 69.68, 76, 68.6, 65.65, NA, 70.43, 74, 
71.76, 71.17, 74.63, 74.22, NA, 69.47, NA, 68.72, 67, 62.82, 
NaN, 77.33, 69.76, 75.42, 67.62), NARR_L2 = c(65.08, 61, NA, 
71.18, 68.46, NA, 62.75, 66.32, 73.42, 59.83, NA, 51.8, 64.77, 
67.88, NaN, 72.27, 64.25, NaN, 62.6, 54.75, 64.74, NA, NaN, 51.58, 
67.05, 62.38, 64.57, NA, 65.56, NaN, NA, 70.71, NA, NA, 68.1, 
NA, 58.43, NA, 55, 65.29, NA, 58.86, NaN, 64.18, NA, 70.33, 58.5, 
64.84, 65.19, 63.14, 59.12, NaN, 62.75, NA, NaN, 68.82, 65.04, 
66.78, 64.86, 69.06, 69.94, 59.31, 65.15, 55.83, 67.71, NA, NA, 
69, 58.83, 60.65, NA), PairedVar1Unlist = c(74.26, NA, NA, 70.94, 
NaN, 75, 66.14, 74.48, 77.07, 73.47, 76, 60.44, 73.92, 77.19, 
71.4, 77.59, 72, NA, 70.38, 65.47, 70.54, NA, 68.09, 64.61, 66.5, 
72.52, 62.59, 69.25, 71.48, 71.88, 74.4, 70.1, NA, 70, 69.6, 
78.04, 62.3, 68.79, 73.44, 72.25, NA, NA, 67, 68.25, NA, NA, 
65.94, 75.71, 72.43, 69.68, 76, 68.6, 65.65, NA, 70.43, 74, 71.76, 
71.17, 74.63, 74.22, NA, 69.47, NA, 68.72, 67, 62.82, NaN, 77.33, 
69.76, 75.42, 67.62), PairedVar2Unlist = c(65.08, 61, NA, 71.18, 
68.46, NA, 62.75, 66.32, 73.42, 59.83, NA, 51.8, 64.77, 67.88, 
NaN, 72.27, 64.25, NaN, 62.6, 54.75, 64.74, NA, NaN, 51.58, 67.05, 
62.38, 64.57, NA, 65.56, NaN, NA, 70.71, NA, NA, 68.1, NA, 58.43, 
NA, 55, 65.29, NA, 58.86, NaN, 64.18, NA, 70.33, 58.5, 64.84, 
65.19, 63.14, 59.12, NaN, 62.75, NA, NaN, 68.82, 65.04, 66.78, 
64.86, 69.06, 69.94, 59.31, 65.15, 55.83, 67.71, NA, NA, 69, 
58.83, 60.65, NA)), row.names = c(NA, -71L), class = "data.frame")

解决方案

report_text()目前不直接支持列表输入,需要对列表中的每个检验结果单独调用函数,再整合输出。

方法1:基础循环处理

先清理原列表中的空元素,再循环生成每个检验的报告:

# 清理列表,移除空元素
tests_clean <- Filter(Negate(is.null), tests)

# 初始化报告存储列表
reports <- list()

# 循环生成报告
for (i in seq_along(tests_clean)) {
  reports[[i]] <- report_text(tests_clean[[i]])
}

# 合并并输出所有报告
cat(unlist(reports), sep = "\n\n")

方法2:用purrr包批量处理

如果使用tidyverse生态,purrr::map可以更简洁地完成批量操作:

library(purrr)

# 清理列表后批量生成报告
reports <- map(tests_clean, report_text)

# 逐个打印报告
walk(reports, print)

# 或者合并为单个文本输出
cat(flatten_chr(reports), sep = "\n\n")

优化原循环的建议

原循环使用zz作为列表索引会导致列表出现空元素,建议直接用递增索引管理配对变量:

tests <- list()
# 定义配对变量的列索引:ARG_L1/ARG_L2、NARR_L1/NARR_L2
pair_indices <- list(c(2, 3), c(4, 5))

for (i in seq_along(pair_indices)) {
  idx1 <- pair_indices[[i]][1]
  idx2 <- pair_indices[[i]][2]
  
  var1 <- data[[idx1]]
  var2 <- data[[idx2]]
  var_names <- c(names(data)[idx1], names(data)[idx2])
  
  # 运行配对t检验
  test_result <- t.test(var1, var2, paired = TRUE, exact = FALSE)
  # 设置变量名
  test_result$data.name <- paste(var_names, collapse = " and ")
  
  tests[[i]] <- test_result
}

# 批量生成报告
reports <- map(tests, report_text)
cat(unlist(reports), sep = "\n\n")

内容的提问来源于stack exchange,提问作者Larissa Cury

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 12:55:16