在R中转换时间序列预测的输出格式

在R中转换时间序列预测的输出格式,r,format,forecast,R,Format,Forecast,我正在研究两种产品的时间序列数据——从2016年1月到2019年12月的X1和X2,并在其上应用NNAR预测模型。代码如下: nnar.Accounts_ts = ts(df, start = c(2016, 1), frequency = 12) nnar.Accounts_ts V1 V2 Jan 2016 2792 8882 Feb 2016 3317 10803 Mar 2016 4292 14059 Apr 2016 4500 1561

我正在研究两种产品的时间序列数据——从2016年1月到2019年12月的X1和X2,并在其上应用NNAR预测模型。代码如下:

nnar.Accounts_ts = ts(df, start = c(2016, 1), frequency = 12)
nnar.Accounts_ts
            V1     V2
Jan 2016  2792   8882
Feb 2016  3317  10803
Mar 2016  4292  14059
Apr 2016  4500  15617
May 2016  5234  19211
Jun 2016  6657  23632
Jul 2016  6329  25435
Aug 2016  7208  30671
Sep 2016  7046  32429
Oct 2016  7242  35794
Nov 2016  7692  39138
Dec 2016  7860  43767
Jan 2017  6941  42172
Feb 2017  7076  40690
Mar 2017  8943  50362
Apr 2017  8435  50890
May 2017  9757  59852
Jun 2017  9510  62762
Jul 2017  8665  64176
Aug 2017  9538  70739
Sep 2017  8832  69643
Oct 2017  9983  77886
Nov 2017  9541  79059
Dec 2017  9397  82658
Jan 2018 10350  90879
Feb 2018  9853  84161
Mar 2018 12472  98436
Apr 2018 11942 101095
May 2018 12706 109782
Jun 2018 11733 108488
Jul 2018 11114 114713
Aug 2018 12731 122221
Sep 2018 10750 114816
Oct 2018 12319 129158
Nov 2018 12391 127707
Dec 2018 12442 132581
Jan 2019 14218 143658
Feb 2019 13628 131456
Mar 2019 15629 149794
Apr 2019 16457 157845
May 2019 16880 166019
Jun 2019 15362 160458
Jul 2019 15509 175690
Aug 2019 16195 178887
Sep 2019 14645 173253
Oct 2019 16930 189340
Nov 2019 16586 181478
Dec 2019 16520 199305

set.seed(54321)
Brand_nnar_Accounts_forecast <- lapply(nnar.Accounts_ts, function(x) forecast(nnetar(x, PI = TRUE),h = 30))
我想把它转换成以下格式

Jan 2020  Feb 2020  Mar 2020  Apr 2020  May 2020  Jun 2020  Jul 2020  Aug 2020  Sep 2020  Oct 2020  Nov 2020
X1  17055.65  16935.27  17235.31  17290.44  17310.37  17215.72  17227.75  17276.04  17134.86  17311.74  17297.26
X2 208483.04 187984.26 220114.30 225257.26 235741.04 225815.39 248283.10 248211.23 241011.62 261533.17 249032.70
    Dec 2020  Jan 2021  Feb 2021  Mar 2021  Apr 2021  May 2021  Jun 2021 Jul 2021  Aug 2021  Sep 2021  Oct 2021  Nov 2021
X1  17293.96  17317.46  17312.78  17324.23  17326.22  17326.92  17323.58  17324.0  17325.72  17320.61  17326.95  17326.47
X2 270361.60 275791.92 254342.50 286256.43 286638.89 292843.56 286383.42 298840.4 298059.78 294739.42 303357.43 298140.34
    Dec 2021  Jan 2022  Feb 2022 Mar 2022  Apr 2022 May 2022  Jun 2022
X1  17326.35  17327.17  17327.01  17327.4  17327.47  17327.5  17327.38
X2 305902.18 307070.02 300128.09 309649.4 309460.55 310678.2 309377.82                                                      

NNAR模型具有嵌入式列表,当我使用建议的解决方案转换格式时,这会产生问题。一个选项是在转换为
xts
后,将
列表中的元素转置,然后执行
rbind

library(xts)
`row.names<-`(do.call(rbind, lapply(lst1, function(x) t(as.xts(x)))), names(lst1))
#      Jan 2020 Feb 2020 Mar 2020 Apr 2020 May 2020 Jun 2020 Jul 2020 Aug 2020 Sep 2020
#Product 1       41       56        2       16       78       60       89       31       68
#Product 2       52       23       57       48       80       53       63       36       10
#          Oct 2020 Nov 2020 Dec 2020 Jan 2021 Feb 2021 Mar 2021 Apr 2021 May 2021 Jun 2021
#Product 1       73        2       40       45       11       43       63       58       29
#Product 2       24       24       39        4       56       85        6       20       69
#          Jul 2021 Aug 2021 Sep 2021 Oct 2021 Nov 2021 Dec 2021
#Product 1       39       76        8       89       14        3
#Product 2       93       26       23       95       79       56
库(xts)

`row.names请使用
dput
显示示例感谢您的输入。您的解决方案正在列表上运行,但我的解决方案是预测模型(nnar-nnetar)的输出,并获得错误消息as-error in UseMethod(“as.xts”):没有适用于“as.xts”类对象的“as.xts”方法@jayanandshiragavi请使用小示例的
dput
更新您的帖子,以便其他人能够正确获得结构
library(xts)
`row.names<-`(do.call(rbind, lapply(lst1, function(x) t(as.xts(x)))), names(lst1))
#      Jan 2020 Feb 2020 Mar 2020 Apr 2020 May 2020 Jun 2020 Jul 2020 Aug 2020 Sep 2020
#Product 1       41       56        2       16       78       60       89       31       68
#Product 2       52       23       57       48       80       53       63       36       10
#          Oct 2020 Nov 2020 Dec 2020 Jan 2021 Feb 2021 Mar 2021 Apr 2021 May 2021 Jun 2021
#Product 1       73        2       40       45       11       43       63       58       29
#Product 2       24       24       39        4       56       85        6       20       69
#          Jul 2021 Aug 2021 Sep 2021 Oct 2021 Nov 2021 Dec 2021
#Product 1       39       76        8       89       14        3
#Product 2       93       26       23       95       79       56
lst1 <- list(`Product 1` = ts(sample(100, 24, replace = TRUE), start = c(2020, 1),
    frequency  =12), `Product 2` = ts(sample(100, 24, replace = TRUE),
    start = c(2020, 1), frequency  =12))