2

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.

4

2 回答 2

2

最复杂的事情是使函数 topredict和 ussing lapply。这不是很难做到。

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

如果你想 rbind y.predicted 只需使用

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

高温高压

于 2011-04-05T16:24:41.227 回答
2

已编辑

关键是将ldply() 与predict() 一起使用。这是一个使用虚拟数据的示例:

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
于 2011-04-05T16:17:21.473 回答