Hello,

I'm new to the zoo and xts packages. I'd like to adjust my data so it has
no look-ahead by using the actual report dates that I have and put it in
daily time series format.

First, below is a little mock up of what my date looks like raw (rd1 =
report dates, ua = unadjusted data values). Below that is a quick hack that
does what I want, but I'm looking for something much faster. (I have ~10k
tickers for each matrix and hundereds of matrices of unique raw financial
data). In the code below, slowness comes from the loop and the merge (of
course), but it shows you the result I am after.

(I'm considering using xts to speed this up or going a completely different
approach that doesn't pre-process the data at all.)

# mock data: this is how it comes to me (raw)
# in practice I have over 10,000 tickers and 100s of unadjusted raw data
items
# the 'periods' for my mockup data
z.dates =
c("2007-03-31","2007-06-30","2007-09-30","2007-12-31","2008-03-31","2008-06-30","2008-09-30","2008-12-31")

nms = c("A","B","C","D") # my mock tickers

# these are the report dates that are the real days the data was available
rd1 =
matrix(c("20070514","20070814","20071115","20080213","20080514","20080814","20081114","20090217",

"20070410","20070709","20071009","20080109","20080407","20080708","20081007","20090112",
              "20070426","--","--","--","--","--","--","20090319",
              "--","--","--","--","--","--","--","--"),
            nrow=8,ncol=4)
dimnames(rd1) = list(z.dates,nms)

# this is the unadjusted raw data, that always has the same dimensions,
rownames, and colnames as the report dates above (rd1)
ua = matrix(c(640.35,636.16,655.91,657.41,682.06,702.90,736.15,667.65,

2625.050,2625.050,2645.000,2302.000,1972.000,1805.000,1547.000,1025.000,
              NaN, NaN,-98.426,190.304,180.894,183.220,172.520, 144.138,
              NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN),
            nrow=8,ncol=4)
dimnames(ua) = list(z.dates,nms)

################################# a quick hack so you can see my results

fix = function(x)
{
  year = substring(x, 1, 4);
  mo = substring(x, 5, 6);
  day = substring(x, 7, 8);
  ifelse(year=="--", "NA", paste(year, mo, day, sep = "-"))
}

rd = apply(rd1, 2, fix)
dimnames(rd) = dimnames(rd)

dt1 <- seq(from =as.Date(z.dates[1]), to = as.Date("2009-03-25"), by =
"day")
dt = sapply(dt1, as.character)

fin = dt
ck_rows = length(dt)
bad = character(0)
start_t_all = Sys.time()
for(cn in 1:ncol(ua)){
  uac = ua[,cn]
  tkr = colnames(ua)[cn]
  rdc = rd[,cn]
  ua_rd = cbind(uac,rdc)
  colnames(ua_rd) = c(tkr,'rt_date')
  xx1 = merge(dt,ua_rd,by.x=1,by.y= 'rt_date',all.x=T)
  xx = as.character(xx1[,2])
  values <- c(NA, xx[!is.na(xx)])
  ind = cumsum(!is.na(xx)) + 1
  y <- values[ind]
  if(ck_rows == length(y)){
    fin  = data.frame(fin,y)
  }else{
    bad = c(bad,tkr)
  }
}

nms = nms[nms != bad]
colnames(fin) = c('daily_dates',nms)
print(fin) # this is what I am after.
print("over all time for loop")
print(Sys.time()-start_t_all)

Thanks....sorry for the bad code. Just producing some sample results for
better understanding.

ben

	[[alternative HTML version deleted]]

