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?
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 ])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With