0

I am trying to model an outcome as a function of several exposures, adjusting the models for any covariates that may be confounders (≥ 10% ∆ to outcome coefficient when added to model). I am looking at many covariates as potential confounders, so have created a dataframe with all of them and am using lapply (the outcome and exposures are in a separate dataframe which has already been attached). To make sorting through all my outputs easier, I have tried to write a function which will only display the output if the covariate is a confounder. The exposures and number of them are different in each model, so I find myself having to write code like bellow each time I run my analyses, but know there must be an easier way. Would there be a function I could write to just lapply with, using the model without confounders and the Covariates dataframe as arguments? Thanks!

lapply(Covariates, function(x) {
  model <- summary(lm(Outcome ~ Exposure1 + Exposure2 + ... + x))
  if ((model$coefficients[2, 1] - summary(lm(Outcome ~ Exposure))$coefficients[2, 1])/
      model$coefficients[2, 1] >= .1)
    return(model)
})
Jklein
  • 101
  • 1
  • 11
  • 2
    Please [provide a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jean Feb 16 '17 at 04:18
  • Try using `colnames` to grab the column names of everything. So long as Outcome is the only other field, you can basically write inside your function `exposures<-colnames(x)` then remove `"Outcome"` from the list and feed exposures into the boolean mask for confounders. Likely you may need to alter your grammar and addressing some, but it should work. – sconfluentus Feb 16 '17 at 04:21

1 Answers1

1

I have written a function to solve this problem!

    confounder <- function(model) {
      model.sum <- summary(model)
      model.b <- model.sum$coefficients[2, 1]
      oldmodel <- update(model, . ~ . -x)
      oldmodel.sum <- summary(oldmodel)
      oldmodel.b <- oldmodel.sum$coefficients[2, 1]
      model.frame <- tidy(model.sum)
      model.sub <- subset(model.frame, term = "x")
      model.sub.b <- model.sub[, 5]
      if ((model.b - oldmodel.b)/model.b >= .1 |
           model.sub.b < .05)
        return(model.sum)
    }

I then lapply this function to the model:

    lapply(Covariates, function(x) {
      confounder(lm(Outcome ~ Exposure1 + Exposure2 + ... + x))
    })
Jklein
  • 101
  • 1
  • 11