I am having trouble with the scoping of variables in a "lm" function embedded within a data.table environment embedded within a function. I am trying to pass a formula through a self-written function that is passed to a lm function that is embedded in a data.table. I have created a simple example of what I am trying to accomplish:
require(data.table)
d <- as.data.frame(cbind(seq(1,10), rnorm(1000), rnorm(1000)))
names(d) <- c('firm', 'y', 'x')
lm_example <- function(formula, data, group){
data <- data.table(data)
data[ , list(model = list(lm(formula))), by=group]
# what I want it to evaluate to
# data[ , list(model = list(lm(y ~ x))), by='firm']
}
lm_example(y ~ x, data=d, group='firm') # this doesn't work
The example evaluates to "Error in eval(expr, envir, enclos) : object 'y' not found." As a hack I have tried all sorts of combinations of "paste", "quote", "eval", "substitute", and "as.formula" but I can't seem to get that to work either.
Any help with figuring out the scoping and syntax to get this function to work would be very much appreciated.