使用R对多个物品进行先知预测 [英] Prophet Forecasting using R for multiple items

查看:91
本文介绍了使用R对多个物品进行先知预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用R中的Prophet进行时间序列预测非常陌生.我能够使用Prophet预测单个产品的值.有什么办法可以使用Prophet使用循环为多个产品生成预测?下面的代码对单个产品绝对适用,但是我正在尝试为多个产品生成预测

I am very new to time series forecasting using Prophet in R. I am able to predict values for one single product using Prophet. Is there any way if i can use loop to generate forecast using Prophet for multiple products? The below code works absolutely fine for single product but i am trying to generate forecasts for multiple products

 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')])

样本数据集:

推荐答案

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

This can be done by using lists and map functions from the purrr package.

让我们建立一些数据:

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

更新(只是输入部分,构建和预测部分相同):

Update (just the input part, build and prediction part it's the same):

我根据OP请求创建了一个新示例,基本上,您需要将所有内容再次放入列表对象:

I created a new example based on OP request, basically you need to put everything again in a list object:

# 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..

这篇关于使用R对多个物品进行先知预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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