Make regressions and predictions for groups in R

半腔热情 提交于 2019-12-04 19:14:14

The most complicated thing to do is make the function to predict and ussing lapply. Which is not very hard to do.

dates <- data.frame(t = dates)
y.predicted <- lapply(models, function (x) predict(x, newdata = dates))

if you want to rbind y.predicted just use

y.predicted <- do.call(rbind, y.predicted)

HTH

Edited

The key is to use ldply() with predict(). Here is an example using dummy data:

library(plyr)
d <- data.frame(
        f = rep(LETTERS[1:5], each=20),
        t = rep(1:20, 5),
        y = runif(100))

models <- dlply(d, "f", function(df) loess(y ~ as.numeric(t), data = df))
predict(models[[1]])

x <- ldply(models, predict)
colnames(x) <- c("f", 1:20)
x
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!