【发布时间】:2018-07-12 18:05:15
【问题描述】:
我正在对时间序列数据执行预测,但我正在努力减少计算时间。这是代码示例。所以代码实际上预测了不同监测站的温度。对于 134 个站点,在我的计算机上大约需要 10 分钟。我在想是否有办法减少整体计算时间。
示例数据如下所示。全站共134个,观测时间为2个月。
date station1 station2 station3 station4
18/01/2017 0:00 36.8 36.25 27.4 25.75
19/01/2017 0:00 30.71428571 34.6 29.4 22.33333333
20/01/2017 0:00 38.75 40.33333333 30.16666667 29.33333333
21/01/2017 0:00 40.83333333 40.33333333 31.2 3 2.25
dat1 <-read.csv("smart.csv")
library(forecast)
attach(dat1)
library(forecastHybrid)
ptm <- proc.time()
result<-data.frame(auto=0,nnetar=0)
for(i in 2:135) {
temp.ts <-ts(dat1[i])
train = temp.ts[1:600]
test = temp.ts[601:620]
hm3 <- hybridModel(train, weights = "equal", errorMethod = "MASE", models =
"an")
accuracy(hm3,individual = TRUE)
hForecast <- forecast(hm3, h = 1)
result<-rbind(result,data.frame(auto=hForecast$pointForecasts[1],
nnetar=hForecast$pointForecasts[2]))
fit_accuracy <- accuracy(hForecast, test)
}
proc.time()-ptm
write.csv(result, file= "xyz.csv")
【问题讨论】:
-
当您给我们一些样本数据时,更容易回答问题。尽管如此,我发现的第一件事是您在每个步骤中都在增加结果数据框。我会在循环之前初始化最终维度(即
results <- list(135),然后将结果存储在该列表中。最后您可以将其转换为一个data.frame(即rbind.data.frame(results)) -
Profile 你的代码!如果大部分时间都花在
hybridModel上,那么就没有明显简单的方法来提高效率。 -
...当然除了并行化。
-
您能否提供数据样本,这将有助于发现您的瓶颈
-
感谢@kath 的建议。我会试试看它是否有效。
标签: r time prediction computation