R中的时间序列未获得预期的索引日期格式

时间:2018-12-02 20:04:08

标签: r

我正在尝试将我的数据设置为时间序列,但是它没有按预期进行。我有一个日期列和一些变量。

我使用以下行:

library(xts)
data2 <- xts(x=data, order.by=weeks)

将其设置为xts对象,但索引列看起来像下面的X2008.03.03,稍后会引起问题。

E.G。这个plot(as.numeric(data$UNITS.y))不会产生预期的线图,并且在x轴上显示日期。

数据1:

weeks <- structure(c(14557, 14998, 15166, 15250, 14165, 14963, 14333, 
14515, 14613, 14550, 15208, 15243, 14641, 14753, 14088, 15285, 
15166, 15453, 14837, 14004, 14788, 15579, 14466, 14592, 14858, 
15600, 15068, 14851, 14305, 14501, 15362, 14319, 14480, 14165, 
15054, 14368, 14368, 14522, 14137, 14368, 14907, 14431, 15089, 
15068, 15292, 15054, 14354, 14900, 14599, 15579, 14522, 15614, 
14970, 14844, 13948, 14529, 13997, 14389, 15397, 14312, 15341, 
14893, 15138, 14557, 14942, 14480, 14424, 14767, 15579, 14403, 
15124, 14116, 14739, 14900, 14242, 14963, 14361, 15271, 13941, 
14438, 15495, 14102, 13997, 14620, 15327, 15516, 15362, 15579, 
14753, 14648, 15026, 15362, 14592, 15425, 14942, 14851, 14900, 
14368, 14725, 15334), class = "Date")

数据2:

data <- structure(list(WEEK = structure(c(14557, 14998, 15166, 15250, 
14165, 14963, 14333, 14515, 14613, 14550, 15208, 15243, 14641, 
14753, 14088, 15285, 15166, 15453, 14837, 14004, 14788, 15579, 
14466, 14592, 14858, 15600, 15068, 14851, 14305, 14501, 15362, 
14319, 14480, 14165, 15054, 14368, 14368, 14522, 14137, 14368, 
14907, 14431, 15089, 15068, 15292, 15054, 14354, 14900, 14599, 
15579, 14522, 15614, 14970, 14844, 13948, 14529, 13997, 14389, 
15397, 14312, 15341, 14893, 15138, 14557, 14942, 14480, 14424, 
14767, 15579, 14403, 15124, 14116, 14739, 14900, 14242, 14963, 
14361, 15271, 13941, 14438, 15495, 14102, 13997, 14620, 15327, 
15516, 15362, 15579, 14753, 14648, 15026, 15362, 14592, 15425, 
14942, 14851, 14900, 14368, 14725, 15334), class = "Date"), UNITS.y = c(20L, 
54L, 58L, 45L, 37L, 20L, 230L, 1L, 11L, 18L, 87L, 5L, 1L, 17L, 
5L, 1L, 13L, 363L, 591L, 1671L, 16L, 49L, 64L, 7L, 17L, 21L, 
6L, 172L, 52L, 165L, 59L, 2L, 17L, 255L, 51L, 3L, 123L, 174L, 
819L, 212L, 64L, 12L, 5L, 4L, 436L, 1320L, 1394L, 965L, 6L, 1242L, 
104L, 274L, 6L, 19L, 1457L, 1248L, 297L, 5L, 49L, 9L, 428L, 151L, 
345L, 171L, 12L, 940L, 9L, 26L, 113L, 988L, 20L, 317L, 29L, 511L, 
46L, 53L, 392L, 728L, 28L, 11L, 7L, 4L, 829L, 3L, 10L, 7L, 100L, 
363L, 43L, 182L, 22L, 726L, 38L, 2L, 2L, 761L, 24L, 66L, 6L, 
846L), COLUPC = c(17032200651, 2.29e+10, 15400010060, 12840005160, 
17100701094, 13913116891, 33913116503, 14369507125, 21200000826, 
13100018038, 2.29e+10, 13500076232, 18087800246, 13377601100, 
12708618335, 12840005160, 818491218170, 14900001463, 17764490040, 
14530000040, 37469007168, 13450063532, 11200081007, 12550006874, 
11707710532, 14190007171, 2.12e+10, 14470000240, 15150001700, 
13040076366, 21380013522, 13620002443, 22900000766, 17800008316, 
1.7047e+10, 13600067656, 14900002345, 14200096166, 37672116379, 
33663200660, 15010040230, 617852304010, 13700027785, 8.82e+12, 
14900005011, 14200096402, 17030002018, 12840008418, 13700008885, 
11200080997, 14148302952, 12840008314, 17032200342, 17800005216, 
14900002890, 37465300003, 24148302202, 24100000365, 17100701868, 
23663203528, 8819999845457, 17047018082, 24148302178, 31200000916, 
13700049657, 14800100643, 14900000935, 8839999825998, 22840008446, 
14900004255, 14148300099, 14300001425, 14200096454, 8839999824448, 
8829999881913, 17047000655, 35100006629, 8.1947e+11, 14900002934, 
17169000032, 17469004158, 11600028370, 8829999835202, 14148301805, 
13800042756, 13700024001, 17672140377, 22840007735, 1.7047e+10, 
12840008342, 17800008310, 12100002649, 14158100131, 23700006222, 
814105804056, 14900002934, 17469004705, 17047000668, 12430009001, 
12700038249), DOLLARS.y = c(10.4, 53.46, 433.92, 134.4, 123.56, 
101.8, 464.6, 2.5, 43.89, 18, 87, 14.95, 2.5, 50.15, 9.25, 3.99, 
50.57, 1863.77, 590.45, 1729, 34.93, 24.5, 234.24, 29.05, 67.83, 
61.79, 8.94, 254.48, 113.45, 870.35, 102.66, 3.99, 22.54, 718.26, 
30.77, 11.37, 364.08, 1213.47, 859.56, 105.9, 64, 83.4, 49.95, 
3.96, 378.13, 7029.61, 2080.96, 1915.18, 86.94, 2757.24, 131.55, 
823.39, 16.14, 83.41, 4167.99, 2221.66, 1006.83, 7.45, 165.62, 
7.11, 1535.44, 58.89, 707.14, 519.63, 43.08, 1398.81, 15.21, 
233.74, 225.51, 2080.64, 29.8, 773.48, 124.2, 1558.55, 95.22, 
36.54, 709.12, 654.69, 93.58, 15.29, 20.93, 9.56, 1245.75, 9.27, 
32.9, 65.03, 200.21, 712.29, 27.09, 542.36, 153.78, 1841.78, 
76, 7.98, 22.58, 1883.59, 59.22, 43.52, 20.94, 610.08)), row.names = c(515902L, 
870381L, 1003647L, 1071702L, 182652L, 839976L, 317699L, 483188L, 
561919L, 510735L, 1040310L, 1063188L, 590850L, 674413L, 127860L, 
1095261L, 1006376L, 1216422L, 744394L, 76411L, 702505L, 1298747L, 
432135L, 546899L, 759674L, 1308527L, 923808L, 752419L, 293463L, 
463316L, 1153643L, 308720L, 446621L, 180805L, 917577L, 345543L, 
348404L, 485693L, 164479L, 349182L, 802579L, 404074L, 939665L, 
923827L, 1104689L, 916110L, 333502L, 794421L, 549892L, 1295567L, 
486483L, 1321161L, 844189L, 749469L, 47191L, 496572L, 72581L, 
365068L, 1177218L, 300357L, 1140203L, 786420L, 984787L, 517942L, 
825162L, 441700L, 398683L, 682990L, 1296907L, 376751L, 973427L, 
148151L, 664039L, 795655L, 234153L, 842359L, 341468L, 1087636L, 
44416L, 410297L, 1239945L, 139889L, 72375L, 567908L, 1130255L, 
1253628L, 1154594L, 1295228L, 676598L, 592781L, 893501L, 1153045L, 
546038L, 1193357L, 822212L, 751525L, 791203L, 347851L, 652010L, 
1135337L), class = "data.frame")

编辑:

我得到的图: enter image description here

预期的输出(注意:这与我使用的数据不同)

enter image description here

我正在尝试绘制UNITS.y列,然后运行auto.arima(data)

1 个答案:

答案 0 :(得分:1)

您可以这样做:

data2 <- xts(x=data[,2], order.by=weeks)
head(data2)
plot(data2)

现在,data2如下所示:

             [,1]
2008-03-03     28
2008-03-10   1457
2008-04-28    297
2008-04-28    829
2008-05-05   1671
2008-07-28      5

这将产生以下情节:

enter image description here

希望有帮助。