0

I am trying to create a user-defined function that outputs odds ratios (95% CIs) from logistic regression models.

I am basically stuck on Line 2 where I predict the odds ratio using oddsratio::or_glm() function. I need to pass the predictor input into a named list in one of the arguments. The paste() approach does not appear to work...

I wonder if anyone can help?

This is the function:

compute_ors = function(outcome, predictor){
  
  fit.glm = glm(paste(outcome, " ~ ", predictor), data = mydata, family = binomial)
  
  x = oddsratio::or_glm(mydata, model = fit.glm, incr = list(predictor = 0.1), ci = 0.95) 
  
    ## How can I pass the 'predictor' variable as a named list in the 'incr=' argument of the 'or_glm' function?
  
  return(x)
  
}

compute_ors("died", "b_fi.score")

Here is a mock data:

library(oddsratio)
mydata = structure(list(died = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), b_fi.score = c(0.111111111111111, 
                                                                                 0.0555555555555556, 0.0185185185185185, 0.148148148148148, 0.0555555555555556, 
                                                                                 0.0277777777777778, 0.0277777777777778, 0.166666666666667, 0.0925925925925926, 
                                                                                 0.0925925925925926, 0.0740740740740741, 0.166666666666667, 0.0185185185185185, 
                                                                                 0.111111111111111, 0.0555555555555556, 0.101851851851852, 0.0925925925925926, 
                                                                                 0.138888888888889, 0.0462962962962963, 0.0925925925925926, 0.0555555555555556, 
                                                                                 0.0185185185185185, 0.0555555555555556, 0.259259259259259, 0.0925925925925926, 
                                                                                 0.101851851851852, 0.0925925925925926, 0.0833333333333333, 0.0555555555555556, 
                                                                                 0.111111111111111, 0.0555555555555556, 0.111111111111111, 0.111111111111111, 
                                                                                 0.0925925925925926, 0.222222222222222, 0.0740740740740741, 0.037037037037037, 
                                                                                 0.12962962962963, 0.0555555555555556, 0.148148148148148, 0.037037037037037, 
                                                                                 0.12962962962963, 0.111111111111111, 0.0740740740740741, 0.0925925925925926, 
                                                                                 0.0740740740740741, 0.0740740740740741, 0.175925925925926, 0.12962962962963, 
                                                                                 0.0740740740740741)), row.names = c(61L, 88L, 140L, 155L, 162L, 
                                                                                                                     234L, 260L, 466L, 552L, 567L, 618L, 643L, 754L, 817L, 912L, 921L, 
                                                                                                                     928L, 978L, 989L, 995L, 1021L, 1031L, 1050L, 1064L, 1101L, 1156L, 
                                                                                                                     1170L, 1180L, 1181L, 1206L, 1211L, 1221L, 1228L, 1230L, 1274L, 
                                                                                                                     1276L, 1286L, 1290L, 1318L, 1329L, 1340L, 1495L, 1509L, 1546L, 
                                                                                                                     1576L, 1661L, 1685L, 1703L, 1705L, 1714L), class = "data.frame")

Dani
  • 161
  • 9

2 Answers2

2

You may use setNames(list(0.1), predictor) instead of list(predictor = 0.1)

iago
  • 2,990
  • 4
  • 21
  • 27
0

It could be passed into dplyr::lst with assignment (:=) operator

compute_ors = function(outcome, predictor){
  
  fit.glm = glm(reformulate(predictor, response = outcome), 
                 data = mydata, family = binomial)
  
  x = oddsratio::or_glm(mydata, model = fit.glm,
          incr = dplyr::lst(!!predictor := 0.1), ci = 0.95) 
  
  
  
  return(x)
  
}

-testing

compute_ors("died", "b_fi.score")
 predictor oddsratio ci_low (2.5) ci_high (97.5) increment
1 b_fi.score     1.993        0.186         14.559       0.1
akrun
  • 874,273
  • 37
  • 540
  • 662