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
yfor 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 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.