使用glm构建逻辑回归模型时排除多个列

时间:2016-06-04 20:36:17

标签: r regression logistic-regression glm

例如我的模型有这个代码

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

因此,-ID排除ID列。如果我想排除更多列,该怎么办?我试过了

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

引发了错误。

2 个答案:

答案 0 :(得分:2)

尝试在数据参数中使用否定命名列:

 glm(... , data=train[, -c( "ID", "YOB","ABC")], ...)

答案 1 :(得分:2)

?formula提到您可以使用-删除字词。方法如下:

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

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

好吧,我可以给你一些例子:

> 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

现在我们建立一个模型,放弃GirthHeight

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

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

Coefficients:
(Intercept)  
      30.17  

现在你看到只估计了拦截。