Convert epoch time dataframe with index as epoch time to xts object as accepted by ts object for forecasting

旧街凉风 提交于 2019-12-12 06:44:21

问题


I have a dataframe in R of the form:

              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    

It has epoch times and values. Epoch times are row.names in this dataframe. Now, I want to convert this dataframe into ts() as understood by ARIMA. So, what I figured out now is ; I should convert this dataframe zoo or xts so that my epoch times are converted to datetime and then convert it into timeseries object as recognised by arima for forecasting. Also, I have found one link https://stats.stackexchange.com/questions/64383/what-is-the-best-forecasting-model-for-time-series-data where this data also has epoch timethen they converted it into xts and then to ts object. But in my case, epoch time is index column or row.names, How do i do that in my case? Here, the frequency is of data is 1 hour.

input is dataframe as given above

I want to convert it into something like :

                           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

where index is proper timestamp based on above epoch time. Then I will call ts() on this dataframe.

I tried to do this using :

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

** Edits : What I figured out is xts is taken by arima modelling and it works. Now my concern is from the above line of code is it able to understand that my data is per hour data or I need to do something like below to make it understand that it is a per hour data:

attr(xts_dataframe, 'frequency') <- 24

** I have this doubt because : Case 1 : When I run arima on dataframe like :

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

The traces produced are some what like this :

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

Best model is (4,1,1) and the forecast produced is like this:

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

Clearly epoch times are separated by an hour. (but these epoch times produced are very less than as compared to initial dataset; these epoch times belong to 1970 when i converted and saw it)

Case 2:When I run arima on dataframe like :

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

 attr(xts_dataframe, 'frequency') <- 24

The traces produced looks like this :

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

Best model is again (4,1,1) and the forecast produced looks like below:

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

In this case, how do I convert the dates to make sure these are future timestamps?

Please confirm which way is correct though I'm getting same results from both.

来源:https://stackoverflow.com/questions/45052353/convert-epoch-time-dataframe-with-index-as-epoch-time-to-xts-object-as-accepted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!