Announcement

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

  • Keeping column names with mata

    My Mata is pretty weak, but I need to use it to invert some giant matrices, as much as 11,000 by 11,000. The person I am trying to help was using Stata matrix commands and they would never finish. Mata works pretty well except for one thing:

    I have commands like

    Code:
    local flist VCM VCVM estcfN estcfA estcfC estcfF
    local flist `flist' estcfN1 estcfA1 estcfC1 estcfF1
    foreach filename of local flist {
            use `filename'.dta, clear
            putmata `filename' = (*)
            clear
    }
    Unfortunately the Mata matrices do not contain the original column/variable names. How can I keep them or add them back? One nice thing about mkmat is that it keeps the column names.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

  • #2
    You can store the variable names in a string array (string rowvector in Mata parlance) with
    Code:
    Varnames = st_varname(1..st_nvar())
    I think that to add them back, you'd need to loop
    Code:
    real scalar k
    for (k=1; k<=st_nvar(); k++) {
        st_varrename(k, Varnames[k])
    }
    but others might have written a canned Mata function that does that.

    Comment


    • #3
      Thanks Joseph. I'm mildly surprised that Mata doesn't make this easy or even automatic.

      Luckily it turns out I don't need the column/ variable names after all, but I'll keep your tip in mind if I ever do.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      Stata Version: 17.0 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Originally posted by Richard Williams View Post
        I'm mildly surprised that Mata doesn't make this easy or even automatic.
        If I had to do this for a project, I'd probably just whip up a Mata class, maybe "StataMatrix", that takes care of everything behind the scenes of a public method -StataMatrix.invert(string scalar matrix_or_file_name)-, and then use objects instantiated from that.

        Comment


        • #5
          RIchard: Also perhaps check out
          Code:
          st_matrixrowstripe(...)
          and
          Code:
          st_matrixcolstripe(...)
          described at
          Code:
          help mata st_matrix

          Comment


          • #6
            Thanks John. I can indeed get the column/variable names with st_matrixcolstripe. But there doesn't seem to be a simple way to use that info to rename the columns in the Mata data matrix.
            -------------------------------------------
            Richard Williams, Notre Dame Dept of Sociology
            Stata Version: 17.0 MP (2 processor)

            EMAIL: [email protected]
            WWW: https://www3.nd.edu/~rwilliam

            Comment


            • #7
              Richard Williams
              First, let me use this opportunity to thank you for your website which I visit from time to time, always with great pleasure.

              I'm not satisfied with the functionality wrt matrices in Stata/Mata, either.

              Over time, I developed a set of Mata utilities, which I use eg in my package matrixtools (scc install).

              I've made a small summary in the example below. Maybe some of it can be of use for you?
              Enjoy
              Code:
              . quietly sysuse auto, clear
              . quietly regress price i.rep78 weight
              Import table from regression
              Code:
              . mata: lm = nhb_mt_labelmatrix()
              . mata: lm.from_matrix("r(table)")
              See the content (One can control output (e.g. stata, html, latex, markdown), decimals etc)
              Code:
              . mata: lm.print()
              -------------------------------------------------------------------------
                      1b.rep78   2.rep78   3.rep78   4.rep78  5.rep78  weight     _cons
              -------------------------------------------------------------------------
              b           0.00    783.90   1379.12   2068.27  3245.27    2.44  -3000.41
              se                 1911.99   1765.47   1802.43  1884.18    0.42   2139.02
              t                     0.41      0.78      1.15     1.72    5.87     -1.40
              pvalue                0.68      0.44      0.26     0.09    0.00      0.17
              ll                -3036.91  -2148.90  -1533.61  -519.96    1.61  -7274.90
              ul                 4604.71   4907.13   5670.14  7010.50    3.27   1274.09
              df         63.00     63.00     63.00     63.00    63.00   63.00     63.00
              crit        2.00      2.00      2.00      2.00     2.00    2.00      2.00
              eform       0.00      0.00      0.00      0.00     0.00    0.00      0.00
              -------------------------------------------------------------------------
              Subselection by regular expression on keep(default)/drop(0), names(default)/equations(0), and rows(default)/columns(0).
              Code:
              . mata: lm = lm.regex_select("b|se").regex_select("rep78", 1, 1, 0)
              Transposing is simple
              Code:
              . mata: lm.transposed().print()
              --------------------------
                              b       se
              --------------------------
              1b.rep78     0.00         
              2.rep78    783.90  1911.99
              3.rep78   1379.12  1765.47
              4.rep78   2068.27  1802.43
              5.rep78   3245.27  1884.18
              --------------------------
              Generating new column names. The method ::column_names() returns the current column names.
              Code:
              . mata: R = rows(lm.column_names())
              . mata: new_cnms = J(R,1,"")
              . mata: for(r=1;r<=R;r++) new_cnms[r] = regexm(lm.column_names()[r], "([0-9])b?\.(.+)") ? sprintf("%s(%s)", regexs(2), regexs(1)) : ""
              Also, column names can be changed to the values of a string column vector having the vector as argument.
              Code:
              . mata: lm.column_names(new_cnms)
              . mata: lm.print()
              ----------------------------------------------------
                  rep78(1)  rep78(2)  rep78(3)  rep78(4)  rep78(5)
              ----------------------------------------------------
              b       0.00    783.90   1379.12   2068.27   3245.27
              se             1911.99   1765.47   1802.43   1884.18
              ----------------------------------------------------
              Other utility functions lmatrixtools for renaming are:
              Code:
              . mata: nhb_sae_unique_values("foreign")
                     1   2
                  +---------+
                1 |  0   1  |
                  +---------+
              . mata: nhb_sae_labelsof("foreign")
                            1          2
                  +-----------------------+
                1 |  Domestic    Foreign  |
                  +-----------------------+
              . mata: nhb_sae_labelsof("foreign", 1)
                Foreign
              Kind regards

              nhb

              Comment


              • #8
                Neils, Thank you! I will check these out.
                -------------------------------------------
                Richard Williams, Notre Dame Dept of Sociology
                Stata Version: 17.0 MP (2 processor)

                EMAIL: [email protected]
                WWW: https://www3.nd.edu/~rwilliam

                Comment

                Working...
                X