1
$\begingroup$

I am trying to restructure a correlation matrix such that the variables with the strongest correlations are grouped together

Needs["GraphUtilities`"]

These are the column headers of the data'

t = {"Age", "Blood Pressure", "Specific Gravity", "Albumin", "Sugar","Red Blood Cells"}

This is the data

m={{1.,0.131701,-0.107092,0.0781839,0.188485,-0.00275326},{0.131701,1.,-0.123132,0.122221,0.189071,-0.149835},{-0.107092,-0.123132,1.,-0.502224,-0.291435,0.269056},{0.0781839,0.122221,-0.502224,1.,0.286883,-0.394184},{0.188485,0.189071,-0.291435,0.286883,1.,-0.0923074},{-0.00275326,-0.149835,0.269056,-0.394184,-0.0923074,1.}}

These are the ticks for the matrix plot

ticks = {{Table[{i, t[[i]]}, {i, 1, Length@t}], None}, {None, 
Table[{i, Rotate[columnHeaders[[i]], 90 Degree]}, {i, 1, 
  Length@t}]}};

and here is the correlation matrix

matplot =MatrixPlot[m, Mesh -> True,ColorFunction -> ColorData[{"TemperatureMap", "Reverse"}],FrameTicks -> ticks]

I am attempting to use MinimumBandwidthOrdering[] to essentially restructure the matrix so that the strongest correlated varibales are grouped together.

{r, c} = MinimumBandwidthOrdering[m]

Plotting the matrices side by side

{MatrixPlot[m], MatrixPlot[m[[r, c]]]}

My problem is this: How can i plot the the matrices such that when the matrix is transformed, the column headers that are associated with that variable are transformed with it.

from the plots, it is just labelled as: 1,2,3,4,5,6 Whereas i was hoping it to be in the format of the plot that was given from the matplot function.

Thank you for reading.

$\endgroup$
3
  • $\begingroup$ With t[[c]]...? $\endgroup$ Commented Mar 5, 2020 at 13:46
  • $\begingroup$ If you mean t[[c]] as in the collum headers appended to the collums yes. But with t appended to the columns and rows as shown in the plot from the matplot[] correlation matrix. $\endgroup$
    – isaac5122
    Commented Mar 5, 2020 at 13:50
  • 1
    $\begingroup$ use ticks2 = {{Table[{i, t[[r]][[i]]}, {i, 1, Length@t}], None}, {None, Table[{i, Rotate[t[[c]][[i]], 90 Degree]}, {i, 1, Length@t}]}}; for FrameTicks in MatrixPlot of m[[r,c]]? $\endgroup$
    – kglr
    Commented Mar 5, 2020 at 14:39

1 Answer 1

1
$\begingroup$

Define ticks2 using t[[r]] and t[[c]] in place of t in your code for ticks.

ticks2 = {{Table[{i, t[[r]][[i]]}, {i, 1, Length@t}], None}, {None, 
    Table[{i, Rotate[t[[c]][[i]], 90 Degree]}, {i, 1, Length@t}]}};

Row[MapThread[Panel @
   MatrixPlot[#, Mesh -> True, ImageSize -> 1 -> 40, 
     ColorFunction -> ColorData[{"TemperatureMap", "Reverse"}], 
     FrameTicks -> #2, 
     Epilog -> {Text[Style[#3, 16], Scaled@{-.15, 1.15}]}] &, 
  {{m, m[[r, c]]}, {ticks, ticks2}, {"m", "m[[r, c]]"}}], Spacer[20]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.