2

I am trying to make an irregular multivariate time series regular. I am doing this by merging the irregular time series (one measure every 7 days) with a regular "NA" filled time series (daily measures) as suggested by:
- Joshua Ulrich here.
- Dirk Eddelbuettel here.

When I try this method for multivariate time series, I get the error:

"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"

My question is 2 fold:

  1. How can I merge these two xts data sets without getting this error?
  2. Is there a "better" way of making an irregular multivariate time series regular? I guess I was expecting to find a method in the xts package, but could not find one.

Code to Reproduce Error:

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)

In practice my real data are sporadic, sometimes daily, sometimes skipping several days. My approach is to normalize all data to 1 observation per day with 0 for days with missing values.

Thanks in advance.

EDIT:
Here is my sessionInfo() as requested:

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    
Community
  • 1
  • 1
Stan
  • 905
  • 9
  • 20
  • The code you provided does not give me an error using R-3.1.1 and the latest xts/zoo on CRAN (xts_0.9-7, zoo_1.7-12). What is your `sessionInfo()` output? – Joshua Ulrich Oct 02 '14 at 15:06
  • Please delete that comment and edit it into your question so it's more readable. Do you still get an error if you update R and zoo? – Joshua Ulrich Oct 02 '14 at 15:12
  • 1
    So...I updated my libraries, but not my version of R yet (work security = delays). The change @agstudy posted works for both my old version of zoo and my new version. Thanks so much for the help AND XTS!!! :) – Stan Oct 03 '14 at 15:17

1 Answers1

2

This works fine for me, I just follow Joshua Ulrich link :

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
.....
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • &*^#$*$^@ !!! Sheeesh I really appreciate your help! I wish I had posted this on S.O. earlier. I could have sworn I already tried this before going down the road I posted above. Thanks again! – Stan Oct 03 '14 at 15:25