用于在R中一次预测多个数据集的For循环 [英] For loop for forecasting several datasets at once in R

查看:74
本文介绍了用于在R中一次预测多个数据集的For循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含时间,地区,销售"变量的数据集,我想使用library(forecast)使用ARIMA或ETS(SES)预测每个地区的销售.总共有70个区域,每个区域每个都有152个观测值(3年的数据).像这样:

I have a dataset with "Time, Region, Sales" variables and I want to forecast sales for each region using ARIMA or ETS(SES) using library(forecast). There are a total of 70 regions and all of them have 152 observations each and (3 years of data). Something like this:

  Week      Region    Sales 
01/1/2011      A       129
07/1/2011      A       140
14/1/2011      A       133
21/1/2011      A       189
...           ...      ...
01/12/2013     Z       324
07/12/2013     Z       210
14/12/2013     Z       155
21/12/2013     Z       386
28/12/2013     Z       266 

因此,我希望R将每个区域视为不同的数据集并执行auto.arima.我猜想for循环应该是这里的理想选择,但我不幸失败了. 我理想地希望它做的是一个for循环,以运行类似这样的内容(每152个观察值自动生成一个Arima):

So, I want R to treat every region as a different dataset and perform an auto.arima. I am guessing a for loop should be an ideal fit here but I miserably failed with it. What I would ideally want it to do is a for loop to run something like this (an auto arima for every 152 observations):

fit.A <- auto.arima(data$Sales[1:152])  
fit.B <- auto.arima(data$Sales[153:304])
....
fit.Z <- auto.arima(data$Sales[10490:10640])

我遇到了,但是在将数据帧转换为时间序列时,我得到的只是NA

I came across this but while converting the dataframe into timeseries, all I got is NAs.

感谢您的帮助!谢谢.

推荐答案

尝试使用非常高效的data.table包(假设您的数据集称为temp)

Try the very efficient data.table package (assuming your data set called temp)

library(data.table)
library(forecast)
temp <- setDT(temp)[, list(AR = list(auto.arima(Sales))), by = Region]

最后一步将以list格式将结果保存在temp中(因为这是可以存储此类对象的唯一格式).

The last step will save your results in temp in a list formats (as this is the only format you can store this type of an object).

后记之后,您可以在这些列表上执行任何所需的操作,例如,检查它们:

Afterwords you can do any operation you want on these lists, for example, Inspecting them:

temp$AR
#[[1]]
# Series: Sales 
# ARIMA(0,0,0) with non-zero mean 
# 
# Coefficients:
#   intercept
# 147.7500
# s.e.    12.0697
# 
# sigma^2 estimated as 582.7:  log likelihood=-18.41
# AIC=40.82   AICc=52.82   BIC=39.59
#
#[[2]]
# Series: Sales 
# ARIMA(0,0,0) with non-zero mean 
# 
# Coefficients:
#   intercept
# 268.2000
# s.e.    36.4404
# 
# sigma^2 estimated as 6639:  log likelihood=-29.1
# AIC=62.19   AICc=68.19   BIC=61.41

或绘制预测(等等)

temp[, sapply(AR, function(x) plot(forecast(x, 10)))]

这篇关于用于在R中一次预测多个数据集的For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆