Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

vector - Using character as an object for linear model (R)

I'm trying to test linear models with different interactions removed, e.g.

lmtest<-lm(out1~(.)^2 - var4:var5, data=dt1)

The interactions I'm testing for are stored in a character vector cvect = (var1:var2, var1:var3... etc), and I'm looking to use these to drop interactions. I've tried

lmtest<-lm(out1~(.)^2 - cvect[5], data=dt1)
lmtest<-lm(out1~(.)^2 - noquotes(cvect[5]), data=dt1)
lmtest<-lm(out1~(.)^2 - paste(cvect[5]), data=dt1)

But neither of these worked. Is there an alternative way of making this approach work?

question from:https://stackoverflow.com/questions/65923317/using-character-as-an-object-for-linear-model-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can never keep the formula-transforming functions straight so I usually build formulas as character strings. In your case:

# build formulas as characters
my_formula = paste("out1 ~ (.)^2 -", cvect)
# use like this:
lmtest(as.formula(my_formula[1]), data = dt1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...