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

使用forest_model包时,能否同时结合model_list与panel参数?(R统计分析)

解决forest_model合并多模型时自定义panels失效的问题

问题核心

使用model_list传入多个Cox比例风险模型并设置merge_models=TRUE时,自定义的panels参数被忽略,但单个模型调用时该参数能正常生效。

解决方案

1. 显式指定参数名

你当前的调用中panels是作为位置参数传入的,多模型场景下函数参数顺序可能变化,导致panels未被正确识别。需明确指定参数名:

forest_model(model_list = model_list, merge_models = TRUE, panels = panels, exponentiate = TRUE)

2. 适配合并后的数据结构

合并多模型后,生成的数据集会新增model等标识字段,需调整panels中的表达式匹配新结构:

  • 可添加面板展示每个变量所属的模型
  • 确保display引用的字段(如variable、level)在合并数据中存在

调整后的示例panels:

panels <- list(
    list(width = 0.03),
    # 新增模型名称展示面板
    list(width = 0.15, display = ~model, fontface = "bold", heading = "Model"),
    list(width = 0.1, display = ~variable, fontface = "bold", heading = "Variable"),
    list(width = 0.1, display = ~level),
    list(width = 0.05, display = ~n, hjust = 1, heading = "N"),
    list(width = 0.03, item = "vline", hjust = 0.5),
    list(
        width = 0.45, 
        item = "forest",
        hjust = 0.5,
        heading = "Hazard ratio",
        linetype = "dashed",
        line_x = 1  # Cox模型HR参考线应为1,修正原0的错误设置
        ),
    list(width = 0.03, item = "vline", hjust = 0.5),
    list(
        width = 0.12, 
        display = ~ ifelse(reference, "Reference", sprintf("%0.2f (%0.2f - %0.2f)", estimate, conf.low, conf.high)),
        display_na = NA,
        heading="95% CI"
        ),
    list(
        width = 0.05,
        display = ~ ifelse(reference, "", format.pval(p.value, digits = 1, eps = 0.001)),
        display_na = NA, hjust = 1, heading = "p"
        ),
    list(width = 0.03)
  )

3. 先验证合并数据集结构

手动提取合并后的数据,确认字段是否符合预期,再针对性调整panels:

# 获取合并后的模型数据
merged_data <- forestmodel::prepare_models(model_list, merge_models = TRUE, exponentiate = TRUE)
# 查看数据字段结构
str(merged_data)

根据merged_data的字段调整display表达式,确保所有引用的列都存在。

4. 逐步简化排查

如果调整后仍失效,先用极简版panels测试,确认函数是否识别自定义面板:

simple_panels <- list(
  list(display = ~model),
  list(display = ~variable),
  list(item = "forest")
)
forest_model(model_list = model_list, merge_models = TRUE, panels = simple_panels, exponentiate = TRUE)

若极简版生效,再逐步添加原panels中的其他项,定位导致失效的具体部分。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 18:03:10