简体   繁体   中英

using paste() in a for loop with glm

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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