Automatically Create Model Formula in R

Live Online Training : Data Science with R

- Explain Advanced Algorithms in Simple English
- Live Projects
- Case Studies
- Job Placement Assistance
- Get 10% off till Oct 26, 2017
- Batch starts from October 28, 2017

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)

R Tutorials : 75 Free R Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

0 Response to "Automatically Create Model Formula in R"

Post a Comment

Next → ← Prev