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

Exclude more than one columns when build logistic regression model using glm

例如我的模型有这个代码

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

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

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

这引发了错误。

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

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

?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  

现在你看到只有拦截是估计的。