Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • labels on mata matrices

    Hi
    One of the things I like about the -matrix- in Stata is the possiblity to add row and column labels to matrices there.
    Is there something similar for Mata matrices?
    I've been trying to find something in the documentation without luck, so the answer could be that it is not possible.
    But in that case what do others do if they would like to use this facility?

    Looking forward to hear from you
    Kind regards

    nhb

  • #2

    There is no built-in facility for row and column labels for Mata matrices.

    However, you can build separate string matrices that correspond to the
    row and column labels (also known as matrix stripes) for any given
    matrix. I image one trouble you have is trying to display your Mata
    matrix using the labels you have built.

    There is an not-documented Mata function _matrix_list() that was
    developed to do this for optimize() and moptimize(). The
    source for _matrix_list() is in your adopath.

    Code:
    viewsource st_matrix_list.mata
    In fact, this is the code produces the output for the
    matrix list command.

    The basic syntax for _matrix_list() is

    Code:
    _matrix_list(matrix, rstripe, cstripe)
    matrix is a numeric Mata matrix.
    rstripe is a string Mata matrix representing the row labels.
    cstripe is a string Mata matrix representing the column labels.

    The rstripe and cstripe matrices both have 2 columns.
    Column 1 is the equation part, and Column 2 is the coefficient part.
    The rows of rstripe correspond with the rows of matrix.
    The rows of cstripe correspond with the columns of matrix.

    Here is a quick example to see that _matrix_list() will produce the
    same output as matrix list.

    Code:
    sysuse auto
    gsem mpg <- turn i.rep
    matrix list e(b)
    
    mata:
    b = st_matrix("e(b)")
    rlabels = st_matrixrowstripe("e(b)")
    clabels = st_matrixcolstripe("e(b)")
    rlabels
    clabels
    _matrix_list(b,rlabels,clabels)
    end
    Here is a log.

    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . gsem mpg <- turn i.rep
    
    Iteration 0:   log likelihood = -189.61577  
    Iteration 1:   log likelihood = -189.61577  
    
    Generalized structural equation model             Number of obs   =         69
    Log likelihood = -189.61577
    
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    mpg <-       |
            turn |  -.9131143   .1214961    -7.52   0.000    -1.151242   -.6749863
                 |
           rep78 |
              2  |   .2936464   3.000444     0.10   0.922    -5.587115    6.174408
              3  |  -1.505792   2.758856    -0.55   0.585    -6.913051    3.901466
              4  |  -1.616119   2.832069    -0.57   0.568    -7.166871    3.934633
              5  |   1.466023   2.976163     0.49   0.622    -4.367149    7.299196
                 |
           _cons |   58.43769   5.652368    10.34   0.000     47.35925    69.51612
    -------------+----------------------------------------------------------------
       var(e.mpg)|   14.27104   2.429663                      10.22204    19.92387
    ------------------------------------------------------------------------------
    
    . matrix list e(b)
    
    e(b)[1,8]
                mpg:         mpg:         mpg:         mpg:         mpg:
                              1b.           2.           3.           4.
               turn        rep78        rep78        rep78        rep78
    y1   -.91311427            0     .2936464   -1.5057924    -1.616119
    
                mpg:         mpg:  var(e.mpg):
                  5.                          
              rep78        _cons        _cons
    y1    1.4660234    58.437685    14.271038
    
    . 
    . mata:
    ------------------------------------------------- mata (type end to exit) -----
    : b = st_matrix("e(b)")
    
    : rlabels = st_matrixrowstripe("e(b)")
    
    : clabels = st_matrixcolstripe("e(b)")
    
    : rlabels
            1    2
        +-----------+
      1 |       y1  |
        +-----------+
    
    : clabels
                    1            2
        +---------------------------+
      1 |         mpg         turn  |
      2 |         mpg     1b.rep78  |
      3 |         mpg      2.rep78  |
      4 |         mpg      3.rep78  |
      5 |         mpg      4.rep78  |
      6 |         mpg      5.rep78  |
      7 |         mpg        _cons  |
      8 |  var(e.mpg)        _cons  |
        +---------------------------+
    
    : _matrix_list(b,rlabels,clabels)
                mpg         mpg         mpg         mpg         mpg         mpg
              turn    1b.rep78     2.rep78     3.rep78     4.rep78     5.rep78
    y1  -.91311427           0    .2936464  -1.5057924   -1.616119   1.4660234
    
                mpg  var(e.mpg)
             _cons       _cons
    y1   58.437685   14.271038
    
    : end

    Comment


    • #3
      Hi Steve
      Thank you very much.
      I'll look into it
      Kind regards

      nhb

      Comment

      Working...
      X