我正在尝试使不规则的多元时间序列变得规则。我通过将不规则时间序列(每 7 天一次测量)与常规“NA”填充时间序列(每日测量)合并来实现此目的,如下所示:
- 约书亚·乌尔里希here 。
- 德克·埃德尔比特尔here .
当我对多元时间序列尝试此方法时,出现错误:
"Error in
colnames<-
(*tmp*
, value = c("C.1", "C.2", "C.1.1", "C.2.1" : length of 'dimnames' [2] not equal to array extent"
我的问题有两个:
- 如何合并这两个 xts 数据集而不出现此错误?
- 是否有“更好”的方法使不规则的多元时间序列变得规则?我想我本来希望在 xts 包中找到一种方法,但找不到。
重现错误的代码:
require(xts)
set.seed(42)
# make irregular index
irr_index <- seq(from=as.Date("2010-01-19"), length.out=10, by=7)
# make irregular xts
irr_xts <- xts( x= matrix( data= rnorm(20), ncol= 2,
dimnames= list(c(1:length(irr_index)),
c("C.1", "C.2"))),
order.by= irr_index)
# make regular index
reg_index <- seq(from=as.Date(start(irr_xts)), to=as.Date(end(irr_xts)), by=1)
empty <- xts(matrix(data = NA,
nrow = length(reg_index),
ncol = ncol(irr_xts)),
reg_index )
reg_xts <- na.fill(merge(irr_xts, empty), fill=0)
实际上,我的真实数据是零星的,有时是每天的,有时是跳过几天的。我的方法是将所有数据标准化为每天 1 次观察,其中 0 次表示缺失值的天数。
提前致谢。
编辑:
这是我的sessionInfo()
根据要求:
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] xts_0.9-7 zoo_1.7-10
loaded via a namespace (and not attached):
[1] grid_3.0.2 lattice_0.20-24 tools_3.0.2
最佳答案
这对我来说效果很好,我只需点击 Joshua Ulrich 链接即可:
empty <- xts(,reg_index ) ## No need to set coredata to create empty xts
merge(irr_xts, empty, fill=0)
C.1 C.2
2010-01-19 1.370958 1.30487
2010-01-20 0.000000 0.00000
2010-01-21 0.000000 0.00000
2010-01-22 0.000000 0.00000
2010-01-23 0.000000 0.00000
2010-01-24 0.000000 0.00000
.....
关于r - "Error in colnames"合并xts集时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162151/