## Matrices within matrices

| Gabriel |

I was playing with the multiple correspondence analysis command in Stata and noticed that some of the return matrices had not just row and column labels, but hierarchy within the table. For instance, the return matrix e(A) is organized such that columns are dimensions, the high level rows are variables, and the low level rows are variables. I thought this was interesting but couldn’t find any explicit documentation of the hierarchy issue, either with reference to mca return matrices specifically or to matrices in general. After some tinkering (and help from UCLA ATS) I figured out that you can either ignore the high level rows and call values by absolute position in the matrix or call values by using a colon like this “main:sub”. It seems to work best with labels.

Anyway, here’s what I’m talking about. Note that the last few commands are synonymous as all three call the Cartesian coordinates for people who strongly agree with the statement that “any change makes nature worse”. The only difference is in their use of labels or numbers and absolute position in the matrix versus using the “variable:value” structure:

. webuse issp93a, clear
(Selection from ISSP (1993))

. quietly mca A B C D

. mat mca_coeff=e(A)

. matlist mca_coeff

|      dim1       dim2
-------------+----------------------
A            |
agree_stro~y |  1.836627   .7274591
agree |  .5462399  -.2844426
neither_ag~e | -.4467973  -1.199439
disagree | -1.165903   .7367824
disagree_s~y | -1.995217   2.470026
-------------+----------------------
B            |
agree_stro~y |  2.924321   1.370078
agree |   .641516  -.6669377
neither_ag~e |  .3460504  -.9639179
disagree |  -.714126  -.2800712
disagree_s~y | -1.353725   2.107677
-------------+----------------------
C            |
agree_stro~y |  2.157782    .908553
agree |  .2468277  -.5916111
neither_ag~e | -.6189958  -1.044412
disagree | -1.348858   .6346467
disagree_s~y | -1.467582   3.016588
-------------+----------------------
D            |
agree_stro~y |  1.203782   1.821975
agree | -.2211514  -.0069347
neither_ag~e | -.3846555  -1.158694
disagree | -.2216352  -.2105125
disagree_s~y |  .7077495   1.151804

. matlist mca_coeff[11,1..2]

|      dim1       dim2
-------------+----------------------
C            |
agree_stro~y |  2.157782    .908553

. matlist mca_coeff[11,"dim1".."dim2"]

|      dim1       dim2
-------------+----------------------
C            |
agree_stro~y |  2.157782    .908553

. matlist mca_coeff["C:agree_strongly","dim1".."dim2"]

|      dim1       dim2
-------------+----------------------
C            |
agree_stro~y |  2.157782    .908553  

Note that this kind of thing is common in other languages. So Perl uses package qualifiers (“main::sub”) to distinguish between objects within objects (or commands within libraries). Likewise R (which treats data more like Stata matrices than Stata master data) uses lots of nested data which you can call as “main[sub]”. For instance, igraph has 9 objects nested within a graph data object.