问题描述
我目前正在尝试添加一个具有p值的列,用于评估线性趋势线性回归模型。我还没有在文档中找到解决方案。有没有人找到解决方法?如果是这样,您能和我分享吗?
# install dev versions
remotes::install_github("ddsjoberg/gtsummary@mice_nnet")
remotes::install_github("larmarange/broom.helpers")
# load packages
library(gtsummary)
library(nnet)
theme_gtsummary_compact()
# dummy data
crime <-data.frame(city = sample(as.factor(c(1,2,3,4)),13000,replace = TRUE),sex = sample(c("Male","Female"),year = sample(as.numeric(sample(10:70,replace = TRUE)))
)
# serperate data sets by sex
crime_f <- crime %>%
filter(sex == "Female")
crime_m <- crime %>%
filter(sex == "Male")
# build model for females
mod_f <- lm(year ~ city,data = crime_f,na.action=na.exclude)
# build model for males
mod_m <- lm(year ~ city,data = crime_m,na.action=na.exclude)
# linear trend test between year and city
# females
mod2_f <- lm(year ~ as.numeric(city),na.action=na.exclude)
# males
mod2_m <- lm(year ~ as.numeric(city),na.action=na.exclude)
# make regression table from results
# femlaes
tbl_regression(mod_f,exponentiate = TRUE) %>%
modify_header(estimate ~ "**OR**")
# males
tbl_regression(mod_m,exponentiate = TRUE) %>%
modify_header(estimate ~ "**OR**")
# lm model tabulated with gtsummary
tbl <- tbl_merge(
tbls = list(mod_f,mod_m),tab_spanner = c("**Female**","**Male**")
)
解决方法
最简单的方法是构建两个模型(一个模型,一个模型将变量视为连续模型),然后将表合并在一起。下面的示例。
# load packages
library(gtsummary)
theme_gtsummary_compact()
# model cyl as a categorical
mod1 <- lm(mpg ~ cyl,data = mtcars %>% dplyr::mutate(cyl = factor(cyl)))
# model cyl as continuous (p-trend)
mod2 <- lm(mpg ~ cyl,data = mtcars)
# summarize primary model
tbl1 <- tbl_regression(mod1)
# summarize model with p-trend,and hide the estimate and CI
tbl2 <- tbl_regression(mod2) %>%
modify_table_header(c(estimate,ci),hide = TRUE) %>%
modify_header(p.value ~ "**p-trend**")
# merge primary model and p-trend
tbl_merge(list(tbl1,tbl2)) %>%
# remove spanning header
modify_spanning_header(everything() ~ NA)