将索引作为纪元时间的纪元时间数据帧转换为ts对象接受的xts对象,以进行预测

时间:2017-07-12 08:28:02

标签: r datetime time-series xts epoch

我在表格R中有一个数据框:

              y
1484337600  19.22819            
1484341200  19.28906            
1484344800  19.28228            
1484348400  19.21669            
1484352000  19.32759            
1484355600  19.21833            
1484359200  19.20626            
1484362800  19.28737            
1484366400  19.20651            
1484370000  19.18424    

它有时代和价值观。大纪元时间是此数据帧中的row.names。现在,我希望将此数据帧转换为ARIMA所理解的ts()。所以,我现在想到的是;我应该转换此数据帧zoo或xts,以便将我的纪元时间转换为日期时间,然后将其转换为时间序列对象,由arima识别以进行预测。此外,我找到了一个链接https://stats.stackexchange.com/questions/64383/what-is-the-best-forecasting-model-for-time-series-data,其中这些数据也具有纪元时间,然后将它们转换为xts然后转换为ts对象。但在我的情况下,纪元时间是索引列或row.names,我怎么做我的情况?这里的频率是1小时。

输入是上面给出的数据帧

我想将其转换为:

                           y
2017-01-13 13:00:00 19.28906
2017-01-13 14:00:00 19.28228
2017-01-13 15:00:00 19.21669
2017-01-13 16:00:00 19.32759
2017-01-13 17:00:00 19.21833
2017-01-13 18:00:00 19.20626
2017-01-13 19:00:00 19.28737
2017-01-13 20:00:00 19.20651
2017-01-13 21:00:00 19.18424
2017-01-13 22:00:00 19.25841
2017-01-13 23:00:00 19.15664
2017-01-14 00:00:00 19.16237

其中index是基于上述纪元时间的正确时间戳。然后我会在这个数据帧上调用ts()。

我尝试使用以下方法执行此操作:

xts_dataframe <- xts(x = dataframe$y, order.by = as.POSIXct(as.numeric(row.names(dataframe)), origin="1970-01-01"))

**编辑: 我发现xts是由arima建模得到的,它可以工作。现在我关注的是上面的代码行是能够理解我的数据是每小时数据还是我需要做类似下面的事情来让它理解它是每小时的数据:

attr(xts_dataframe, 'frequency') <- 24

**我有这个疑问,因为:  案例1:当我在数据框架上运行arima时:

xts_dataframe <- xts(x = dataframe$y, order.by = as.POSIXct(as.numeric(row.names(dataframe)), origin="1970-01-01"))

产生的痕迹是这样的:

ARIMA(0,1,0)                    : 1054.432
 ARIMA(0,1,0) with drift         : 1056.433
 ARIMA(0,1,1)                    : -22.90881
 ARIMA(0,1,1) with drift         : -20.90774
 ARIMA(0,1,2)                    : -21.05988
 ARIMA(0,1,2) with drift         : -19.05796
 ARIMA(0,1,3)                    : -130.5101
 ARIMA(0,1,3) with drift         : -128.5064
 ARIMA(0,1,4)                    : -667.7424
 ARIMA(0,1,4) with drift         : -665.7444
 ARIMA(0,1,5)                    : -669.2482

最佳模型是(4,1,1),所产生的预测是这样的:

Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
15552001       18.86242 18.58299 19.14186 18.43507 19.28978
15555601       18.79712 18.48352 19.11072 18.31751 19.27673
15559201       18.94144 18.60992 19.27296 18.43443 19.44845
15562801       18.85743 18.47365 19.24120 18.27049 19.44436
15566401       18.84522 18.45196 19.23847 18.24378 19.44665
15570001       18.91105 18.51072 19.31139 18.29879 19.52331
15573601       18.86002 18.44583 19.27420 18.22657 19.49346
15577201       18.86466 18.44761 19.28172 18.22683 19.50249
15580801       18.89258 18.47185 19.31331 18.24913 19.53603
15584401       18.86414 18.43775 19.29054 18.21202 19.51626
15588001       18.87189 18.44333 19.30045 18.21646 19.52732
15591601       18.88261 18.45075 19.31446 18.22214 19.54307
15595201       18.86782 18.43197 19.30367 18.20125 19.53440
15598801       18.87426 18.43585 19.31267 18.20377 19.54475

显然,纪元时间相隔一小时。 (但是这些时代产生的时间比初始数据集少得多;这些时代属于1970年,当我转换并看到它时)

案例2:当我在数据框架上运行arima时,如:

xts_dataframe <- xts(x = dataframe$y, order.by = as.POSIXct(as.numeric(row.names(dataframe)), origin="1970-01-01"))

 attr(xts_dataframe, 'frequency') <- 24

产生的痕迹如下所示:

ARIMA(0,1,0)                               : 1054.44
 ARIMA(0,1,0)            with drift         : 1056.442
 ARIMA(0,1,0)(0,0,1)[24]                    : 602.822
 ARIMA(0,1,0)(0,0,1)[24] with drift         : 604.8247
 ARIMA(0,1,0)(0,0,2)[24]                    : 383.8319
 ARIMA(0,1,0)(0,0,2)[24] with drift         : 385.8355
 ARIMA(0,1,0)(1,0,0)[24]                    : 389.8581
 ARIMA(0,1,0)(1,0,0)[24] with drift         : 391.8609
 ARIMA(0,1,0)(1,0,1)[24]                    : Inf
 ARIMA(0,1,0)(1,0,1)[24] with drift         : Inf
 ARIMA(0,1,0)(1,0,2)[24]                    : Inf
 ARIMA(0,1,0)(1,0,2)[24] with drift         : Inf
 ARIMA(0,1,0)(2,0,0)[24]                    : 134.4009
 ARIMA(0,1,0)(2,0,0)[24] with drift         : 136.4046
 ARIMA(0,1,0)(2,0,1)[24]                    : Inf
 ARIMA(0,1,0)(2,0,1)[24] with drift         : Inf
 ARIMA(0,1,0)(2,0,2)[24]                    : Inf
 ARIMA(0,1,0)(2,0,2)[24] with drift         : Inf
 ARIMA(0,1,1)                               : -22.90074
 ARIMA(0,1,1)            with drift         : -20.89968
 ARIMA(0,1,1)(0,0,1)[24]                    : -280.0375
 ARIMA(0,1,1)(0,0,1)[24] with drift         : -278.0347
 ARIMA(0,1,1)(0,0,2)[24]                    : -422.9901
 ARIMA(0,1,1)(0,0,2)[24] with drift         : -420.9862
 ARIMA(0,1,1)(1,0,0)[24]                    : -373.8774
 ARIMA(0,1,1)(1,0,0)[24] with drift         : -371.8742
 ARIMA(0,1,1)(1,0,1)[24]                    : Inf
 ARIMA(0,1,1)(1,0,1)[24] with drift         : Inf
 ARIMA(0,1,1)(1,0,2)[24]                    : Inf
 ARIMA(0,1,1)(1,0,2)[24] with drift         : Inf
 ARIMA(0,1,1)(2,0,0)[24]                    : -551.472
 ARIMA(0,1,1)(2,0,0)[24] with drift         : -549.4676
 ARIMA(0,1,1)(2,0,1)[24]                    : Inf
 ARIMA(0,1,1)(2,0,1)[24] with drift         : Inf
 ARIMA(0,1,1)(2,0,2)[24]                    : Inf
 ARIMA(0,1,1)(2,0,2)[24] with drift         : Inf
 ARIMA(0,1,2)                               : -21.0519
 ARIMA(0,1,2)            with drift         : -19.04998
 ARIMA(0,1,2)(0,0,1)[24]                    : -280.6503
 ARIMA(0,1,2)(0,0,1)[24] with drift         : -278.6467
 ARIMA(0,1,2)(0,0,2)[24]                    : -423.2597

最佳模型又是(4,1,1),预测产生如下:

Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
181.0000       18.86242 18.58299 19.14186 18.43507 19.28978
181.0417       18.79712 18.48352 19.11072 18.31751 19.27673
181.0833       18.94144 18.60992 19.27296 18.43443 19.44845
181.1250       18.85743 18.47365 19.24120 18.27049 19.44436
181.1667       18.84522 18.45196 19.23847 18.24378 19.44665
181.2083       18.91105 18.51072 19.31139 18.29879 19.52331
181.2500       18.86002 18.44583 19.27420 18.22657 19.49346
181.2917       18.86466 18.44761 19.28172 18.22683 19.50249
181.3333       18.89258 18.47185 19.31331 18.24913 19.53603
181.3750       18.86414 18.43775 19.29054 18.21202 19.51626
181.4167       18.87189 18.44333 19.30045 18.21646 19.52732
181.4583       18.88261 18.45075 19.31446 18.22214 19.54307
181.5000       18.86782 18.43197 19.30367 18.20125 19.53440
181.5417       18.87426 18.43585 19.31267 18.20377 19.54475
181.5833       18.87769 18.43584 19.31955 18.20193 19.55346
181.6250       18.87046 18.42500 19.31592 18.18919 19.55174
181.6667       18.87480 18.42638 19.32322 18.18900 19.56060
181.7083       18.87544 18.42353 19.32735 18.18430 19.56658
181.7500       18.87212 18.41677 19.32746 18.17573 19.56851
181.7917       18.87472 18.41623 19.33321 18.17352 19.57592
181.8333       18.87448 18.41257 19.33638 18.16806 19.58090
181.8750       18.87306 18.40784 19.33827 18.16157 19.58454
181.9167       18.87450 18.40610 19.34290 18.15814 19.59085
181.9583       18.87410 18.40239 19.34581 18.15268 19.59551
182.0000       18.87355 18.39862 19.34848 18.14721 19.59989

在这种情况下,如何转换日期以确保这些是未来的时间戳?

请确认哪种方式正确,但我从两者都得到相同的结果。

0 个答案:

没有答案