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

如何基于插补结果生成规范表格与交互图?求适配R包

针对多重插补数据生成APA格式表格与交互图的R包方案

你遇到的问题核心是sjPlot和interplot不支持mitml包生成的多重插补模型对象。以下是可直接处理这类数据的工具链:

一、生成APA格式表格

1. gtsummary包(推荐,一键生成APA风格)

gtsummary原生支持mitml的合并模型对象,可快速生成符合APA规范的表格:

# 先合并插补模型结果
library(mitml)
pooled_fit <- pool(fit)

# 生成APA格式表格
library(gtsummary)
tbl_regression(pooled_fit,
               intercept = TRUE,
               estimate_fun = ~style_number(.x, digits = 3), # 保留3位小数
               pvalue_fun = ~style_pvalue(.x, digits = 3)) %>% # APA风格p值
  modify_caption("**Multilevel Model Results (Pooled Across 25 Imputed Datasets)**") %>%
  modify_footnote(all_stat_cols() ~ "Note: Coefficients are pooled estimates from multiple imputation.") %>%
  as_gt() %>%
  gt::opt_apa_style() # 应用APA格式样式

2. mitml + apaTables/flextable

如果需要更精细化控制,可先用mitml汇总结果,再用apaTables或flextable格式化:

# 提取合并后的系数与统计量
summary_pooled <- summary(pooled_fit)

# 用apaTables生成APA表格
library(apaTables)
apa_lmer_table(summary_pooled, filename = "apa_model_table.docx")

# 或用flextable手动调整样式
library(flextable)
flextable(summary_pooled$estimates) %>%
  set_caption("Model Results (Multiply Imputed Data)") %>%
  theme_apa()

二、绘制交互图

1. emmeans + ggplot2(高度自定义)

通过emmeans计算边际效应,再用ggplot2绘制符合APA风格的交互图:

library(emmeans)
library(ggplot2)

# 计算交互项的边际预测值(选取总参与次数的分位数作为分组)
emm <- emmeans(pooled_fit, 
               ~ PercGreen * TotalSessionsAttended,
               at = list(TotalSessionsAttended = quantile(impList[[1]]$TotalSessionsAttended, c(0.1, 0.5, 0.9))))

# 转换为数据框并绘图
emm_df <- as.data.frame(emm)

ggplot(emm_df, aes(x = PercGreen, y = emmean, color = factor(TotalSessionsAttended))) +
  geom_line(linewidth = 1) +
  geom_ribbon(aes(ymin = lower.CL, ymax = upper.CL, fill = factor(TotalSessionsAttended)), 
              alpha = 0.2, color = NA) +
  labs(x = "Percentage of Green Space", 
       y = "Predicted RigbyFinal Score",
       color = "Total Sessions Attended",
       fill = "Total Sessions Attended") +
  theme_classic(base_family = "Times New Roman") + # APA指定字体
  scale_color_brewer(palette = "Set1")

2. ggeffects包(快速生成效应图)

ggeffects支持mitml的合并模型,可一键生成交互效应图:

library(ggeffects)

# 生成交互项的预测效应图
ggpredict(pooled_fit, terms = c("PercGreen", "TotalSessionsAttended [quart1, med, quart3]")) %>%
  plot() +
  theme_classic(base_family = "Times New Roman") +
  labs(x = "Percentage of Green Space", y = "Predicted RigbyFinal Score")

关键说明

之前工具失败的原因是sjPlot和interplot未适配mitml的with()输出对象,必须先通过mitml::pool()将多个插补模型的结果合并为统一对象,再用上述支持合并模型的工具处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 07:10:31