2

For example my model has this code

g = glm(Vote ~., -ID, data=train, family=binomial)

So, -ID is excluding ID columns. What do I do if I want to exclude a few more columns? I tried

g = glm(Vote ~., -c(ID,YOB,ABC) , data=train, family=binomial)

that threw an error.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
PeddiePooh
  • 403
  • 8
  • 17

2 Answers2

4

?formula mentioned that you can use - to drop term. Here is how:

glm(Vote ~. -ID, data = train, family = binomial)

g = glm(Vote ~. - ID - YOB - ABC, data = train, family = binomial)

Well I might give you some example:

> head(trees) ## this is R's built-in dataset

  Girth Height Volume
1   8.3     70   10.3
2   8.6     65   10.3
3   8.8     63   10.2
4  10.5     72   16.4
5  10.7     81   18.8
6  10.8     83   19.7

Now we build a model, dropping Girth and Height:

> lm(Volume ~. -Girth - Height, trees)

Call:
lm(formula = Volume ~ . - Girth - Height, data = trees)

Coefficients:
(Intercept)  
      30.17  

Now you see that only intercept is estimated.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
2

Try using negation of named columns in data argument:

 glm(... , data=train[, -c( "ID", "YOB","ABC")], ...)
IRTFM
  • 258,963
  • 21
  • 364
  • 487