Search code examples
rfunctionlogistic-regressionglm

How to use logistic regression in R function


I am trying to use r base logistic regression function in my customized r function but my glm() is not able to recognize my variables. I have tried multiple searching keywords in the search engine but all answers are related to fitting a logistic regression.

For example:

First try:

dat <- data.frame(a = c(3,4,5), b = c("a","a","b"))

logit <- function(dataname, x, y) {
  model = glm(y ~ x, data = dataname, family = "binomial")
  model
}

logit(dat, a, b)
Error in eval(predvars, data, env) : object 'b' not found

The alternative way:

logit <- function(dataname, x, y) {
  model = glm(eval(substitute(y), dat) ~ eval(substitute(x), dat), family = "binomial")
  model
}

logit(dat, a, b)

The output will change my IV to eval(substitute(x), dataname) instead of x.

glm.fit: fitted probabilities numerically 0 or 1 occurred
Call:  glm(formula = eval(substitute(y), dataname) ~ eval(substitute(x), 
    dataname), family = "binomial")

Coefficients:
                  (Intercept)  eval(substitute(x), dataname)  
                      -208.27                          46.34  

Degrees of Freedom: 2 Total (i.e. Null);  1 Residual
Null Deviance:      3.819 
Residual Deviance: 3.597e-10    AIC: 4

Is there any way I can have the proper output with the correct name of IV in the output?

Thanks


Solution

  • I agree with @IceCreamToucan that the best way is to pass in the formula to the function

    logit <- function(dataname, formula) {
      model = glm(formula, data = dataname, family = "binomial")
      model
    }
    
    logit(dat, b~a)
    

    Otherwise you should build the formula first, then pass it to glm

    logit <- function(dataname, x, y) {
      formula <- reformulate(as.character(substitute(x)), as.character(substitute(y)))
      model = glm(formula, data = dataname, family = "binomial")
      model
    }
    
    logit(dat, a, b)