Fit model by group using Data.Table package

怎甘沉沦 提交于 2021-02-06 22:00:37

问题


How can I fit multiple models by group using data.table syntax? I want my output to be a data.frame with columns for each "by group" and one column for each model fit. Currently I am able to do this using the dplyr package, but can't do this in data.table.

# example data frame
df <- data.table(
   id = sample(c("id01", "id02", "id03"), N, TRUE),     
   v1 = sample(5, N, TRUE),                          
   v2 = sample(round(runif(100, max = 100), 4), N, TRUE) 
)

# equivalent code in dplyr
group_by(df, id) %>%
do( model1= lm(v1 ~v2, .),
    model2= lm(v2 ~v1, .)
  )

# attempt in data.table
df[, .(model1 = lm(v1~v2, .SD), model2 = lm(v2~v1, .SD) ), by = id ]

# Brodie G's solution
df[, .(model1 = list(lm(v1~v2, .SD)), model2 = list(lm(v2~v1, .SD))), by = id ]

回答1:


Try:

df[, .(model1 = list(lm(v1~v2, .SD)), model2 = list(lm(v2~v1, .SD))), by = id ]

or slightly more idiomatically:

formulas <- list(v1~v2, v2~v1)
df[, lapply(formulas, function(x) list(lm(x, data=.SD))), by=id]


来源:https://stackoverflow.com/questions/29420674/fit-model-by-group-using-data-table-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!