1

我正在尝试交叉验证多个时间序列并将所有结果绘制在一个图中。让我们看看使用单个时间序列案例的工作原理。

单TS案例

对于 5 年期间的模拟月度时间序列,我们可以使用以下方式交叉验证预测性能,

library(prophet)
library(dplyr)
library(purrr)

## Dataset creation
ds <- seq(as.Date("2014-01-01"), as.Date("2018-12-31"), by = "month")
y <- sample(60)
df <- data.frame(ds, y)
head(df)

m <- prophet(df, seasonality.mode = 'multiplicative')
future <- make_future_dataframe(m, periods = 60)
fcst <- predict(m, future)

df.cv <- cross_validation(m, initial = 730, horizon = 365, period = 180, units = 'days')
plot_cross_validation_metric(df.cv, metric = 'mape')

最后一行给了我这样的情节,

在此处输入图像描述

具有多个时间序列的复杂案例,

假设我们有 4 个时间序列(可能更多,例如数千个)。我正在尝试获取显示 mape 值的类似线图。

我在这里做的是这个,

library(prophet)
library(dplyr)
library(purrr)

## Dataset creation
id1 <- rep(12, 60)
ds1 <- seq(as.Date("2014-01-01"), as.Date("2018-12-31"), by = "month")
value1 <- sample(60)

id2 <- rep(132, 48)
ds2 <- seq(as.Date("2015-01-01"), as.Date("2018-12-31"), by = "month")
value2 <- sample(48)

id3 <- rep(210, 72)
ds3 <- seq(as.Date("2013-01-01"), as.Date("2018-12-31"), by = "month")
value3 <- sample(72)

id <- c(id1, id2, id3)
ds <- c(ds1, ds2, ds3)
y <- c(value1, value2, value3)

df <- data.frame(id, ds, y)
head(df)

# preparations
l_df <- df %>% split(.$id)

m_list <- map(l_df, prophet) # prophet call
future_list <- map(m_list, make_future_dataframe, periods = 1) # makes future obs
forecast_list <- map2(m_list, future_list, predict)

df.cv <- cross_validation(m_list, initial = 720, period = 30, horizon = 365, units = 'days')

这给了我一个错误,

> df.cv <- cross_validation(m_list, initial = 720, period = 30, horizon = 365, units = 'days')
Error in generate_cutoffs(df, horizon.dt, initial.dt, period.dt) : 
  Less data than horizon after initial window. Make horizon or initial shorter.
In addition: Warning messages:
1: In max(df$ds) : no non-missing arguments to max; returning -Inf
2: In min(df$ds) : no non-missing arguments to min; returning Inf
3: In max(df$ds) : no non-missing arguments to max; returning -Inf
4: In min(df$ds) : no non-missing arguments to min; returning Inf

我尝试了不同的输入组合,但没有一个工作。所以,我想知道如何交叉验证所有模型并从中绘制一个图表。知道怎么做吗?

4

0 回答 0