Dear R-Helpers,

   I need to compute cross-correlation on two signals wich may contain
missing values.
   One cannot pass "Na.action=na.pass" to the ccf() function.

  So, I wrote a naive function of my own (see below).
  Unsurprisingly, this function is not very fast.
  Do you think that it is possible to do better, or should I accept my fate
?

Bruno.

my_ccf <- function (X, Y, lag.before = NULL, lag.after = NULL) {
  l_X <- length(X)
  l_Y <- length(Y)

  if  (l_Y != l_X) {
    cat ("X and Y should have same size !")
    return
  }
  if (is.null(lag.before)) {
    lag.before <- l_X / 10
    #should do 10*log10(N/m)
  }
  if (is.null(lag.after)) {
    lag.after <- l_X / 10
    # idem lag.before.
  }

  res <- list()
  res$cor <- rep(0.0, 1 + lag.before + lag.after)
  sub_X <-  X[ 1 + lag.before : (l_X - lag.after)]

  res$delay <- seq(-lag.before, lag.after)
  for (i in res$delay) {
    sub_Y <-  Y[(lag.before+i+1) : (l_X-lag.after+i+1)]
    OK <- !(is.na(sub_X) | is.na(sub_Y))
    x_select <- sub_X[OK]
    y_select <- sub_Y[OK]

    res$cor[i+lag.before+1] <- cor (x_select, y_select)
  }

  return (res)
}

	[[alternative HTML version deleted]]


