R - using glm inside a data.table
You need to correctly specify the data
argument within glm
. Inside a data.table
(using [
), this is referenced by .SD
. (see create a formula in a data.table environment in R for related question)
So
modellingDF[,list(Outcome, fitted = glm(data = .SD,
formula = Outcome ~ IntCol ,family = binomial(link = logit))$fitted),
by=variable]
will work.
While in this case (simply extracting the fitted values and moving on), this approach is sound, using data.table
and .SD
can get in a mess of environments if you are saving the whole model and then attempting to update
it (see Why is using update on a lm inside a grouped data.table losing its model data?)