r - 使用 R 预测多个时间序列

标签 r time-series forecast

考虑一个随机的 data.frame:
d <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))
我想将每一行视为一个独特的时间序列(在本例中为十年)。所以首先,我需要将数据转换为时间序列。我尝试了以下代码:
d1 <- ts(d, start=2000, end=2009)
但是,我认为这段代码将时间序列视为一个长达 100 年的长时间序列。就我而言,我想要 1000 个独特的时间序列,持续 10 年。

然后我想预测每 1,000 个时间序列(假设 1 年)。通过使用以下代码:
fit <- tslm(d1~trend) fcast <- forecast(fit, h=1) plot(fcast)
我得到一个预测(因为我在我的数据集 d1 中只考虑一个时间序列)。

谁能帮我这个?

最佳答案

如果我们正在寻找为每一列创建时间序列,那么使用 lapply 遍历数据集的列。并创建它

library(forecast)
lst1 <- lapply(d, ts, start = 2000, end = 2009)
#If we want to split by `row`
#lst1 <- lapply(asplit(as.matrix(d), 1), ts, start = 2000, end = 2009)
par(mfrow = c(5, 2))
lapply(lst1, function(x) {
        fit <- tslm(x ~ trend)
        fcast <- forecast(fit, h = 1)
        plot(fcast)
   })

enter image description here

关于r - 使用 R 预测多个时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58327544/

相关文章:

python - 如何在python 3.7中安装金字塔arima

python - 有没有办法像使用 SARIMAX 模型一样使用 LSTM 来预测具有外生变量的时间序列?

r - 在 R 中,我如何使用贪婪的位点选择算法来最大化未代表的物种丰富度?

python - 在 Bokeh Timeseries 图中,如何指定 line_width?

r - 如何将循环的迭代值保存在 R 中的向量中?

pandas - 如何将 unix 纪元转换为高度准确的时间戳 [pandas]?

database - BTrDB 连接失败

python - sklearn 的 TimeSeriesSplit 仅支持单步预测范围是否有原因?

linux - 从linux运行r错误: cannot open the connection

r - 全局环境数据集汇总表