Reformulate in R log(Y+1)

2019-07-16 17:41发布

问题:

I am working on a data set where I have to generate formula to be passed to lm dynamically. So, I am using reformulate to do this.

formula = reformulate(termlabels = c('feature1', 'feature2', 'feature3'), response="y")

y is continuous.

This gets me the formula as y ~ feature1+feature2+feature3

However I want the formula to be log(y+1) ~ feature1+feature2+feature3

How do I do this using reformulate?

回答1:

Pass in the response as a quoted expression:

x <- c("feature1", "feature2", "feature3")
reformulate(x, response=quote(log(y+1)))

Or you could just construct the formula manually. This is what reformulate does under the hood.

formula(paste("log(y + 1) ~", paste(x, collapse="+"))) 


标签: r formula