在glm中用for循环使用paste() [英] using paste() in a for loop with glm

查看:197
本文介绍了在glm中用for循环使用paste()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,df.pts是一个数据框。我想运行使用不同的y变量(只有两个代码中显示)的十几个glm模型。我正在使用paste()函数for循环,但我似乎无法使paste()函数正常工作。我错过了paste()?

  SPCA2 = df.pts [,3] 
CLQU2 = df。 pts [,4]

df.list = list(SPCA2,CLQU2)

for(i in df.list){
qp.mod = glm(粘贴(i,〜NDVI + ELEV),family =quasipoisson,data = my.data)
print(summary(gp.mod))
}


解决方案

非常感谢!主要的问题是,df.list是一个向量列表,应该是一个名称列表。

换句话说,要纠正这个问题...

pre $ $ $ $ $ $ df.list =(SPCA2,CLQU2)

而不是

  df.list = list(SPCA2,CLQU2)

然而,正确地指出数据帧my.data不是正确的数据框。最后,虽然没有它,但函数as.formula()也起作用。再次,非常感谢!

In the code below, df.pts is a dataframe. I'd like to run about dozen glm models using different y variable (only two shown in the code). I'm using a for loop with the paste() function, but I can't seem to get the paste() function to work properly. What am I missing with paste()?

SPCA2 = df.pts[,3]
CLQU2 = df.pts[,4]

df.list = list(SPCA2, CLQU2)

for (i in df.list) {
    qp.mod = glm(paste(i,"~NDVI+ELEV"), family="quasipoisson", data=my.data)
    print(summary(gp.mod))
 }

解决方案

Many thanks! The main problem was that df.list was a list of vectors, and should have been a list of names.

I other words, to correct the problem...

df.list = ("SPCA2", "CLQU2")

instead of

df.list = list(SPCA2, CLQU2)

However, it was also correctly pointed out that the dataframe, my.data, was not the correct dataframe. Finally, while it worked without it, the function as.formula() also worked. Again, many thanks!

这篇关于在glm中用for循环使用paste()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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