0

Consider, i have two matrices, one with response, one with predictor variables, and i calculate distance matrices for each of those variables:

resp <- as.data.frame(matrix(rnorm(6*6,mean=1000,sd=100), 6, 6)) pred <- as.data.frame(matrix(rnorm(6*6,mean=1000,sd=100), 6, 6)) colnames(resp) <- paste("Resp_",letters[1:6],sep="") colnames(pred) <- paste("Pred_",LETTERS[1:6],sep="") #calculate distance matrices for each variable per site Z <- lapply(resp, function(x) dist(x)) A <- lapply(pred, function(x) dist(x)) 

I want to perform mantel statistics for every pair of matrices between A and Z:

library(vegan) res1 <- mantel(Z[[1]], A[[1]], method="spearman", perm=999) res2 <- mantel(Z[[2]], A[[1]], method="spearman", perm=999) 

or broadly put

res(i,j) <- mantel(Z[[1:i]], A[[1:j]], method="spearman", perm=999) 

I am interested in retrieving the r and p-values as in res1[[3]] and res1[[4]], possibly in a matrix, just like

cor(resp, pred) 

How can i do this in a loop?

Thank you!

1 Answer 1

1

You could try this:

r <- p <- matrix(NA, 6, 6) colnames(r) <- colnames(p) <- names(A) rownames(r) <- rownames(p) <- names(Z) for (i in 1:6) { for (j in 1:6) { m <- mantel(Z[[i]], A[[j]], method="spearman", perm=999) r[i, j] <- m$statistic p[i, j] <- m$signif } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.