【问题标题】:Apply loop in automated forecast在自动预测中应用循环
【发布时间】:2015-03-17 13:44:34
【问题描述】:

我正在尝试从长格式的 data.frame 中预测单个变量。我陷入了循环 [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

【问题讨论】:

  • 您只是在寻找sapply(myindicators, function(i) f.forecast(dfm, "A", myindicator = i, forecast.length = 6, freq = 12)) 吗?

标签: r forecasting


【解决方案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)

【讨论】:

  • 谢谢!这种方法很有意义,但我无法让它发挥作用。代码仍然希望将两个变量都塞进一个图中。此外,由于拆分方法,我不得不删除情节中的“主要”部分,这意味着情节的标题现在是通用的。
  • 我错过了情节部分,当然你不能通过mysitemyindicator,因为它们不存在。我要编辑我的答案来解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
相关资源
最近更新 更多