gpt4 book ai didi

r - 使用 R 对多个项目进行 Prophet 预测

转载 作者:行者123 更新时间:2023-12-01 19:34:33 25 4
gpt4 key购买 nike

我对在 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')])

示例数据集:

enter image description here

最佳答案

这可以通过使用 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..

关于r - 使用 R 对多个项目进行 Prophet 预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com