如何在 R 中使用 glm 使循环包含混杂因素(协变量)?

How to make loop to contain confounders(covariates) in a binary logistic regression using glm in R?

曝光变量50多个,数据总量16000条。
我需要分析所有暴露变量和结果之间的关联。

我想重复以下公式。

example.data <- data.frame(outcome = c(0,0,0,1,0,1,0,0,1,0),
                           exposure_1 = c(2.03, 2.13, 0.15, -0.14, 0.32,2.03, 2.13, 0.15, -0.14, 0.32),
                           exposure_2 = c(-0.11, 0.93, -1.26, -0.95, 0.24,-0.11, 0.93, -1.26, -0.95, 0.24),
                           age = c(20, 25, 30, 35, 40, 50, 55, 60, 65, 70),
                           bmi = c(20, 23, 21, 20, 25, 18, 20, 25, 26, 27))

logit_1 <- glm(outcome ~exposure_1, family = binomial, data = example.data)
logit_2 <- glm(outcome~ exposure_2 + age+ bmi, family = binomial, data = example.data)

我做了一个这样的公式。

Model1 <- function(x) {
  temp <- glm(reformulate(x,response="outcome"), data=example.data, family=binomial)
  c(exp(summary(temp)$coefficients[2,1]), # OR
    exp(confint(temp)[2,1]), # CI 2.5 
    exp(confint(temp)[2,2]), # CI 97.5
    summary(temp)$coefficients[2,4], # P-value
    colAUC(predict(temp, type = "response"),example.data$outcome)) #AUC
Model1.data <- as.data.frame(t(sapply(setdiff(names(example.data),"outcome"), Model1)))
}

Model2 <- function(x) {
  temp <- glm(reformulate(x + age + bmi, response="outcome"), data=example.data, family=binomial)
  c(exp(summary(temp)$coefficients[2,1]), # OR
    exp(confint(temp)[2,1]), # CI 2.5 
    exp(confint(temp)[2,2]), # CI 97.5
    summary(temp)$coefficients[2,4], # P-value
    colAUC(predict(temp, type = "response"),example.data$outcome)) #AUC
}
Model2.data <- as.data.frame(t(sapply(setdiff(names(example.data),"outcome"), Model2)))

但是,功能“Model2”不起作用。
自己做的代码只运行单二元logistic回归,不能加入多个混杂因素进行分析

reformulate 中使用 c() 而不是 +。在您的两个函数中, x 都采用列名的值。我将使用 mtcars 列名称来说明:

## Model1 works
reformulate("hp", response = "mpg")
# mpg ~ hp

## Model2 doesn't work
reformulate("hp" + wt + cyl, response = "mpg")
# Error in reformulate("hp" + wt + cyl, response = "mpg") : 
#   object 'wt' not found

## Fix it with `c()` and quoted column names
reformulate(c("hp", "wt", "cyl"), response = "mpg")
# mpg ~ hp + wt + cyl

## Showing it works with a mix of variables and quoted column names
x = "hp"
reformulate(c(x, "wt", "cyl"), response = "mpg")
# mpg ~ hp + wt + cyl

所以在你的 Model2 中,更改为 reformulate(c(x, "age", "bmi"), response="outcome")

您还有其他问题 - 除了 outcome (setdiff(names(example.data),"outcome")),您在所有列上都是 运行 Model2,但您还应该排除 bmiage 因为它们包含在函数中。

因此,如果我没理解错的话,您需要一个模型 outcome ~ exposure 和一个模型 outcome ~ exposure + age + bmi 用于 50 个曝光变量中的每一个...

这是使用函数式编程实现此目的的一种方法,假设所有曝光变量都命名为 exposure_1exposure_2 等。:

library(tidyverse)

# formulas for exposure only
lexpo1 <-
  str_subset(string = names(example.data), pattern = "^exposure") %>%
  as.list() %>%
  modify(., ~ reformulate(termlabels = ., response = "outcome"))

# formulas for exposure + age + bmi
lexpo2 <-
  lexpo1 %>%
  modify(~ modelr::add_predictors(.x, ~age, ~bmi))

# put together
list_mods <-
  c(lexpo1, lexpo2)

# fit the models
list_res <-
  list_mods %>%
  map(., ~ glm(., family = binomial, data = example.data))