I am performing a regression using data.table
. However, I would like to dynamically specify the right-hand side of the formula as in the following example:
library(data.table)
dt <- data.table(data.frame("a"=seq(1,10,1),
"b"=seq(1,20,2),
"c"=seq(1,30,31),
"d"=c("one", "one", "one", "one", "one",
"two", "two", "two", "two", "two")))
varname <- "a"
dt[, lmtest::coeftest(x = lm(get(varname) ~ b),
vcov. = sandwich::NeweyWest(x = lm(get(varname) ~ b)) ), by = "d"]
do.regr <- function(rhs) {
dt[, lmtest::coeftest(x = lm(get(varname) ~ rhs),
vcov. = sandwich::NeweyWest(x = lm(get(varname) ~ rhs)) ), by = "d"]
}
do.regr("b+c")
This gives an error. Is there a way to pass a string and have that being the variables used on the right-hand side of the formula?