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

查看:14
本文介绍了我如何从 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)

想法?

推荐答案

除了使用 - 就像在评论中一样

In addition to using the - like in the comments

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 )

(注意最后一个,subset 函数有时在其他函数内部不能很好地工作)

(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天全站免登陆