Error in apply(x, 3L, qr.default, ...) : 'MARGIN' does not match dim(X)

130 views
Skip to first unread message

IC K

unread,
Jun 12, 2024, 9:55:05 PM6/12/24
to brainGraph-help
Hello,

I'm encountering an issue with the NBS function from the brainGraph package in R. The function works fine when the number of nodes n is 10 or less, but it throws an error when n is greater than 10. Here's a minimal reproducible example to demonstrate the problem:

Code:
library(brainGraph)
library(data.table)

# Create example data
set.seed(123)
n <- 11  # Number of nodes
m <- 48  # Number of subjects

# Create 3D array A (n, n, m)
A <- array(rnorm(n * n * m), dim = c(n, n, m))

# Ensure A is symmetric
for (i in 1:m) {
  A[, , i] <- (A[, , i] + t(A[, , i])) / 2
}

# Create covariate data frame
covars <- data.table(
  Group1 = rep(1:2, each = m * 0.5),
  Age = rnorm(m, 30, 5),
  Sex = rep(0:1, times = m * 0.5),
  Edu = sample(12:16, m, replace = TRUE),
  SubjSleep = sample(0:1, m, replace = TRUE)
)

# Convert factor variables to numeric
covars$Group1 <- as.numeric(factor(covars$Group1))
covars$Sex <- as.numeric(factor(covars$Sex))
covars$Edu <- as.numeric(factor(covars$Edu))
covars$SubjSleep <- as.numeric(factor(covars$SubjSleep))

# Define contrast vector
contrasts <- c(0, 1, 0, 0, 0, 0)

# Check design matrix
design <- model.matrix(~ Group1 + Age + Sex + Edu + SubjSleep, data = covars)
print(design)
print(dim(design))  # Should be (48, 6)

# Check for NA values in A and covars
print(sum(is.na(A)))      # Should be 0
print(sum(is.na(covars))) # Should be 0

# Run NBS
nbs.result <- NBS(A, covars, contrasts, con.type = "t", X = design, p.init = 0.001, N = 1000)

# View results
print(nbs.result)

Error Message:
Error in apply(x, 3L, qr.default, ...) : 'MARGIN' does not match dim(X)
This error only occurs when n (the number of nodes) is greater than 10. When n is 10 or less, the function works as expected.

Things I've tried:

Ensured that the dimensions of A are correct.
Confirmed that there are no NA values in A or covars.
Verified that the design matrix design is correctly specified.
Has anyone encountered a similar issue or have any suggestions on how to resolve this?

Thank you for your help!

Chris Watson

unread,
Jun 17, 2024, 4:59:00 PM6/17/24
to brainGr...@googlegroups.com
I didn't get an error from that code. What versions are you using?

--
You received this message because you are subscribed to the Google Groups "brainGraph-help" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brainGraph-he...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brainGraph-help/5159cfb3-2dca-4004-a6a9-bfea81c2d14bn%40googlegroups.com.

Chris Watson

unread,
Jun 18, 2024, 11:07:35 AM6/18/24
to brainGr...@googlegroups.com
Can you also run traceback()
so I can see where the error occurs.

IC K

unread,
Jun 19, 2024, 12:03:39 AM6/19/24
to brainGraph-help
Thank you.
Here is the message that appears after running traceback():

Error in apply(x, 3L, qr.default, ...) : 'MARGIN' does not match dim(X)
> traceback()
13: stop("'MARGIN' does not match dim(X)")
12: apply(x, 3L, qr.default, ...)
11: qr.array(M)
10: qr(M)
9: MASS::Null(t(contrast))
8: partition(X, contrasts[[j]], part.method)
7: setup_randomise(perm.method, part.method, X, contrasts, ctype,
       nC)
6: randomise(perm.method, part.method, N, perms, X, yMat, contrasts,
       ctype, nC, skip = skip, n = n, p = p, ny = ny, dfR = dfR)
5: NBS(A, covars, contrasts, con.type = "t", X = design, p.init = 0.001,
       N = 1000) at NBS_test.R#40
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))

Chris Watson 在 2024年6月18日 星期二晚上11:07:35 [UTC+8] 的信中寫道:

Chris Watson

unread,
Jun 21, 2024, 1:15:06 PM6/21/24
to brainGr...@googlegroups.com
I think that's due to a change in R 4.0. You can try including the following code before you run NBS:

qr.matrix <- function(x, ...) qr.default(x, ...)

I think it should work after that.

Yi-Chia Clementine Kung

unread,
Jul 23, 2024, 8:55:48 AM7/23/24
to brainGr...@googlegroups.com
It works!

Thank you so much.

Chris Watson <chris.w...@gmail.com> 於 2024年6月22日 週六 上午1:15寫道:
Reply all
Reply to author
Forward
0 new messages