Search code examples
rloopsassociationsglm

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


There are more than 50 exposure variables and a total of 16,000 data.
I need to analyze the association between all exposure variables and outcome.

I would like to repeat the following formula.

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)

I made a formula like this.

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)))

However, function "Model2" is not working.
The code that I made only operates single binary logistic regression, but can not be analyzed by adding multiple confounders.


Solution

  • Use c() not + in reformulate. In both your functions, x takes the value of column names. I'll use mtcars column names to illustrate:

    ## 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
    

    So in your Model2, change to reformulate(c(x, "age", "bmi"), response="outcome")

    You have additional problems - you are running Model2 on all columns except for outcome (setdiff(names(example.data),"outcome")), but you should also exclude bmi and age since they are included inside the function.