Announcement

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

  • Mata Matrix Headers

    Hi Stata gurus, I am trying to mix string and numeric matrices, in order to improve readability, adding horizontal/vertical string headers.

    When I append the "header" below, it works
    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    : labels=("a","b","c","d","e")
    
    : (labels',strofreal(diag(1..5),"%2.1f"))\("",labels)
             1     2     3     4     5     6
        +-------------------------------------+
      1 |    a   1.0   0.0   0.0   0.0   0.0  |
      2 |    b   0.0   2.0   0.0   0.0   0.0  |
      3 |    c   0.0   0.0   3.0   0.0   0.0  |
      4 |    d   0.0   0.0   0.0   4.0   0.0  |
      5 |    e   0.0   0.0   0.0   0.0   5.0  |
      6 |          a     b     c     d     e  |
        +-------------------------------------+
    However, when I prepend the "header", it does not work.
    Code:
    : ("",labels)\(labels',strofreal(diag(1..5),"%2.1f"))
    [symmetric]
             1     2     3     4     5     6
        +-------------------------------------+
      1 |                                     |
      2 |    a   1.0                          |
      3 |    b   0.0   2.0                    |
      4 |    c   0.0   0.0   3.0              |
      5 |    d   0.0   0.0   0.0   4.0        |
      6 |    e   0.0   0.0   0.0   0.0   5.0  |
        +-------------------------------------+
    
    : end
    any clue? thks

  • #2
    Symmetric labels? Consider that the labels are matrix elements in this case. Transposing will show you that they exist.

    Code:
    mata
    labels=("a","b","c","d","e")
    A= ("",labels)\(labels',strofreal(diag(1..5),"%2.1f"))
    A'
    end
    Res.:

    Code:
    : 
    : labels=("a","b","c","d","e")
    
    : 
    : A= ("",labels)\(labels',strofreal(diag(1..5),"%2.1f"))
    
    : 
    : A'
    [symmetric]
             1     2     3     4     5     6
        +-------------------------------------+
      1 |                                     |
      2 |    a   1.0                          |
      3 |    b   0.0   2.0                    |
      4 |    c   0.0   0.0   3.0              |
      5 |    d   0.0   0.0   0.0   4.0        |
      6 |    e   0.0   0.0   0.0   0.0   5.0  |
        +-------------------------------------+
    
    : 
    : end

    Comment


    • #3
      Luis: Might this give you something like you are imagining:
      Code:
      mata
      labels=("a","b","c","d","e")
      lab=labels',J(length(labels),1," ")
      mat=diag(1..5)
      st_matrix("smat",mat)
      st_matrixrowstripe("smat",lab)
      st_matrixcolstripe("smat",lab)
      stata("matrix list smat, noheader")
      end
      Results in
      Code:
           a:  b:  c:  d:  e:
      a:   1
      b:   0   2
      c:   0   0   3
      d:   0   0   0   4
      e:   0   0   0   0   5

      Comment


      • #4
        Yes John, your approach gives all I need. However, I am still curious why would I need all that "machinery" , if appending the row vetor (labels)
        Code:
        (labels',strofreal(diag(1..5),"%2.1f"))\("",labels)
        works, same way. So, I assumed that prepending it
        Code:
        ("",labels)\(labels',strofreal(diag(1..5),"%2.1f"))
        would work, but it does not. THANKS, anyway.

        Comment


        • #5
          Originally posted by Luis Pecht View Post
          I am still curious why would I need all that "machinery" , if appending the row vetor (labels)
          Code:
          (labels',strofreal(diag(1..5),"%2.1f"))\("",labels)
          works, same way. So, I assumed that prepending it
          Code:
          ("",labels)\(labels',strofreal(diag(1..5),"%2.1f"))
          would work, but it does not.
          I explained this to you in #2, but maybe you did not follow. In symmetric matrices, only elements corresponding to the lower triangular matrix are shown. You do not complain not seeing the upper triangular matrix. So if you append the labels in the first row, the symmetry is maintained and you do not see them. Try appending different labels that violate the symmetry.

          Comment


          • #6
            Yes Andrew, now I followed you. Should not use a diagonal matrix as the toy example


            Code:
            : mata
            
            : labels1=("a","b","c","d","e")
            
            : labels2=("a","b","c","d","e","f")
            
            : ("",labels2)\(labels1',strofreal(J(5,6,0),"%2.1f"))
                     1     2     3     4     5     6     7
                +-------------------------------------------+
              1 |          a     b     c     d     e     f  |
              2 |    a   0.0   0.0   0.0   0.0   0.0   0.0  |
              3 |    b   0.0   0.0   0.0   0.0   0.0   0.0  |
              4 |    c   0.0   0.0   0.0   0.0   0.0   0.0  |
              5 |    d   0.0   0.0   0.0   0.0   0.0   0.0  |
              6 |    e   0.0   0.0   0.0   0.0   0.0   0.0  |
                +-------------------------------------------+
            
            : end
            thanks, again.

            Comment


            • #7
              Dear Luis
              If you install the package -matrixtools- you get the functionality, you ask for, I think.
              When installed (-ssc install matrixtools-) you can see the Mata content by:
              Code:
              mata mata describe using lmatrixtools
              One of the classes in lmatrixtools.mata is -nhb_mt_labelmatrix-
              Code:
              . * Initialize
              . mata: lm = nhb_mt_labelmatrix()
              
              . * add content
              . mata: lm.values((1::2) # (1..3))
              . mata: lm.row_names("mt_r" :+ strofreal(1::3))
              . mata: lm.column_names("mt_c" :+ strofreal(1::2))
              
              . * print
              . mata: lm.print()
                
              --------------------------
                     mt_c1  mt_c2  mt_c2
              --------------------------
              mt_r1   1.00   2.00   3.00
              mt_r2   2.00   4.00   6.00
              --------------------------
              You can export to a Stata matrix (-matprint- is in the package -matrixtools- as well and it is my version of -matlist-):
              Code:
              . mata: lm.to_matrix("A")
              . matprint A
                
              --------------------------
                     mt_c1  mt_c2  mt_c2
              --------------------------
              mt_r1   1.00   2.00   3.00
              mt_r2   2.00   4.00   6.00
              --------------------------
              Let us create a new Stata matrix
              Code:
              . matrix B = 2 * A
              . matrix colnames B = st_c1 st_c2 st_c3
              . matrix rownames B = st_r1 st_r2
              Stata matrix B can easily be imported into Mata:
              Code:
              . mata: lm.from_matrix("B")
              . mata: lm.print("md", 0)
              
              ----------------------------------------
                           st_c1      st_c2      st_c3
              -------    -------    -------    -------
              st_r1            2          4          6
              
              st_r2            4          8         12
              
              ----------------------------------------
              Note that you can choose different layouts like markdown (md), html, latex or csv.
              And that control number of decimals on row or cell level
              Code:
              . mata: lm.print("md", (1,2,3))
              
              -----------------------------------------
                           st_c1      st_c2       st_c3
              -------    -------    -------    --------
              st_r1          2.0       4.00       6.000
              
              st_r2          4.0       8.00      12.000
              
              -----------------------------------------
              
              . mata: lm.print("md", (1,2,3 \ 3,2,1))
              
              ----------------------------------------
                           st_c1      st_c2      st_c3
              -------    -------    -------    -------
              st_r1          2.0       4.00      6.000
              
              st_r2        4.000       8.00       12.0
              
              ----------------------------------------
              There is a lot more in the package.
              Enjoy
              Kind regards

              nhb

              Comment

              Working...
              X