安装新版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
相关产品推荐
相关产品推荐

