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

使用marginaleffects绘图函数处理多重插补数据时遇错误

问题:marginaleffects绘图函数处理多重插补模型时出错,数值函数正常

使用marginaleffects包的plot_predictions()和plot_comparisons()函数处理基于多重插补数据的线性模型时,出现两类不同的错误,但同包的avg_predictions()和avg_comparisons()生成数值结果完全正常。

可复现代码

完整数据测试(正常运行)

# 加载包与设置种子
library(marginaleffects)
library(ggplot2)
library(mice)
set.seed(1024)

# 完整数据模型
dat <- iris
mod <- lm(Petal.Width ~ Sepal.Length * Sepal.Width + Species, data = dat)

# 数值预测(正常)
avg_predictions(
  mod,
  variables = "Species",
  newdata = "balanced"
)

# 绘图(正常)
plot_predictions(
  mod,
  condition = "Species",
  newdata = "balanced"
)

# 数值对比(正常)
avg_comparisons(
  mod,
  variables = "Sepal.Length",
  by = "Species",
  newdata = "balanced"
)

# 绘图(正常)
plot_comparisons(
  mod,
  variables = "Sepal.Length",
  by = "Species",
  newdata = "balanced"
)

多重插补数据测试(绘图函数出错)

# 创建含缺失值的数据
dat_imp <- iris
dat_imp$Sepal.Length[sample(seq_len(nrow(iris)), 40)] <- NA
dat_imp$Sepal.Width[sample(seq_len(nrow(iris)), 40)] <- NA
dat_imp$Species[sample(seq_len(nrow(iris)), 40)] <- NA

# 多重插补与模型拟合
dat_mice <- mice(dat_imp, m = 20, printFlag = FALSE, .Random.seed = 1024)
mod_mice <- with(dat_mice, lm(Petal.Width ~ Sepal.Length * Sepal.Width + Species))

# 数值预测(正常)
avg_predictions(
  mod_mice,
  variables = "Species",
  newdata = "balanced"
)

# 绘图函数出错
plot_predictions(
  mod_mice,
  condition = "Species",
  newdata = "balanced"
)

错误信息:

Error: Entries in the `condition` argument must be element of: 
# 数值对比(正常)
avg_comparisons(
  mod_mice,
  variables = "Sepal.Length",
  by = "Species",
  newdata = "balanced"
)

# 绘图函数出错
plot_comparisons(
  mod_mice,
  variables = "Sepal.Length",
  by = "Species",
  newdata = "balanced"
)

错误信息:

Error in subset.default(modeldata, select = cl) : 
  argument "subset" is missing, with no default

尝试添加subset参数后的新错误:

plot_comparisons(
  mod_mice,
  variables = "Sepal.Length",
  by = "Species",
  newdata = subset(dat_imp, Species == "setosa"),
)

错误信息:

Error in 1 - conf_level : non-numeric argument to binary operator

会话信息

> sessionInfo()
R version 4.4.3 (2025-02-28 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

other attached packages:
[1] mice_3.18.0            ggplot2_3.5.2          marginaleffects_0.28.0

解决思路

  1. 手动生成绘图数据(推荐)
    既然avg_predictions()和avg_comparisons()能正常输出数值结果,可直接用这些结果结合ggplot2手动绘图,绕开plot_*函数的兼容性问题:

    • 预测图示例:
      pred_df <- avg_predictions(
        mod_mice,
        variables = "Species",
        newdata = "balanced"
      )
      ggplot(pred_df, aes(x = Species, y = Estimate)) +
        geom_col(fill = "steelblue") +
        geom_errorbar(aes(ymin = `2.5 %`, ymax = `97.5 %`), width = 0.2) +
        labs(title = "Predicted Petal.Width by Species", y = "Estimate") +
        theme_minimal()
      
    • 对比图示例:
      comp_df <- avg_comparisons(
        mod_mice,
        variables = "Sepal.Length",
        by = "Species",
        newdata = "balanced"
      )
      ggplot(comp_df, aes(x = Species, y = Estimate)) +
        geom_point(size = 3) +
        geom_errorbar(aes(ymin = `2.5 %`, ymax = `97.5 %`), width = 0.2) +
        geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
        labs(title = "Comparison of Sepal.Length Effect by Species", y = "Estimate") +
        theme_minimal()
      
  2. 更新marginaleffects包
    当前使用的marginaleffects_0.28.0可能存在针对mice插补模型的绘图函数兼容性bug,尝试更新到最新版本:

    install.packages("marginaleffects")
    
  3. 手动构建newdata
    避免使用newdata = "balanced"字符串参数,手动创建平衡数据框传给newdata,确保函数能正确解析变量:

    # 手动创建平衡的Species数据
    newdat <- expand.grid(
      Species = unique(dat_imp$Species[!is.na(dat_imp$Species)]),
      Sepal.Length = mean(dat_imp$Sepal.Length, na.rm = TRUE),
      Sepal.Width = mean(dat_imp$Sepal.Width, na.rm = TRUE)
    )
    # 尝试绘图
    plot_predictions(mod_mice, condition = "Species", newdata = newdat)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:43:16