简体   繁体   English

在带有glm的for循环中使用paste()

[英]using paste() in a for loop with glm

In the code below, df.pts is a dataframe. 在下面的代码中,df.pts是一个数据帧。 I'd like to run about dozen glm models using different y variable (only two shown in the code). 我想使用不同的y变量(代码中仅显示两个)运行大约十二个glm模型。 I'm using a for loop with the paste() function, but I can't seem to get the paste() function to work properly. 我正在使用带有paste()函数的for循环,但似乎无法使paste()函数正常工作。 What am I missing with paste()? 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. 主要问题是df.list是向量列表,并且应该是名称列表。

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. 但是,也正确指出该数据帧my.data不是正确的数据帧。 Finally, while it worked without it, the function as.formula() also worked. 最后,虽然没有它也可以工作,但是函数as.formula()也可以工作。 Again, many thanks! 再次非常感谢!

您需要在paste之前添加as.formula ,以让R知道您要将其视为公式而不是字符。

qp.mod = glm(as.formula(paste(i,"~NDVI+ELEV")), family="quasipoisson", data=my.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM