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

安装新版R后gtsummary表格异常:小于-1的值显示为1.00求解决

问题描述

自从安装最新版本的R后,我的gtsummary表格中所有小于-1的值均显示为1.00。多次重装R与gtsummary后问题仍未解决。

示例代码:

library(tidyverse)
library(gtsummary)
library(haven)
library(mice)
library(googlesheets4)

data <- read_sheet("https://docs.google.com/spreadsheets/d/1yyw-0xseZSLjD4jc8sw7IksN-S0M3vcKWHy4ksMPL4c/edit?usp=sharing")
  
datami <- mice(data,  m = 23, seed=10)

datareg <- with(datami, 
                lm(SUD ~ NUM + MIND + 
                     AGE + SEX  + CRAVE) )

table <- tbl_regression(datareg,
                        estimate_fun = purrr::partial(style_ratio, digits = 2),
                        pvalue_fun = ~style_pvalue(.x, digits = 2),
                        add_estimate_to_reference_rows = TRUE
) %>% modify_header(label="**Predictor**",estimate="**Unstandardized Coefficient**") %>%
  modify_footnote(update = c(p.value, ci, estimate)  ~ "Reference group")%>%
  modify_caption("Table: Multiple Imputation Predicting Variable")
table
解决方案

问题根源是错误使用了style_ratio()函数格式化线性回归的非标准化系数。

style_ratio()是专门为比率类统计量(如优势比OR、风险比HR)设计的,它会自动将负值转换为其绝对值的倒数(例如-2会被转为0.5,-1转为1.00),但线性回归的非标准化系数可以是任意正负数值,完全不适合用这个函数处理。

修正方法非常直接,把estimate_fun替换为专门格式化数值的style_number():

table <- tbl_regression(datareg,
                        estimate_fun = purrr::partial(style_number, digits = 2),  # 替换style_ratio为style_number
                        pvalue_fun = ~style_pvalue(.x, digits = 2),
                        add_estimate_to_reference_rows = TRUE
) %>% modify_header(label="**Predictor**",estimate="**Unstandardized Coefficient**") %>%
  modify_footnote(update = c(p.value, ci, estimate)  ~ "Reference group")%>%
  modify_caption("Table: Multiple Imputation Predicting Variable")
table

替换后就能正确显示所有正负系数,不会再出现负值被错误转换的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 19:56:50