如何将我的 ts 预测转换为具有日期值的数据框?
How can I turn my ts forecast into a dataframe with date values?
我正在使用 arima 开发一个需要导出到数据库的预测模型。我需要将我的预测转化为数据框。我已经以一种精心设计的方式这样做了,但我希望有一些更简单的东西,这样我就不必硬编码日期了。
为了获得预测,我创建了一个“适合”的模型,然后 运行 fcst1 <- forecast(fit, 4)
以获得四个月的价值,例如。
这是我打印预测时的结果格式:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 2021 x
Feb 2021 x
Mar 2021 x
Apr 2021 x
我只想要点预测,我知道我可以从 forecast$mean 获得它,但我还想要日期列。我还将多个预测(针对不同的细分市场)合并为一个 table,所以目前我可以通过这样做得到我的结果:
months <- c('Jan-2021', 'Feb-2021', 'Mar-2021', 'Apr-2021')
forecasts <- as.data.frame(rbind(cbind(months, fcst1$mean, "Segment 1"),
cbind(months, fcst2$mean, "Segment 2"),
cbind(months, fcst3$mean, "Segment 3"),
cbind(months, fcst4$mean, "Segment 4"),
cbind(months, fcst5$mean, "Segment 5"),
cbind(months, fcst6$mean, "Segment 6"),
cbind(months, fcst7$mean,"Segment 7")))
它给我的预测结果分为三列:月份、预测和细分。
months fcst$mean "Segment"
1 Sep-2020 2 Segment 1
2 Oct-2020 1 Segment 1
3 Nov-2020 3 Segment 1
4 Dec-2020 2 Segment 1
但是,为了在将来重复table,如果我在几个月内不必硬编码,那将是理想的,它可以从预测中提取它(因为它已经无论如何)。有没有办法从 ts 对象中提取日期作为数据框列?
rownames(fcst1) returns as null 我一直无法在互联网上找到这个问题的答案。提前致谢!
*注意:日期为年月格式
如果对象已经在全局环境中创建,使用mget
获取list
中的值
Forecast <- sapply(mget(ls(pattern = '^fcst\d+$')), function(x) x$mean)
现在,我们用data.frame
构造一个data.frame
data.frame(months, Forecast, Segment = paste0("Segment", seq_along(Forecast))))
要从 forecast
对象自动获取月份,请使用 index
outlst <- lapply(mget(ls(pattern = '^fcst\d+$')), function(x)
data.frame(months = index(x), Forecast = x$mean))
do.call(rbind, Map(cbind, outlst, Segment = paste("Segment", seq_along(outlst))))
我正在使用 arima 开发一个需要导出到数据库的预测模型。我需要将我的预测转化为数据框。我已经以一种精心设计的方式这样做了,但我希望有一些更简单的东西,这样我就不必硬编码日期了。
为了获得预测,我创建了一个“适合”的模型,然后 运行 fcst1 <- forecast(fit, 4)
以获得四个月的价值,例如。
这是我打印预测时的结果格式:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 2021 x
Feb 2021 x
Mar 2021 x
Apr 2021 x
我只想要点预测,我知道我可以从 forecast$mean 获得它,但我还想要日期列。我还将多个预测(针对不同的细分市场)合并为一个 table,所以目前我可以通过这样做得到我的结果:
months <- c('Jan-2021', 'Feb-2021', 'Mar-2021', 'Apr-2021')
forecasts <- as.data.frame(rbind(cbind(months, fcst1$mean, "Segment 1"),
cbind(months, fcst2$mean, "Segment 2"),
cbind(months, fcst3$mean, "Segment 3"),
cbind(months, fcst4$mean, "Segment 4"),
cbind(months, fcst5$mean, "Segment 5"),
cbind(months, fcst6$mean, "Segment 6"),
cbind(months, fcst7$mean,"Segment 7")))
它给我的预测结果分为三列:月份、预测和细分。
months fcst$mean "Segment"
1 Sep-2020 2 Segment 1
2 Oct-2020 1 Segment 1
3 Nov-2020 3 Segment 1
4 Dec-2020 2 Segment 1
但是,为了在将来重复table,如果我在几个月内不必硬编码,那将是理想的,它可以从预测中提取它(因为它已经无论如何)。有没有办法从 ts 对象中提取日期作为数据框列?
rownames(fcst1) returns as null 我一直无法在互联网上找到这个问题的答案。提前致谢!
*注意:日期为年月格式
如果对象已经在全局环境中创建,使用mget
获取list
Forecast <- sapply(mget(ls(pattern = '^fcst\d+$')), function(x) x$mean)
现在,我们用data.frame
构造一个data.frame
data.frame(months, Forecast, Segment = paste0("Segment", seq_along(Forecast))))
要从 forecast
对象自动获取月份,请使用 index
outlst <- lapply(mget(ls(pattern = '^fcst\d+$')), function(x)
data.frame(months = index(x), Forecast = x$mean))
do.call(rbind, Map(cbind, outlst, Segment = paste("Segment", seq_along(outlst))))