作为学习 plyr 的练习,我尝试制作 Rob Hyndman 最近发布的 plyr 版本:
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))
但是,我一直在挣扎。这是我的尝试:
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))
不返回任何值。有人可以帮我解决我对 plyr 的误解吗?