xts 在转换为 xts 时创建额外的一天

xts creates an additional day when converting to xts

我有一些类似如下的时间序列数据:

           AAPL.Close MSFT.Close GOOG.Close INTC.Close NVDA.Close
2020-01-06     299.80     159.03    1394.21      59.93    237.060
2020-01-07     298.39     157.58    1393.34      58.93    239.930
2020-01-08     303.19     160.09    1404.32      58.97    240.380

数据已经是xts格式,结构如下:

An ‘xts’ object on 2020-01-06/2020-04-03 containing:
  Data: num [1:63, 1:5] 300 298 303 310 310 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "AAPL.Close" "MSFT.Close" "GOOG.Close" "INTC.Close" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
List of 4
 $ from   : chr "20200105  18:04:21"
 $ to     : chr "20200405  18:04:21"
 $ src    : chr "IB"
 $ updated: POSIXct[1:1], format: "2020-04-05 19:04:22.20857"

然而,我想放入的结构与以下内容更密切相关:

An ‘xts’ object on 2020-01-06/2020-04-02 containing:
  Data: num [1:62, 1:5] 300 298 303 310 310 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "AAPL.Close" "MSFT.Close" "GOOG.Close" "INTC.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2020-04-05 16:30:35"

当我尝试以下操作时:

dat2 <- xts(dat, order.by = as.Date(time(dat)))

结构变成这样:

An ‘xts’ object on 2020-01-05/2020-04-02 containing:
  Data: num [1:63, 1:5] 300 298 303 310 310 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "AAPL.Close" "MSFT.Close" "GOOG.Close" "INTC.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

但是,它 "creates" 一个新的时间段。当我将原来的 dat 与新的 dat2 进行比较时,我发现它现在从 2020-01-05 开始,而之前它从 2020-01-06.

开始
> head(dat)
           AAPL.Close MSFT.Close GOOG.Close INTC.Close NVDA.Close
2020-01-06     299.80     159.03    1394.21      59.93     237.06
2020-01-07     298.39     157.58    1393.34      58.93     239.93
2020-01-08     303.19     160.09    1404.32      58.97     240.38
2020-01-09     309.63     162.09    1419.83      59.30     243.02
2020-01-10     310.33     161.34    1429.73      58.94     244.32
2020-01-13     316.96     163.28    1439.23      59.59     251.98
> head(dat2)
           AAPL.Close MSFT.Close GOOG.Close INTC.Close NVDA.Close
2020-01-05     299.80     159.03    1394.21      59.93     237.06
2020-01-06     298.39     157.58    1393.34      58.93     239.93
2020-01-07     303.19     160.09    1404.32      58.97     240.38
2020-01-08     309.63     162.09    1419.83      59.30     243.02
2020-01-09     310.33     161.34    1429.73      58.94     244.32
2020-01-12     316.96     163.28    1439.23      59.59     251.98

如何更正 xts 函数以获得正确的 xts 对象,而不是将数据移回一天。

数据:

dat <- structure(c(299.8, 298.39, 303.19, 309.63, 310.33, 316.96, 312.68, 
311.34, 315.24, 318.73, 316.57, 317.7, 319.23, 318.31, 308.95, 
317.69, 324.34, 323.87, 309.51, 308.66, 318.85, 321.45, 325.21, 
320.03, 321.55, 319.61, 327.2, 324.87, 324.95, 319, 323.62, 320.3, 
313.05, 298.18, 288.08, 292.65, 273.52, 273.36, 298.81, 289.32, 
302.74, 292.92, 289.03, 266.17, 285.34, 275.43, 248.23, 277.97, 
242.21, 252.86, 246.67, 244.78, 229.24, 224.37, 246.88, 245.52, 
258.44, 247.74, 254.81, 254.29, 240.91, 244.93, 241.41, 159.03, 
157.58, 160.09, 162.09, 161.34, 163.28, 162.13, 163.18, 166.17, 
167.1, 166.5, 165.7, 166.72, 165.04, 162.28, 165.46, 168.04, 
172.78, 170.23, 174.38, 180.12, 179.9, 183.63, 183.89, 188.7, 
184.44, 184.71, 183.71, 185.35, 187.23, 187.28, 184.42, 178.59, 
170.89, 168.07, 170.17, 158.18, 162.01, 172.79, 164.51, 170.55, 
166.27, 161.57, 150.62, 160.92, 153.63, 139.06, 158.83, 135.42, 
146.57, 140.4, 142.71, 137.35, 135.98, 148.34, 146.92, 156.11, 
149.7, 160.23, 157.71, 152.11, 155.26, 153.83, 1394.21, 1393.34, 
1404.32, 1419.83, 1429.73, 1439.23, 1430.88, 1439.2, 1451.7, 
1480.39, 1484.4, 1485.95, 1486.65, 1466.71, 1433.9, 1452.56, 
1458.63, 1455.84, 1434.23, 1485.94, 1447.07, 1448.23, 1476.23, 
1479.23, 1508.68, 1508.79, 1518.27, 1514.66, 1520.74, 1519.67, 
1526.69, 1518.15, 1485.11, 1421.59, 1388.45, 1393.18, 1318.09, 
1339.33, 1389.11, 1341.39, 1386.52, 1319.04, 1298.41, 1215.56, 
1280.39, 1215.41, 1114.91, 1219.73, 1084.33, 1119.8, 1096.8, 
1115.29, 1072.32, 1056.62, 1134.46, 1102.49, 1161.75, 1110.71, 
1146.82, 1162.81, 1105.62, 1120.84, 1097.88, 59.93, 58.93, 58.97, 
59.3, 58.94, 59.59, 59.43, 58.94, 59.66, 59.6, 60.55, 62.73, 
63.32, 68.47, 65.69, 67.31, 66.33, 66.47, 63.93, 64.42, 65.46, 
67.34, 67.09, 66.02, 66.39, 67.41, 67.46, 67.44, 67.27, 66.14, 
67.11, 65.45, 64.34, 61.76, 59.73, 59.65, 55.83, 55.52, 58.18, 
55.97, 58.68, 56.96, 55.77, 50.85, 53.98, 51.66, 45.54, 54.43, 
44.61, 50.08, 47.61, 45.94, 45.83, 49.58, 52.4, 51.26, 55.54, 
52.37, 55.49, 54.12, 51.88, 54.35, 54.13, 237.06, 239.93, 240.38, 
243.02, 244.32, 251.98, 247.28, 245.57, 248.93, 249.28, 247.94, 
250.05, 252.86, 250.48, 240.2, 247.97, 245.54, 245.81, 236.43, 
240.33, 247.13, 250.76, 254.245, 251.59, 262.97, 267.89, 272.54, 
270.78, 289.79, 296.57, 314.7, 308.7, 294.07, 273.28, 262.05, 
267.65, 252.6, 270.07, 276.43, 265.89, 284.51, 273.29, 266.04, 
245.44, 261.08, 246.47, 216.31, 240.84, 196.4, 217.27, 202.82, 
212.97, 205.75, 212.69, 249.18, 245.62, 257.24, 252.73, 265.59, 
263.6, 243.07, 255.47, 243.91), .Dim = c(63L, 5L), .Dimnames = list(
    NULL, c("AAPL.Close", "MSFT.Close", "GOOG.Close", "INTC.Close", 
    "NVDA.Close")), index = structure(c(1578265200, 1578351600, 
1578438000, 1578524400, 1578610800, 1578870000, 1578956400, 1579042800, 
1579129200, 1579215600, 1579561200, 1579647600, 1579734000, 1579820400, 
1580079600, 1580166000, 1580252400, 1580338800, 1580425200, 1580684400, 
1580770800, 1580857200, 1580943600, 1581030000, 1581289200, 1581375600, 
1581462000, 1581548400, 1581634800, 1581980400, 1582066800, 1582153200, 
1582239600, 1582498800, 1582585200, 1582671600, 1582758000, 1582844400, 
1583103600, 1583190000, 1583276400, 1583362800, 1583449200, 1583708400, 
1583794800, 1583881200, 1583967600, 1584054000, 1584313200, 1584399600, 
1584486000, 1584572400, 1584658800, 1584918000, 1585004400, 1585090800, 
1585177200, 1585263600, 1585519200, 1585605600, 1585692000, 1585778400, 
1585864800), tzone = "", tclass = c("POSIXct", "POSIXt")), .indexTZ = "", class = c("xts", 
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", 
"POSIXt"), tzone = "", from = "20200105  18:04:21", to = "20200405  18:04:21", src = "IB", updated = structure(1586106262.20857, class = c("POSIXct", 
"POSIXt")))

几乎总是 是由于从 Date 翻转到 POSIXct,而您的本地时区与 UTC 不同。 Date 将变为 UTC 午夜 ,对于 POSIXct,反映北美时区,变为前一天。

例子
R> as.Date("1970-01-01")
[1] "1970-01-01"
R> as.POSIXct(as.Date("1970-01-01"))
[1] "1969-12-31 18:00:00 CST"
R> 

如果您的数据是(业务或市场)每日数据,您可以将 Datexts 一起使用并避免该问题。