6

I am trying to figure out how to use graph.adjacency to create a graph using a correlation matrix (values -1 to 1), but only having the most strongly correlated edges included in the graph file, ie <-.8 or >.8

Here is the code that successfully gives me the network with the full data set:

corrdata<-read.csv("spearmancorr.csv",header=FALSE)
cor_mat<-as.matrix(corrdata)
diag(cor_mat)<-0
graph<-graph.adjacency(cor_mat,weighted=TRUE,mode="lower")

I tried using delete.edges to reduce the network to at least >.8 to test it out, but the resulting file still shows edge weights below 0.8

graph.copy <- delete.edges(graph, which(E(graph)$weight !<0.8)-1)
write.graph(graph.copy, file="gsig80.graphml", format="graphml")

Any advice on how to get the graph file I want?

user2988430
  • 73
  • 2
  • 6

1 Answers1

6

You can delete the edges from the graph if you want to, or delete them from the matrix in the first place. E.g.

cor_mat[ cor_mat < .8 ] <- 0
diag(cor_mat) <- 0
graph <- graph.adjacency(cor_mat, weighted=TRUE, mode="lower")

Here is how to delete them from the graph, after creating it:

graph <- delete.edges(graph, E(graph)[ weight < 0.8 ])
Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
  • Both those methods appeared to work, based on the reduction in file size. However, when I check the weights using `E(graph)$weights` the edge weights are no longer the r values, but the IDs from Column A of the matrix file. – user2988430 Nov 15 '13 at 18:00
  • I don't understand this, sorry. Some reproducible example would help. Btw. it is `E(graph)$weight`, without the 's'. – Gabor Csardi Nov 16 '13 at 02:56
  • I added a reproducible matrix in the original question, thanks. – user2988430 Nov 18 '13 at 15:56
  • 1
    I can't see any matrix there. Also, where is `spearmancorr.csv`? Please see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Thanks. – Gabor Csardi Nov 18 '13 at 19:31
  • Sorry about the poor reproducible example. However, I determined where the problem occurs. I tested your answer with a smaller matrix (10x10) and it worked fine. My real dataset has a matrix of 5395x5395. When using the big matrix 'delete.edges" strips the graph file of all but 2 edges (which is not correct). Is there a code to make this work with bigger files? – user2988430 Nov 20 '13 at 16:24
  • I understand it even less..... so if the matrix is small, the code works, if it is big, then the edge weights are replaced by some row ids of the matrix? Again, please add a reproducible example. It does not have to be small, if you think the problem is that the matrix is big, set the random seed and create a random matrix. That is reproducible. – Gabor Csardi Nov 20 '13 at 17:51
  • For consistency with the OP's question, this should probably have `cor_mat[abs(cor_mat) < .8] <- 0` (and `abs(weight)`). – jbaums Jan 08 '16 at 07:44