Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plyr for generating forecasts

as an exercise in learning plyr, I've tried to do a plyr version of a recent posting by Rob Hyndman:

library(forecast); library(plyr)
# Hyndman, R. J. (2013, Jan 7). Batch forecasting in R
# Retrieved Jan 8, 2013, from Research Tips: http://robjhyndman.com/researchtips/batch-forecasting/

retail <- read.csv("http://robjhyndman.com/data/ausretail.csv",header=FALSE)
retail <- ts(retail[,-1],f=12,s=1982+3/12)

ns <- ncol(retail)
h <- 24
fcast <- matrix(NA,nrow=h,ncol=ns)
for(i in 1:ns)
  fcast[,i] <- forecast(retail[,i],h=h)$mean

write(t(fcast),file="retailfcasts.csv",sep=",",ncol=ncol(fcast))

But, I've struggled. This is my attempt:

n.cols <- ncol(retail)
h <- 24
series.names <- names(retail[,2:n.cols])

fcast.func <- function(retail) {
  retail.ts <- ts(retail,f=12,s=1982+3/12)  
  fcast.func <- forecast(retail.ts,h=h)$mean
}

ddply.fcast <- ddply(.data=retail[,2:n.cols], .variables=series.names, .fun=colwise(fcast.func))

Which returns no values. Could someone help me with my misunderstanding of plyr please?

like image 686
Isaiah Avatar asked Mar 21 '26 12:03

Isaiah


1 Answers

The problem is that you use ddply(, where the first d = datatype of input = data.frame and the second d = datatype of output = data.frame (again). However, the input you are providing retail[, 2:ncols] is not a data.frame.

class(retail)
[1] "mts" "ts"

Instead, what you could do is ldply which takes a list as input, runs your function and tries to output a data.frame. You could accomplish in this manner.

require(forecast)
require(plyr)

retail <- read.csv("http://robjhyndman.com/data/ausretail.csv",header=FALSE)
retail <- ts(retail[,-1],f=12,s=1982+3/12)

ns <- ncol(retail)
h <- 24
plyr.fcast <- t(ldply(1:ns, function(idx) {
    c(forecast(retail[, idx], h = h)$mean)
}))

This takes quite a lot of time. If you want to run in parallel (assuming you're running on a cluster/machine with many cores), then you can install the package doMC and then use it as follows:

require(doMC)
registerDoMC(20) # BEWARE: use it if you have 20 processors available!!
plyr.fcast <- t(ldply(1:ns, function(idx) {
    c(forecast(retail[, idx], h = h)$mean)
}, .parallel = TRUE))

The transpose gives the results equal to fcast and also type-casts the data.frame output from plyr to a matrix. So, you can use the same write syntax to write to file.

Hope this helps.

like image 60
Arun Avatar answered Mar 24 '26 02:03

Arun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!