0

I am running Quadratic Assignment Procedure (QAP) to find the correlation of a dependent network matrix with four independent monadic node covariate matrices in R using the sna package.

I am using the following code:

cor2<-sna::gcor(list(bilateralaid_network, GDP_capita_receiver, 
                  GDP_capita_sender,
                  HDI_receiver,HDI_sender))
p2<-qaptest(list(bilateralaid_network, GDP_capita_receiver, 
                 GDP_capita_sender,
                 HDI_receiver,HDI_sender), gcor, g1=1, g2=2, reps=1000)
p2<-qaptest(list(bilateralaid_network, GDP_capita_receiver, 
                 GDP_capita_sender,
                 HDI_receiver,HDI_sender), gcor, g1=1, g2=3, reps=1000)
#etc

However I do not want to run a qaptest individually for each dependent variable like shown in this question.

Is there a way of returning all correlation coefficients and p-values in table or matrix format in R?

I read that it is possible with UCINET software, but I personally do not have access to it.

UCINET is able to provide a result like shown in the screenshot below. I would like to produce a similar output in R. enter image description here

AnCo
  • 41
  • 4
  • qap is a bivariate analysis by definition. So, you can't get out from running it two-by-two. That said, it would be straightforward to write a little function that runs all combinations of networks and applies the qaptest to them and then outputs that as a matrix. It is also fairly easy to write a function that does this a little more efficiently by generating the vector of permutations once and then apply that for each pairwise analysis. It might be a little faster since you won't have to draw those permutations separately for each qap run. – Steve G. Jones Dec 22 '21 at 21:04

1 Answers1

0

A very simple solution would be the following. This can be optimized in several ways, but this is quite straightforward, utilizing the functionality from the sna package.

# some random networks
g <- array(dim = c(3, 10, 10))
g[1, , ] <- sna::rgraph(10)
g[2, , ] <- sna::rgraph(10, tprob = g[1, , ]*0.8)
g[3, , ] <- 1
g[3, 1, 2] <- 0

socmat <- network::as.sociomatrix(g)
reps = 1000
testval <- sna::gcor(g)
out <- array(dim = c(dim(g)[1], dim(g)[1], reps))

for (i in 1:reps) {
  out[, , i] <- sna::gcor(sna::rmperm(socmat)) - testval
}

(pgreq <- rowMeans(out > 0, dims = 2))
(pleeq <- rowMeans(out <= 0, dims = 2))
testval

Here, pgreq are the p-values of the correlation being greater than the observed correlation, pleeq contains the p-values for the correlation being smaller or equal to the observed correlation, and testval contains the correlations between the observed networks.

> pgreq
      [,1]  [,2]  [,3]
[1,] 0.000 0.000 0.436
[2,] 0.000 0.000 0.619
[3,] 0.436 0.619 0.000

> pleeq
      [,1]  [,2]  [,3]
[1,] 1.000 1.000 0.564
[2,] 1.000 1.000 0.381
[3,] 0.564 0.381 1.000

> testval
            1          2           3
1  1.00000000  0.7135061 -0.09480909
2  0.71350607  1.0000000 -0.13287777
3 -0.09480909 -0.1328778  1.00000000

You can easily package this into a function and make it more efficient. However, this code will probably already be sufficient in many cases.

Steve G. Jones
  • 325
  • 2
  • 10