在自动预测中应用循环

时间:2015-03-17 13:44:34

标签: r forecasting

我正在尝试以长格式预测data.frame中的各个变量。我陷入了循环[apply]部分。问题是:如何用apply替换手动预测?

library(forecast)
library(data.table)

# get time series
www = "http://staff.elena.aut.ac.nz/Paul-Cowpertwait/ts/cbe.dat"
cbe = read.table(www, header = T)

# in this case, there is a data.frame in long format to start with
df = data.table(cbe[, 2:3])
df[, year := 1958:1990]
dfm = melt(df, id.var = "year", variable.name = "indicator", variable.factor = F) # will give warning because beer = num and others are int
dfm[, site := "A"]
dfm2= copy(dfm) # make duplicate to simulate other site
dfm2[, site := "B"]
dfm = rbind(dfm, dfm2)


# function to make time series & forecast
f.forecast = function(df, mysite, myindicator, forecast.length = 6, frequency  = freq) {

  # get site and indicator
  x = df[site == mysite & indicator == myindicator,]

  # convert to time series
  start.date = min(x$year)
  myts = ts(x$value, frequency = freq, start = start.date)

  # forecast
  myfc = forecast(myts, h = forecast.length, fan = F, robust = T)
  plot(myfc, main = paste(mysite, myindicator, sep = " / "))
  grid()

  return(myfc)
}

# the manual solution
par(mfrow = c(2,1))
f1 = f.forecast(dfm, mysite = "A", myindicator = "beer", forecast.length = 6, freq = 12)
f2 = f.forecast(dfm, mysite = "A", myindicator = "elec", forecast.length = 6, freq = 12)

# how to loop? [in the actual data set there are many variables per site]
par(mfrow = c(2,1))
myindicators = unique(dfm$indicator)
sapply(myindicator, f.forecast(dfm, "A", myindicator = myindicators, forecast.length = 6, freq = 12)) # does not work

1 个答案:

答案 0 :(得分:1)

我建议使用split并删除f.forecast的第二个和第三个参数。您可以直接传递要预测的data.frame的子集。例如:

f.forecast = function(x, forecast.length = 6, frequency  = freq) {
  #comment the first line
  #x = df[site == mysite & indicator == myindicator,]
  #here goes the rest of the body
  #modify the plot line
  plot(myfc, main = paste(x$site[1], x$indicator[1], sep = " / "))
} 

现在您拆分整个df并为每个子集调用f.forecast

dflist<-split(df,df[,c("site","indicator")],drop=TRUE)
lapply(dflist,f.forecast)