I would like to run a regression within a data.table. The formula needs to be constructed dynamically. I have tried the following method:
data.table
formula
lm can accept a character string as the formula so combine that with .SD like this:
lm
.SD
> x[, as.list(coef(lm("a ~ b", .SD))), by = id] id (Intercept) b 1: 1 21 -1 2: 2 21 -1 3: 3 21 -1 4: 4 21 -1 5: 5 21 -1