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:
- make a regression using the
loess()
function(y ~ t)
, for each level off
- make a prediction of
y
for the first day of each month and each level off
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开发者_如何学JAVA 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.
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
精彩评论