Hi I have some data in data.table format in R and I need to run some function.
Let say I have a data.table called A with columns, "name" "height", "weight".
I want to run some function, i.e. linear regression within data.table and store the coefficients, RMSE into the table results.
A[, .(beta = lm(height ~ weight)$coefficients[2], RMSE =
as.numeric(sqrt(crossprod(lm(height
~ weight)$residuals)/(length(lm(height ~ weight)$residuals)-
(length(coef(lm(height ~ weight)))-1)))*100),
by=.(name)]
My question: Is there a way to save the lm(height ~ weight) result as an object and then access this object's data so data.table don't need to run the lm function like 4 times in here?
This runs but it is a bit too slow compared to me using foreach and loop over "name" as I have millions rows of data.
Thanks.