Make regressions and predictions for groups in R

懵懂的女人 提交于 2019-12-12 09:16:04

问题


I have the following data.frame d from an experiment:

- Variable y (response, continuous)
- Factor f (500 levels)
- Time t (posixct)

In the last 8 years, y was measured roughly once a month (exact date in t) for each level of f. Sometimes there are 2 measures per month, sometimes a couple of month passed without any measures.

Sorry for not providing example data, but making up unregular time series goes beyond my R knowledge. ;)

I'd like to do the following with this data:

  1. make a regression using the loess() function (y ~ t), for each level of f
  2. make a prediction of y for the first day of each month and each level of f

The first point I think I solved by using Hadleys answer to this question:

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

So, now I have a models (class list), with a model for each level of f. I also created times for which I'd like to predict y for each level of f like this:

dates <- seq(min(t),max(t),"months")

But now I'm stuck on how to make predictions for each model. Something like this should work (pseudocode):

for each f in models
    p.f <- predict(models(f),dates)
    p.f.complete <- r.bind(p.f.comlete,p.f)
next f

As a result, I'd like to have this data.frame:

  • y.predicted
  • f
  • t.predicted (= dates)

Any help would be greatly appreciated.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/5553755/make-regressions-and-predictions-for-groups-in-r

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