【问题标题】:Prophet Forecasting using R for multiple items使用 R 对多个项目进行 Prophet 预测
【发布时间】:2018-08-29 06:06:36
【问题描述】:

我对在 R 中使用 Prophet 进行时间序列预测非常陌生。我能够使用 Prophet 预测单个产品的值。如果我可以使用循环使用 Prophet 为多种产品生成预测,有什么办法吗?下面的代码对单个产品来说工作得很好,但我正在尝试为多个产品生成预测

 library(prophet)
 df <- read.csv("Prophet.csv")
 df$Date<-as.Date(as.character(df$Date), format =  "%d-%m-%Y")
 colnames(df) <- c("ds", "y")
 m <- prophet(df)
 future <- make_future_dataframe(m, periods = 40)
 tail(future)
 forecast <- predict(m, future)
 write.csv(forecast[c('ds','yhat')],"Output_Prophet.csv")
 tail(forecast[c('ds', 'yhat', 'yhat_lower', 'yhat_upper')])

样本数据集:

【问题讨论】:

标签: r for-loop forecasting facebook-prophet


【解决方案1】:

这可以通过使用purrr 包中的listsmap 函数来完成。

让我们构建一些数据:

library(tidyverse) # contains also the purrr package
set.seed(123)
tb1 <- tibble(
  ds = seq(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day"),
  y = sample(365)
)
tb2 <- tibble(
  ds = seq(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day"),
  y = sample(365)
)

ts_list <- list(tb1, tb2) # two separate time series
# using this construct you could add more of course

构建和预测

library(prophet)

m_list <- map(ts_list, prophet) # prophet call

future_list <- map(m_list, make_future_dataframe, periods = 40) # makes future obs

forecast_list <- map2(m_list, future_list, predict) # map2 because we have two inputs

# we can access everything we need like with any list object
head(forecast_list[[1]]$yhat) # forecasts for time series 1
[1] 179.5214 198.2375 182.7478 173.5096 163.1173 214.7773
head(forecast_list[[2]]$yhat) # forecast for time series 2
[1] 172.5096 155.8796 184.4423 133.0349 169.7688 135.2990

更新(只是输入部分,构建和预测部分是一样的):

我根据OP请求创建了一个新的例子,基本上你需要把所有的东西都放在一个列表对象中:

# suppose you have a data frame like this:
set.seed(123)
tb1 <- tibble(
  ds = seq(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day"),
  productA = sample(365),
  productB = sample(365)
)
head(tb1)
# A tibble: 6 x 3
  ds         productA productB
  <date>        <int>    <int>
1 2018-01-01      105      287
2 2018-01-02      287       71
3 2018-01-03      149        7
4 2018-01-04      320      148
5 2018-01-05      340      175
6 2018-01-06       17      152

# with some dplyr and base R you can trasform each time series in a data frame within a list
ts_list <- tb1 %>% 
  gather("type", "y", -ds) %>% 
  split(.$type)
# this just removes the type column that we don't need anymore
ts_list <- lapply(ts_list, function(x) { x["type"] <- NULL; x })

# now you can continue just like above..

【讨论】:

  • 感谢 RLave。是否可以使用 for 循环来调用更多输入?我有近 50 个项目需要使用 Prophet 进行预测??
  • 就像我一样把它们放在一个列表中,使用 for 循环读取它们,然后你就可以按照我为 2 所做的操作了
  • 抱歉 RLave,我在创建该循环以调用更多输入时面临一些挑战。你能在这里分享示例代码sn-p吗?我在上面粘贴了示例数据集
  • @hari babu 更新了答案。请注意,您应该始终尝试发布数据示例,而不是图片。
  • @hmhsl 只要您使用不应该成为问题的列表,如果您遇到一些问题,您可以就 SO 提出特定的新问题。当然如果你有data.frame,你不能有两个不同长度的产品,所以你需要按照我的第一个例子,我从两个开始,并使用list()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
相关资源
最近更新 更多