Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tombresser opened this issue Aug 1, 2022 · 0 comments

Comments

@tombresser
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant