Message Boards Message Boards

0
|
5892 Views
|
8 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Represent a correlation matrix via a graph

Posted 2 years ago

I have a simple (maybe mistake!) question. Is it possible to present a correlation matrix via a graph? For example:

data1 = {11.22, 12.29, 12.97, 14.15, 17.33, 17.60, 16.62, 14.43, 
   13.62, 11.01};
data2 = {12.29, 12.97, 14.15, 17.33, 17.60, 16.625`, 14.43, 13.62, 
   11.01, 9.25};
data3 = {12.97, 14.15, 17.33, 17.60, 16.625`, 14.43, 13.62, 11.016, 
   9.25, 9.48};

Apply[Correlation, 
  Tuples[{data1, data2, data3}, 2] // Partition[#, 3, 3] &, {2}] // 
 MatrixPlot[#, ColorFunction -> "DeepSeaColors", 
   PlotLegends -> Automatic] &

enter image description here

Thank you for your help.

POSTED BY: Alex Teymouri
8 Replies

@Alex Teymouri the questions you are asking are easily found in documentation. As you can read in the forum rules you should show your own effort first and explain what does and what does not work in your own code. Good places to start:

POSTED BY: Sam Carrettie

Please read the rules: http://wolfr.am/READ-1ST
New questions require new discussions posted separately and not digressing topics of other discussions. This question was moved to its own new discussion.

POSTED BY: Moderation Team
Posted 2 years ago

If you allow for edges to have custom weights, then I'm pretty sure that yes you can represent a correlation matrix with a directed graph. However, if you just label the edges with their weights, it may be challenging to visualize. It would be easy to include/omit edges if the correlation matrix was binary. Changing the thickness or opacity of the edges with their weights would be the best way to visualize a correlation matrix as a graph in my opinion.

POSTED BY: Joseph Stocke
Posted 2 years ago

Along the lines that Joseph suggested

corr = Apply[Correlation, Tuples[{data1, data2, data3}, 2] // Partition[#, 3, 3] &, {2}]

WeightedAdjacencyGraph[corr,
 DirectedEdges -> True,
 EdgeStyle -> {DirectedEdge[i_, j_] :> AbsoluteThickness[corr[[i, j]]]},
 PlotTheme -> "Scientific"]

enter image description here

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Wow!

Thank you so much, man.

For more understanding, is it possible to show correlation values on the figure?

POSTED BY: Alex Teymouri
Posted 2 years ago

As Sam said, this can be easily found in the documentation.

rounded = Round[corr, .01]

WeightedAdjacencyGraph[rounded,
 DirectedEdges -> True,
 EdgeStyle -> {DirectedEdge[i_, j_] :> AbsoluteThickness[rounded[[i, j]]]},
 EdgeLabels -> "EdgeWeight",
 PlotTheme -> "Scientific"]

enter image description here

POSTED BY: Rohit Namjoshi

Hi Rohit,

That is great.

I noticed that recently in the scientific papers, researchers show the correlation between some lists through a graph like the following figure. I thought it may be interesting for you. enter image description here

Best Regards

Mohammad

POSTED BY: M.A. Ghorbani
Posted 2 years ago

Thank you, Joseph.

May I ask you to do for the example data? Is the result understandable?

POSTED BY: Alex Teymouri
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract