Announcement

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

  • Accessing model results from Mata

    I know the functions st_global(), st_numscalar(), and st_matrix() can be used to access information left over in e-/r-return values. However, if I wanted to access results based on the coefficient legend and variable names (e.g., _b[var1], _se[var1]), is there a way to do this from Mata?

  • #2
    Presumably you've tried the following and, like me, found it desperately lacking in elegance.
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . quietly regress mpg weight
    
    . display _b[weight]
    -.00600869
    
    . mata: 
    ------------------------------------------------- mata (type end to exit) -----------------------
    : stata("scalar b = _b[weight]")
    
    : st_numscalar("b")
      -.0060086868
    
    : end
    -------------------------------------------------------------------------------------------------
    
    .

    Comment


    • #3
      So assuming the estimation commands are regress and/or xtreg it seems that it is possible to access the information via r(table). It isn't quite the same, but definitely isn't too much effort to parse through things.

      Comment


      • #4
        I have two suggestions:

        Example code:
        Code:
        cls
        sysuse auto
        regress price weight mpg
        The fast track (not pretty, but simpler as William Lisowski and close to what wbuchanan ask for):
        Code:
        . mata
        : x = `=_b[weight]'
        : x
          1.746559158
        : end
        In most cases I use matrices r(table), e(b) or e(V) and the class nhb_mt_labelmatrix (package matrixtools):
        Code:
        . mata:
        : lm = nhb_mt_labelmatrix()
        : lm.from_matrix("e(b)")
        : lm.regex_select(str_regex="weight",names=1, names=1, row=0).print()
          
        ----------
            weight
        ----------
        y1    1.75
        ----------
        : end
        Kind regards

        nhb

        Comment


        • #5
          Niels Henrik Bruun
          where is that matrix package from? My use case is related to managing/persisting results during simulation studies in a way that is more flexible/easy than hard coding a bunch of stuff.

          Comment


          • #6
            wbuchanan : -ssc install matrixtools- The base of most of my coding is lmatrixtools.mlib/lmatrixtools.mata therein.
            It is on my TODO list to document the code, so you have settle with the lmatrixtools.mata file for now.
            Kind regards

            nhb

            Comment


            • #7
              Niels Henrik Bruun
              I'll definitely take a look at it. Do you host your code anywhere? If I end up using any of the stuff you've written and creating any documentation I'd be more than happy to share.

              Comment


              • #8
                If in addition to the above advice it might be helpful to automate retrieval of parameter names, then st_matrixcolstripe might be helpful. For instance:
                Code:
                cap preserve
                cap drop _all
                
                sysuse auto
                reg price mpg foreign
                
                mata
                
                pnames="_b[":+st_matrixcolstripe("e(b)")[.,2]:+"]"
                pnames
                
                end
                
                cap restore

                Comment


                • #9
                  John Mullahy
                  I get how that example constructs the strings that would be used to reference those values in Stata, but the challenge is accessing the information in any of the r()/e() values that are left over from any commands that people may be using. I'm not sure how many estimation commands do/don't leave results in r(table), but am hoping that many do at the moment.

                  Comment


                  • #10
                    wbuchanan Thank you for the offer in #7. My code is not hosted outside my computer and the ssc.
                    For now my plan is to document my code during this year.

                    If it is a matter of retrieving output from return or ereturn, consider:

                    Code:
                    . sysuse auto
                    . su price, d
                    . mata x=nhb_sae_stored_scalars()
                    . mata x.print()
                      
                    --------------------
                               r scalars
                    --------------------
                    N              74.00
                    sum_w          74.00
                    mean         6165.26
                    Var       8699525.97
                    sd           2949.50
                    skewness        1.65
                    kurtosis        4.82
                    sum        456229.00
                    min          3291.00
                    max         15906.00
                    p1           3291.00
                    p5           3748.00
                    p10          3895.00
                    p25          4195.00
                    p50          5006.50
                    p75          6342.00
                    p90         11385.00
                    p95         13466.00
                    p99         15906.00
                    --------------------
                    
                    . mata x.regex_select("m").print()
                      
                    ----------------
                           r scalars
                    ----------------
                    sum_w      74.00
                    mean     6165.26
                    sum    456229.00
                    min      3291.00
                    max     15906.00
                    ----------------
                    
                    . mata x.regex_select("m").to_matrix("mat")
                    
                    . matprint mat
                      
                    ----------------
                           r scalars
                    ----------------
                    sum_w      74.00
                    mean     6165.26
                    sum    456229.00
                    min      3291.00
                    max     15906.00
                    ----------------
                    Kind regards

                    nhb

                    Comment


                    • #11
                      Niels Henrik Bruun
                      Have you figured anything out to retrieve values referenced using the coefficient legend names (e.g., _b[varname], _se[varname], etc...)?

                      Comment


                      • #12
                        Sorry, no. Not exept what is described in #2 and #4.
                        #8 or
                        Code:
                        lm.row_names()
                        might also be of use.
                        I think I would go for retrieving e(b) and e(V) into nhb_mt_labelmatrix() and take from there
                        Kind regards

                        nhb

                        Comment


                        • #13
                          One way to way to convert _b[] references to something more Mata-like is with makecns. makecns, you could say, is nature's way of converting parameter references to matrices:

                          Code:
                          sysuse auto
                          regress price mpg
                          constraint 1 _b[mpg]=0
                          makecns 1
                          matlist e(Cns)
                          The subinstr() function or subinstr macro function (help macro##macro_fcn) could convert _se reference to _b.

                          In general I think good design will do such translation in Stata, then pass it to Mata. But there can always be exceptions.
                          Last edited by David Roodman; 15 Mar 2021, 08:47.

                          Comment


                          • #14
                            David Roodman
                            The challenge with that approach is constructing all of the constraints efficiently. The idea that Niels mentioned above seems to be fairly efficient and can handle arbitrarily large problems fairly easily. In order to keep things a bit lighter weight, I’m grabbing the rowstripe off of e(V) and then concatenation the strings across the columns to create a single reference string that should be relatively easily matched to variable names.

                            Comment

                            Working...
                            X