Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

igraph (R) How to create correlation network with only strong r values

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?

like image 918
user2988430 Avatar asked Nov 13 '13 22:11

user2988430


1 Answers

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 ])
like image 89
Gabor Csardi Avatar answered Oct 18 '22 02:10

Gabor Csardi