R语言 如何进行多元逻辑回归并显示每个变量的比值比表?

xxhby3vn  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(126)

各位,我的df就像:

或者你可以复制它:

reg_df <- structure(list(NEWDI = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), age = c(79.93, 75.1, 
59.52, 45.62, 63.25, 69.41, 70.37, 70.57, 72.54, 64.63, 67.41, 
60.29, 71.52, 65.11, 73.52, 57.96, 75.88, 65.35, 73.23, 57.25
), smoke_dummy = c(1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), drink_dummy = c(1L, 0L, 
1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 
1L, 1L), bmi = c(27.9595, 32.7313, 28.682, 26.8898, 30.556, 25.763, 
26.9286, 45.3568, 33.1924, 29.391, 36.0282, 25.7202, 35.2501, 
27.0999, 35.5935, 30.1911, 32.557, 28.0998, 25.2214, 23.4699), 
    edu_dummy = c(0L, 1L, NA, 0L, 0L, 1L, NA, 0L, NA, NA, 0L, 
    0L, NA, NA, 1L, NA, 0L, 0L, NA, NA), class_dummy = c(3, 1, 
    0, 0, 0, 0, 3, 0, 1, 0, 3, 0, 0, 1, 3, 2, 3, 0, 1, 0), sex_dummy = c(1, 
    0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1)), row.names = c(NA, 
20L), class = "data. Frame")

我想这样做:

model <- glm(NEWDI ~ class_dummy + age + smoke_dummy + drink_dummy +
              bmi + edu_dummy  + sex_dummy,
             data = reg_df, family = "binomial")

summary(model)

我有个问题:
我想将每个X变量值设置为0作为回归的参考组,并查看它们的OR(除了class_dummy值为0~3,其他variable_dummy值为0~1.),并且smoke_dummy,drink_dummybmi,edu_dummy和bmi具有NA值,我不确定是否可以将回归设置为忽略NA值。
谢谢你!

6ss1mwsb

6ss1mwsb1#

为了在回归中显示参考组,您需要将_dummy变量更改为因子。我用data.table做了这个。
接下来,拟合模型后,您可以使用gtsummary包中的tbl_regression创建回归表。

library(data.table)
library(gtsummary)

target <- grep(pattern = "_dummy", names(reg_df), value = T)

setDT(reg_df)[,(target) := lapply(.SD, as.factor), .SDcols=target]

model <- glm(NEWDI ~ class_dummy + age + smoke_dummy + drink_dummy +
               bmi + edu_dummy  + sex_dummy,
             data = reg_df, family = "binomial")

tbl_regression(
  model,
  exponentiate = T,
)

相关问题