mtpc - error

73 views
Skip to first unread message

virva.sa...@gmail.com

unread,
Aug 5, 2021, 7:26:40 AM8/5/21
to brainGraph-help

Dear Chris,

I have been trying to perform "Multi-threshold permutation correction”. I have been following the example on page 74-75 of the user manual. 

My installation set-up is: R 4.0.3; RStudio 1.3.1093; brainGraph 3.0.2; macOS Catalina 10.15.7

As I run my script: 

mtpcVars <- data.table(level=rep(c('graph', 'vertex'), each=2), outcome=c('E.global.wt', 'Lp', 'strength', 'GC.wt'),                        alt='greater')


# Change H_A for 'Lp'

mtpcVars[outcome == 'Lp', alt := 'less']

setkey(mtpcVars, level, outcome)


# Different number of permutations based on the level

mtpcVars['graph', N := 1e4]

mtpcVars['vertex', N := 5e3]


# Generate permutation matrices using 'shuffleSet' from the 'permute' package

mtpcPerms <- list(vertex=shuffleSet(n=nrow(covars.dti[, 1:4]), nset=mtpcVars['vertex', unique(N)]), graph=shuffleSet(n=nrow(covars.dti[, 1:4]), nset=mtpcVars['graph', unique(N)]))


# Create the contrast matrix

mtpcContrast

mtpcVars


##     level

## 1:  graph

mtpcContrast <- matrix(c(0, 0, 0, 2, 

                         0, 0, 0, -2), 

                       nrow=2, byrow=TRUE,

                       dimnames=list(c('control > preems', 'preem > control')))


# Loop through the network metrics

# The 1st-level of the list is for each 'level' (i.e., 'graph' and 'vertex') 

mtpc.diffs.list <- sapply(mtpcVars[, unique(level)], function(x) NULL)

for (x in names(mtpc.diffs.list)) { # Loop across 'level'

  

  # The 2nd-level is for each network metric (for the given level 'x')

  mtpc.diffs.list[[x]] <- sapply(mtpcVars[x, unique(outcome)], function(x) NULL) 

  for (y in mtpcVars[x, outcome]) {

    

    mtpc.diffs.list[[x]][[y]] <-

      mtpc(g, thresholds, covars=covars.dti[, 1:4], measure=y, contrasts=mtpcContrast,

           con.type='t', level=x, N=mtpcVars[.(x, y), N], perms=mtpcPerms[[x]], 

           alt=mtpcVars[.(x, y), alt])

 } 


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


> traceback() gives: 

12: stop("'MARGIN' does not match dim(X)")

11: apply(x, 3L, qr.default, ...)

10: qr.array(M)

9: qr(M)

8: MASS::Null(t(contrast))

7: partition(X, contrasts[[j]], part.method)

6: setup_randomise(perm.method, part.method, X, contrasts, ctype, 

       nC)

5: randomise(perm.method, part.method, N, perms, X, y, contrasts, 

       ctype, nC, n = n, p = p, ny = ny, dfR = dfR)

4: brainGraph_GLM(z, covars, measure, contrasts, con.type, outcome, 

       con.name = con.name, N = N, level = level, permute = TRUE, 

       perm.method = perm.method, part.method = part.method, perms = perms, 

       alpha = alpha, long = TRUE, ...)

3: FUN(X[[i]], ...)

2: lapply(g.list, function(z) brainGraph_GLM(z, covars, measure, 

       contrasts, con.type, outcome, con.name = con.name, N = N, 

       level = level, permute = TRUE, perm.method = perm.method, 

       part.method = part.method, perms = perms, alpha = alpha, 

       long = TRUE, ...))

1: mtpc(g, thresholds, covars = covars.dti[, 1:4], measure = y, 

       contrasts = mtpcContrast, con.type = "t", level = x, N = mtpcVars[.(x, 

           y), N], perms = mtpcPerms[[x]], alt = mtpcVars[.(x, y), 

           alt])

The "mtpcPerms" is a list of 2 elements

vertex [5000 x 67]

graph[5000 x 67]


67 is the number of my subjects.  

SC of the mtpcContrast & mtpcVars also attached to this mail. 


Do you have any idea, what is wrong with my set-up? I hope you can help me!


With best regards,

Virva

Chris Watson

unread,
Aug 16, 2021, 7:29:59 AM8/16/21
to brainGr...@googlegroups.com
Sorry I neglected to reply to this email.

1. Try not to copy the code from the User Guide, unless you happen to have the exact same hypotheses. They were included only to provide some examples.
2. I think perhaps your design matrix is not set-up correctly. Try running "brainGraph_GLM_design" on the covariates and columns of interest and see what the output looks like. You can then check if it is of full column rank. For example,

X <- brainGraph_GLM_design(covars.dti[, 1:4])
dim(X)[2] == qr(X)$rank

The 2nd line of code should return TRUE.

Chris

--
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/9a8839df-ae2e-4b8f-8162-e66156a61171n%40googlegroups.com.

virva.sa...@gmail.com

unread,
Aug 17, 2021, 2:27:45 AM8/17/21
to brainGraph-help
Hi Chris!

Thank you for your reply! 

I run the code you suggested and get the same error:
Error in apply(x, 3L, qr.default, ...) : 'MARGIN' does not match dim(X)

traceback()
4. stop("'MARGIN' does not match dim(X)")
3. apply(x, 3L, qr.default, ...)
2. qr.array(X)
1. qr(X)

Codes run separately:
> dim(X)[2]
[1] 4

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

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

Chris Watson

unread,
Aug 17, 2021, 7:27:08 AM8/17/21
to brainGr...@googlegroups.com
It seems to be treating X as if it an *array* instead of a *matrix*. Can you return the output of "str(X)"? And can you try "qr.default(X)$rank"?

I believe this is a bug caused by R >= 4.0.0 in which matrix objects now also inherit from class "array" in addition to "matrix".

The quickest solution would be for you to use R < 4.0.0. Otherwise I will have to put out a bugfix which will take time.
Chris

Chris Watson

unread,
Aug 17, 2021, 7:40:13 PM8/17/21
to brainGr...@googlegroups.com
Actually, you could also define the following function and I think things should work:

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

virva.sa...@gmail.com

unread,
Aug 18, 2021, 9:52:36 AM8/18/21
to brainGraph-help

I defined the function as you instructed and now the code seems to work. Thank you so much for your help!

Reply all
Reply to author
Forward
0 new messages