简体   繁体   中英

R - how to use column names as arguments in a function and insert into a model formula

I want a function where the arguments can take a variable name (that is part of a dataset but is not stored as an object in the environment) and inserts that variable name into a model formula.

For example:

# Some data with a couple of variables
my_df <- data.frame(y = rbinom(10, 1,0.5), var1 = runif(10), var2 = runif(10))

# A function that fits a model using predictor specified in the arguments
my_fun <- function(var_name, df){
    glm(y ~ var_name, data = df, family = "binomial")
}

When I try to use the function I get the following error message

my_fun(var1, my_df)
Error in eval(expr, envir, enclos) : object 'var1' not found 

# What I want the function to do
glm(y ~ var1, data = my_df, family = "binomial")

Is there a way to get this kind of function to work?

You can parse unquoted var_name with substitute :

my_fun <- function(var_name, df){
    glm.formula <- substitute(y ~ x, list(x = substitute(var_name)))
    glm(glm.formula, data = df, family = "binomial")
}

An example:

my_fun(var1, my_df)

# Call:  glm(formula = glm.formula, family = "binomial", data = df)
# 
# Coefficients:
# (Intercept)         var1  
#      -1.226        3.108  
# 
# Degrees of Freedom: 9 Total (i.e. Null);  8 Residual
# Null Deviance:        13.46 
# Residual Deviance: 11.35  AIC: 15.35

glm(y ~ var1, data = my_df, family = "binomial")

# Call:  glm(formula = y ~ var1, family = "binomial", data = my_df)
# 
# Coefficients:
# (Intercept)         var1  
#      -1.226        3.108  
# 
# Degrees of Freedom: 9 Total (i.e. Null);  8 Residual
# Null Deviance:        13.46 
# Residual Deviance: 11.35  AIC: 15.35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM