Jfortin1 / neuroCombat_Rpackage

neuroCombat R package
20 stars 8 forks source link

Error in while (change > conv) { : missing value where TRUE/FALSE needed #3

Open tombresser opened 2 years ago

tombresser commented 2 years ago

Hi Jean-Philippe,

While trying to harmonize connectome edges I ran into the following error: Error in while (change > conv) { : missing value where TRUE/FALSE needed

It took some digging, but it was produced because one small batch had rows with only one value plus NAs. In that case, the error makes sense but the error message was vague. To help other users, I propose an additional check within neuroCombat producing an error if this situation occurs.

Below how to reproduce the error. As a suggestion I added a possible helper function that checks for this situation.

# set up example
p=10000
n=10
batch = sample(c(1,2), n, replace = T) 
dat = matrix(runif(p*n), p, n) 

# create row with  only 1 value within a batch
ntwo <- ncol(dat[,batch==2])
dat[,batch==2][1,1:ntwo-1] <- NA

# produces error
neuroCombat(dat, batch)

# additional helper function
.checkNABatch <- function(dat, batch){
  batch     <- as.factor(batch)
  batches   <- lapply(levels(batch), function(x)which(batch==x))
  n.batches <- sapply(batches, length)
  nas.batches <- lapply(batches, function(x) rowSums(is.na(dat[,x])))

  logic.batches <- map2(n.batches, nas.batches, ~ (.x - .y) <= 1)
  n.logic <- lapply(logic.batches, sum)
  x <- sum(unlist(n.logic))
  logic.batches <- unlist(logic.batches)

  if (any(logic.batches)){
    msg <- paste0(x, " row(s) (features) were found to have only 1 value within a batch. Please remove these rows before running neuroCombat.")
    stop(msg)
  }
}

.checkNABatch(dat, batch)