R - 使用 purrr 中的 pmap 遍历具有时间序列函数的列表列表



我编写了以下函数mape_fcn来计算时间序列预测的准确性,如下所示:

library(forecast)
mape_fcn <- function(fcn, trn, tst, h) {
fcst <- forecast(fcn(trn), h = h) # calculates a forecast using the training data
fcst_mean <- as.numeric(fcst$mean) # converts the forecast to a numeric value
mape <- sum(abs(fcst_mean - tst))/sum(tst) # calculates the weighted MAPE (Mean Absolute Percentage Error) of this forecast on the training data to the test data
mape
}

其中FCN是测试的时间序列函数,TRN是时间序列训练数据(除了最后 h 观测值之外的所有数据(,tst是为测试预测的准确性而保留的时间序列测试数据,h是预测的范围。

我将这个函数与purrr库中的pmap一起使用,以计算预测库中一个训练和一个测试数据集上各种函数的 MAPE,如下所示。

library(purrr)
library(forecast)
fcns <- list(auto.arima, nnetar, tbats)
trainlstsku1 <- list(trainsku1)
testlstsku1 <- list(testsku1)
pmap(list(fcns, trainlstsku1, testlstsku1, 12), mape_fcn) 

下面的输出是各种函数的 MAPE(小数,而不是百分比(。

# [[1]]
# [1] 0.4552366
# 
# [[2]]
# [1] 0.3576489
# 
# [[3]]
# [1] 0.4256295

我想将其扩展为分别迭代两个训练集和两个测试集,正如我在下面尝试的那样。

trainlstsample <- list(list(trainsku1, trainsku2))
testlstsample <- list(list(testsku1, testsku2))
pmap(list(fcns, trainlstsample, testlstsample, 12), mape_fcn)

因此,对于每个数据集(sku1 和 sku2(,我希望查看列出的三个函数的 MAPE。将有 6 个 MAPE 输出。

但是这是我得到的错误:

Error in is.constant(x) : 
(list) object cannot be coerced to type 'double'
Called from: is.constant(x)

以下是相应的数据,其中包含将向量转换为ts对象的代码。(由于格式问题,我没有共享为ts对象。虽然通常不建议,但我正在共享所有数据,因为数据集不大,并且预测功能需要足够的观察。提前感谢您的帮助。

trainsku1 <- ts( c(31900, 48000, 16000, 0, 16000, 48000, 96000, 0, 0, 31900, 32000, 63000, 63600, 32000, 0, 0, 0, 63100, 63300, 126500, 32000, 96000, 32000, 61400, 30000, 32000, 63700, 63700, 0, 0, 92800, 29800, 0, 0, 61800, 76500, 47800, 107600, 45200, 31700, 14600, 63600, 79500, 31900, 16000, 48000, 48000, 48000), start = c(2013, 8), frequency = 12)  
testsku1 <- ts( c(16000, 48000, 32000, 16000, 48000, 64000, 111900, 48000, 16000, 62900, 31300, 32000), start = c(2017, 8), frequency = 12)
trainsku2 <- ts( c(56250, 90000, 108900, 96000, 0, 0, 0, 86400, 32400, 43200, 162000, 216000, 64800, 97200, 75600, 75600, 64800, 64800, 0, 0, 0, 0, 108000, 54000, 0, 0, 43200, 43200, 0, 0, 43200, 43200, 43200, 0, 108000, 43200, 43200), start = c(2014, 6), frequency = 12)
testsku2 <- ts( c(54000, 43200, 43200, 0, 0, 97200, 0, 54000, 0, 54000, 129600, 0, start = c(2017, 7), frequency = 12)

更改trainlstsample的定义,testlstsample嵌套列表:

trainlstsample <- list(list(trainsku1), list(trainsku2))
testlstsample <- list(list(testsku1), list(testsku2))

然后,您可以将pmap调用嵌套在循环访问这些map2调用中:

map2( .x = trainlstsample, .y = testlstsample,
~pmap(list(fcns, .x, .y, 12), mape_fcn) )
# [[1]]
# [[1]][[1]]
# [1] 0.4552366
# [[1]][[2]]
# [1] 0.364658
# [[1]][[3]]
# [1] 0.4256295
# [[2]]
# [[2]][[1]]
# [1] 0.7338271
# [[2]][[2]]
# [1] 1.055283
# [[2]][[3]]
# [1] 0.6990185

最新更新