How do I exclude certain variables from glm to R?

I have 50 variables. This is how I use them all in my glm.

var = glm(Stuff ~ ., data=mydata, family=binomial) 

But I want to exclude 2 of them. So how can I exclude 2 in a specific? I was hoping there would be something like this:

 var = glm(Stuff ~ . # notthisstuff, data=mydata, family=binomial) 

thoughts?

+8
r statistics glm
source share
1 answer

In addition to using - as in the comments

glm(Stuff ~ . - var1 - var2, data= mydata, family=binomial)

you can also multiply the data frame passed in

glm(Stuff ~ ., data=mydata[ , !(names(mydata) %in% c('var1','var2'))], family=binomial)

or

 glm(Stuff ~ ., data=subset(mydata, select=c( -var1, -var2 ) ), family=binomial ) 

(be careful with this last one, a subset function sometimes doesn't work well inside other functions)

You can also use the paste function to create a line representing the formula with the conditions of interest (a subset of the group of predictors waiting for you), then use as.formula to convert it to the formula.

+20
source share

All Articles