[英]Looping with glm function in R
So I'm just trying to do some pretty simple stuff using the glm
command. 所以我只是想使用glm
命令来做一些非常简单的事情。 I have a data frame which has 10 columns with different names and data. 我有一个数据框架,其中有10列,具有不同的名称和数据。 I then just need to do something like this: 然后,我只需要执行以下操作:
variables <- c("reponse_var", "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8", "var9")
for (variable in variables) {
reg <- glm(response_var ~ variable, data = df, family = binomial(link = logit))
summary(reg)
So basically it should just use the glm
function on each single variable, and then print out the output. 因此,基本上,它应该只对每个变量使用glm
函数,然后输出输出。 But this just doesn't work. 但这是行不通的。 The output is: 输出为:
variable lengths differ (found for 'variable')
I thought that since the data = df
it already knows which data frame it should take the data from, and usually it is enough to just write the name of the column in the glm
function. 我认为,由于data = df
它已经知道应该从哪个数据帧中获取数据,通常只要在glm
函数中写入列名就足够了。 But when doing this with for loops, well, this is what I get. 但是,使用for循环执行此操作时,这就是我所得到的。
One possible solution is to pack response_var
and variable
into a formula()
object, and then feed that into the regression. 一种可能的解决方案是将response_var
和variable
打包到一个formula()
对象中,然后将其提供给回归。 I think the error here is that you are feeding string objects into the glm()
. 我认为这里的错误是您正在将字符串对象输入glm()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.