如何从R的glm中排除特定变量? [英] how do i exclude specific variables from a glm in R?

查看:213
本文介绍了如何从R的glm中排除特定变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有50个变量.这就是我在glm中全部使用它们的方式.

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

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

但是我想排除其中2个.那么如何具体排除2?我希望会有这样的事情:

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

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

有想法吗?

推荐答案

除了在注释中使用-

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

您还可以对传入的数据帧进行子集

you can also subset the data frame passed in

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

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

(请注意最后一个子集函数有时无法在其他函数中正常工作)

(be careful with that last one, the subset function sometimes does not work well inside of other functions)

您还可以使用paste函数创建一个表示感兴趣项的公式的字符串(替换为所需的预测变量组),然后使用as.formula将其转换为公式.

You could also use the paste function to create a string representing the formula with the terms of interest (subsetting to the group of predictors that you want), then use as.formula to convert it to a formula.

这篇关于如何从R的glm中排除特定变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆