Search code examples
rtime-seriescross-correlationwavelet-transform

Wavemulcor package - wave.multiple.cross.correlation function - replacement has length zero


I would like to use the wavemulcor package and in particular the wave.multiple.cross.correlation function to perform a wavelet multiple cross correlation on my data.

I am following the example as per the package manual but using my data instead. The function works with the example data but throws an error when I try it with mine. The error refers to "replacement has length zero" but I am unsure what this exacly means.

I've googled the error but there are many examples of the same issue for different function and generally they all have something to do with loops in code.

I then googled how to troubleshoot the problem and read about debugging. I tried debugging the code but I can't figure out where it's breaking down, I am still at the early stages of learning to code. I think it might be this section of code in the wave.multiple.cross.correlation function that is causing the problem:

xy.cor.vec <- matrix(unlist(xy.cor), l, dd)
    xy.mulcor <- matrix(0, l, 2 * lm + 1)
    YmaxR <- vector("numeric", l)
    for (i in 1:l) {
        r <- xy.cor.vec[i, ]
        P <- diag(d)/2
        P[lower.tri(P)] <- r
        P <- P + t(P)
        Pidiag <- diag(solve(P))
        if (is.null(ymaxr)) {
            YmaxR[i] <- Pimax <- which.max(Pidiag)
        }

Are there any other ways I can determine why this is not working?

The actual error is as follows:

> Lst <- wave.multiple.cross.correlation(xx, lag.max = NULL, ymaxr = NULL)
Error in YmaxR[i] <- Pimax <- which.max(Pidiag) : 
  replacement has length zero

I have tried to trace the all the variables in the original code but I just can't figure it out.

This is the code I am trying to use and for completeness sake here is a link to a dput() of xx which is a list of the variables I wish to use, see the code below for details of xx

library(wavemulcor)
library(readxl)

rm(list = ls()) # clear objects
graphics.off() # close graphics windows

RNC20_30Hourly <- read_excel(RNC20_30Hourly.xlsx")

RNC20_30Hourly <- RNC20_30Hourly[-1]

RNC20_30TS <- ts(RNC20_30Hourly, start = 1, frequency = 23)

wf <- "d4"
J <- 6
lmax <- 36

n <- nrow(RNC20_30TS)

CK0158U09A3.modwt <- modwt(RNC20_30TS[,"CK0158U09A3"], wf, J)
CK0158U09A3.modwt.bw <- brick.wall(CK0158U09A3.modwt, wf)

CK0158U21A1.modwt <- modwt(RNC20_30TS[,"CK0158U21A1"], wf, J)
CK0158U21A1.modwt.bw <- brick.wall(CK0158U21A1.modwt, wf)

CK0158U21A2.modwt <- modwt(RNC20_30TS[,"CK0158U21A2"], wf, J)
CK0158U21A2.modwt.bw <- brick.wall(CK0158U21A2.modwt, wf)

CK0158U21A3.modwt <- modwt(RNC20_30TS[,"CK0158U21A3"], wf, J)
CK0158U21A3.modwt.bw <- brick.wall(CK0158U21A3.modwt, wf)

xx <- list(CK0158U09A3.modwt.bw, 
           CK0158U21A1.modwt.bw, 
           CK0158U21A2.modwt.bw,
           CK0158U21A3.modwt.bw)

Lst <- wave.multiple.cross.correlation(xx, lag.max = 13, ymaxr = NULL)

CK0158.RTWP.cross.cor <- as.matrix(Lst$xy.mulcor[1:J,])
YmaxR <- Lst$YmaxR

cell.names <- c("CK0158U09A3", "CK0158U21A1", "CK0158U21A2", "CK0158U21A3")
rownames(CK0158.RTWP.cross.cor)<-rownames(CK0158.RTWP.cross.cor,
                                          do.NULL = FALSE, prefix = "Level ")
lags <- length(-lmax:lmax)

lower.ci <- tanh(atanh(CK0158.RTWP.cross.cor) - qnorm(0.975) /
                   sqrt(matrix(trunc(n/2^(1:J)), nrow=J, ncol=lags)- 3))
upper.ci <- tanh(atanh(CK0158.RTWP.cross.cor) + qnorm(0.975) /
                   sqrt(matrix(trunc(n/2^(1:J)), nrow=J, ncol=lags)- 3))

par(mfrow=c(3,2), las=1, pty="m", mar=c(2,3,1,0)+.1, oma=c(1.2,1.2,0,0))
for(i in J:1) {
  matplot((1:(2*lmax+1)),CK0158.RTWP.cross.cor[i,], type="l", lty=1, ylim=c(-1,1),
          xaxt="n", xlab="", ylab="", main=rownames(CK0158.RTWP.cross.cor)[[i]][1])
  if(i<3) {axis(side=1, at=seq(1, 2*lmax+1, by=12),
                labels=seq(-lmax, lmax, by=12))}
  #axis(side=2, at=c(-.2, 0, .5, 1))
  lines(lower.ci[i,], lty=1, col=2) ##Add Connected Line Segments to a Plot
  lines(upper.ci[i,], lty=1, col=2)
  abline(h=0,v=lmax+1) ##Add Straight horiz and vert Lines to a Plot
  text(1,1, labels=cell.names[YmaxR[i]], adj=0.25, cex=.8)
}
par(las=0)
mtext('Lag (hours)', side=1, outer=TRUE, adj=0.5)
mtext('Wavelet Multiple Cross-Correlation', side=2, outer=TRUE, adj=0.5)

Any help to resolve/troubleshoot this issue would be greatly appreciated.


Solution

  • Having reviewed the code with a fine tooth comb I have spotted the error in my code. According to the manual the usage is as follows:

    wave.multiple.cross.correlation(xx, lag.max = NULL, ymaxr = NULL)
    

    yet in the example provided, the author uses a different variable called lmax which specifies the lag to be used.

    Lst <- wave.multiple.cross.correlation(xx, lmax)
    

    As you can see from my example above I specified two different arguments for the lag, lmax = 36 and lag.max = 13

    Ah well it's only cost me 100 reputations!