【发布时间】:2021-03-30 10:13:29
【问题描述】:
我正在尝试遍历我的数据表列,并使用 for 循环将 glm 应用于每一列。然后我想从模型中提取回归系数并将它们添加到我的输出数据表中。
这里dt是一个数据表,y是一个向量:
output = data.table('reg_coef' = numeric())
for(n in 1:ncol(dt)){
model = glm(y ~ dt[, n], family=binomial(link="logit"))
reg_coef = summary(model)$coefficients[2]
output = rbindlist(list(output, list(reg_coef)))
}
为什么这不起作用?我收到此错误:
Error in `[.data.table`(dt, , n) :
j (the 2nd argument inside [...]) is a single symbol but column name 'n' is not found. Perhaps you intended DT[, ..n]. This difference to data.frame is deliberate and explained in FAQ 1.1.
【问题讨论】: