0

I am following up an old question without answer (https://stackoverflow.com/questions/31653029/r-thresholding-networks-with-inputted-p-values-in-q-graph). I'm trying to assess relations between my variables.For this, I've used a correlation network map. Once I did so, I would like to implement a significance threshold component. For instance, I want to only show results with p-values <0.05. Any idea about how could I implement my code?

Data set: https://www.dropbox.com/s/xntc3i4eqmlcnsj/d100_partition_all3.csv?dl=0

My code:

library(qgraph)
cor_d100_partition_all3<-cor(d100_partition_all3)
qgraph(cor_d100_partition_all3, layout="spring",  
   label.cex=0.9, labels=names(d100_partition_all3), 
   label.scale=FALSE, details = TRUE)

Output:enter image description here

Additionally, I have this small piece of code that transform R2 values into p.values:

Code:

cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
  for (j in (i + 1):n) {
  tmp <- cor.test(mat[, i], mat[, j], ...)
  p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
 }
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
p.mat <- cor.mtest(d100_partition_all3)

Cheers

Community
  • 1
  • 1
eFF
  • 267
  • 3
  • 17

1 Answers1

1

There are a few ways to only plot the significant correlations. First, you could pass additional arguments to the qgraph()function. You can look at the documentation for more details. The function call given below should have values that are close to what is needed.

qgraph(cor_d100_partition_all3
       , layout="spring"
       , label.cex=0.9
       , labels=names(d100_partition_all3)
       , label.scale=FALSE
       , details = TRUE
       , minimum='sig' # minimum based on statistical significance
       ,alpha=0.05 # significance criteria
       ,bonf=F # should Bonferroni correction be used
       ,sampleSize=6 # number of observations
)

enter image description here

A second option is to create a modified correlation matrix. When the correlations are not statistically significant based on your cor.mtest() function, the value is set to NA in the modified correlation matrix. This modified matrix is plotted. A main visual difference between the first and second solutions seems to be the relative line weights.

# initializing modified correlation matrix
cor_d100_partition_all3_mod <- cor_d100_partition_all3

# looping through all elements and setting values to NA when p-values is greater than 0.05
for(i in 1:nrow(cor_d100_partition_all3)){
  for(j in 1:nrow(cor_d100_partition_all3)){
    if(p.mat[i,j] > 0.05){
      cor_d100_partition_all3_mod[i,j] <- NA
    }
  }
}

# plotting result
qgraph(cor_d100_partition_all3_mod
       ,layout="spring"
       ,label.cex=0.7
       ,labels=names(d100_partition_all3)
       ,label.scale=FALSE
       ,details = F
       )

enter image description here

Calvin
  • 1,309
  • 1
  • 14
  • 25
  • Hi Calvin, I am wondering if qgraph is able to render edge labels outside the actual edge? I'm trying to insert edge labels outside the edge or readability purposes. I particularly don't like the option of include a white bg below the label, it screws up the edge. According to the manual, it is possible to adjust edge label position only along the line, but not on the side. Is it possible to circumvent this issue? – eFF Feb 24 '16 at 12:44
  • 1
    You should probably make this a new question so that more people can answer and share the code and example figures. I do have an initial solution, but I can not show an example in the comment. – Calvin Feb 24 '16 at 21:19
  • Please find attached a link to a new question: http://stackoverflow.com/questions/35576319/can-qgraph-render-edge-labels-outside-the-actual-edge – eFF Feb 25 '16 at 10:19