The data.table package is very helpful in terms of speed. But I am having trouble actually using the output from a linear regression. Is there an easy way to get the data.table output to be as pretty/useful as that from the plyr package? Below is an example. Thank you!
library('data.table');
library('plyr');
REG <- data.table(ID=c(rep('Frank',5),rep('Tony',5),rep('Ed',5)), y=rnorm(15), x=rnorm(15), z=rnorm(15));
REG;
ddply(REG, .(ID), function(x) coef(lm(y ~ x + z, data=x)));
REG[, coef(lm(y ~ x + z)), by=ID];
The data.table coefficient estimates are output in a single column whereas the plyr/ddply coefficient estimates are output in multiple and nicely labeled columns.
I know I can run the regression three times with data.table but that seems really inefficient. I could be wrong, though.
REG[, Intercept=coef(lm(y ~ x + z))[1],
x =coef(lm(y ~ x + z))[2],
z =coef(lm(y ~ x + z))[3], by=ID];
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…