0

我有一个 xts 对象,对其执行回归,提取每列斜率系数的 t 值,然后在 5 个周期间隔内滚动应用。但是,由于某种原因,日期/时间索引值在 rollapply 之后从 POSIXct 更改为基于秒的时间戳。我需要它不更改或更改回 POSIXct ......有什么想法吗?

> class(w)
[1] "xts" "zoo"

> dput(w)
structure(c(41.8, 41.52, 41.95, 41.95, 41.58, 41.27, 41.02, 41.4, 
41.27, 41.51, 41.94, 42.27, 42.15, 41.5, 41.9, 41.04, 41.08, 
41.53, 41.21, 41.15, 41.82, 41.6, 41.8, 42.13, 41.44, 40.92, 
41, 41.15, 40.78, 40.155, 53.64, 53.42, 54.07, 54.14, 53.82, 
53.04, 52.89, 53.23, 53.17, 53.69, 54.58, 55.09, 54.88, 54.14, 
54.52, 53.35, 53.38, 53.7, 53.62, 53.58, 54.21, 53.69, 53.56, 
54.07, 53.26, 52.78, 52.8, 52.76, 52.5, 51.95, 91.89, 92.12, 
92.51, 92.46, 92.33, 92.07, 91.85, 92.35, 92.51, 92.93, 93.27, 
93.48, 93.43, 92.96, 92.98, 92.63, 92.55, 92.7, 92.48, 92.59, 
92.54, 92.41, 92.26, 92.4, 91.85, 91.55, 91.39, 91.42, 91.21, 
90.71, 108.45, 108.56, 108.13, 107.64, 107.99, 107.77, 108.03, 
108.13, 108.14, 108.17, 107.66, 106.98, 106.79, 107.24, 106.88, 
107.39, 107.16, 106.77, 107.45, 107.89, 107.53, 107.62, 107.88, 
107.39, 108.29, 108.77, 108.78, 108.85, 108.98, 109.01, 83.88, 
83.64, 84.21, 84.11, 83.58, 82.6, 82.47, 82.77, 82.1, 82.64, 
83.25, 84, 83.54, 81.85, 81.83, 81.42, 81.2, 81.53, 81.14, 81.63, 
82.49, 81.19, 81.73, 82.34, 80.34, 79.21, 79.38, 79.15, 78.7, 
77.24, 64.46, 64.65, 64.41, 64.69, 64.42, 64.14, 64.22, 64.23, 
63.95, 64.38, 64.68, 64.78, 65.39, 64.78, 64.55, 63.9, 63.94, 
63.66, 63.29, 64, 64.07, 64.5, 64.02, 64.16, 63.84, 63.19, 62.98, 
62.88, 62.68, 61.46, 124.25, 124.29, 122.74, 121.15, 122.17, 
121.95, 122.81, 123.66, 123.97, 123.69, 121.98, 120.85, 120.11, 
121.74, 120.96, 122.69, 121.48, 120.86, 122.64, 123.36, 122.35, 
122.26, 122.95, 121.79, 124, 125.84, 125.98, 126.19, 126.61, 
126.73), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "GMT", index = structure(c(1349136000, 
1349222400, 1349308800, 1349395200, 1349654400, 1349740800, 1349827200, 
1349913600, 1.35e+09, 1350259200, 1350345600, 1350432000, 1350518400, 
1350604800, 1350864000, 1350950400, 1351036800, 1351123200, 1351209600, 
1351641600, 1351728000, 1351814400, 1352073600, 1352160000, 1352246400, 
1352332800, 1352419200, 1352678400, 1352764800, 1352851200), tzone = "GMT", tclass =    c("POSIXct", 
"POSIXt")), .Dim = c(30L, 7L), .Dimnames = list(NULL, c("EEM", 
"EFA", "HYG", "IEF", "IWM", "IYR", "TLT")))

执行回归

x <- w
f <- function (x) {
  res <- coef(summary(lm(x ~ time(x))))
  sapply(res, "[" ,"time(x)"  ,"t value")
}

滚动应用超过 5 个周期

r <- rollapplyr(x, 5, f, by.column=FALSE)

日期/时间更改?

> last(r,5)    
       Response EEM Response EFA Response HYG Response IEF Response IWM Response     IYR Response TLT
1352332800    -1.318353    -1.663508    -3.328802     2.158261    -1.655036    -3.871996     2.349788
1352419200    -2.916606    -2.456015    -4.642054     2.706211    -3.326938    -4.142477     3.192453
1352678400    -2.117244    -2.981824    -3.803602     3.148467    -2.917112    -6.177137     3.751326
1352764800    -1.623254    -3.322795    -4.656230     3.228277    -3.103968    -4.505336     3.128703
1352851200    -1.278702    -2.681911    -2.102026     0.918324    -3.453862    -9.012422     1.074501

会话信息

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252        LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] quantmod_0.3-17 Defaults_1.1-1  TTR_0.21-1      xts_0.8-6       zoo_1.7-7      

loaded via a namespace (and not attached):
[1] grid_2.15.1    lattice_0.20-6 tools_2.15.1

添加 str()

> str(r)
‘zoo’ series from 1349654400 to 1352851200
  Data: num [1:26, 1:7] -0.0135 -0.9163 -7.4406 -1.9796 -0.7066 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:7] "Response EEM" "Response EFA" "Response HYG" "Response IEF" ...
  Index: Class 'POSIXct'  atomic [1:26] 1.35e+09 1.35e+09 1.35e+09 1.35e+09 1.35e+09 ...
  ..- attr(*, "tzone")= chr "GMT"
4

0 回答 0