遵循这样一个很好的问题:
Why is using update on a lm inside a grouped data.table losing its model data?,我在data.table
内运行回归并将其存储,如下所示:
DT = data.table(iris)
fit = DT[, list(list(lm(Sepal.Length ~ Sepal.Width + Petal.Length))), by = Species]
但是,我想将.J
输出存储为lm
对象lm输出,而不是作为data.table:
class(fit[Species=="setosa"])
#i would like fit to contain 3 lm objects, not data.tables!
# [1] "data.table" "data.frame"
我的问题是,如何在fit
3 lm对象而不是3个数据表中存储,我需要的原因是,我想进一步使用fit
进行样本预测(使用predict.lm
)?
例如,我想在数据表中存储以下类型的元素:
model<-lm(Sepal.Length ~ Sepal.Width + Petal.Length,data=DT[Species=="setosa"])
class(model)
# [1] "lm"
#i would like the first element of fit to inclide model -> the model output object
new_data<-DT #just a toy example :) this isnt really the new data
predict(model,new_data)