Automatically Create Model Formula in R

The following method creates model formula in an automated fashion. In this program, df is a name of dataframe. 'admit' is a target variable. And we are building logistic regression.
names(df) <- make.names(names(df))
y <- "admit"
x <- names(df)[!names(df) %in% y]
mymodel <- as.formula(paste(y, paste(x, collapse="+"), sep="~"))
glm(mymodel, data=df, family = binomial)

Suppose you want to include all the predictors except one. 
glm(admit~.-rank, data = df, family = binomial)
Here, we are telling R to ignore variable rank while building model.

Drop more than one variable in formula
glm(admit~.-rank -gre, data = df, family = binomial)

ListenData Logo
Spread the Word!
Related Posts
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

0 Response to "Automatically Create Model Formula in R"

Post a Comment

Next → ← Prev
Looks like you are using an ad blocker!

To continue reading you need to turnoff adblocker and refresh the page. We rely on advertising to help fund our site. Please whitelist us if you enjoy our content.