Announcement

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

  • Directly accessing scalars inside r- or e-class matrices.

    Dear Stata community,

    when I want to access a coefficient value after running a regression, one way to do that is by first storing the coefficient vector and then accessing the specific coefficient of interest as follows:
    Code:
    clear
    sysuse auto
    
    reg mpg foreign
    matrix define coeff = e(b)
    local b1 = coeff[1,2]
    I've recently come accross a code which shortcuts this same procedure by directly accessing the specific value of the e(b) vector without using "matrix define" as in the following:
    Code:
    clear
    sysuse auto
    
    reg mpg foreign
    local b1 = e(b)[1,2]
    When I try to run the code, it doesn't work, i receive an "invalid syntax" error. However, the code I came accross uses this technique so frequently that I doubt that it is simply a mistake. Is there anything in the Stata settings I can change to make the code "legal" or does it only work for a specifc Stata version? I'm using Stata 15.

    Thank you

  • #2
    I use this technique all the time when I'm making graphs. Could you please show us where the error occurred at? Like show the full context please, including what you team as well as what Stata complained about? Because so far, this looks syntactically legal to me.

    Comment


    • #3
      If I run the last line in the second code block, stata will return

      Code:
      . local b1 = e(b)[1,2]
      invalid syntax
      r(198);

      Comment


      • #4
        Originally posted by Michael Helders View Post
        I'm using Stata 15.
        This feature, i.e., subscription global/system matrices was introduced in Stata 16. In Stata 15 (and before), use

        Code:
        local b1 = el(e(b), 1, 2)
        Edit: The el() function above does not seem to work with e(b) in Stata 14 (to which I have access). It does work with r(table).
        Last edited by daniel klein; 29 Apr 2022, 07:23.

        Comment


        • #5
          This makes sense, thank you!

          Comment


          • #6
            Daniel explained why what you are trying does not work on Stata 15, it is a new feature introduced later.

            I used Stata 15 until recently, say less than a year ago, and my conclusion was that the most robust approach was just to assign these system matrices like e(v) and e(V) to a normal Stata matrix, and then work with the standard Stata matrix, like you were doing in the beginning.

            Again as Daniel reports, the work around through the el() function works in some contexts, and does not work in others.

            The safest and fastest way to handle the situation is just to accept that before Stata 16 system matrices like e(v) and e(V) do not quite behave like normal Stata matrices, and to assign them to normal Stata matrices immediately after estimation, if you intend to access their content.

            Comment


            • #7
              Prior to Stata 16, I found this so frustrating that I wrote a little utility called -mlu- (for "matrix lookup-) to facilitate extracting matrix elements. Some users, especially those with version 15 or prior, may find it easier to remember and use than the Stata code,

              Code:
              view net describe mlu, from("http://digital.cgdev.org/doc/stata/MO/Misc")
              The companion package -mluwild- (think "matrix lookup using wild-card characters") exploits Stata's row- and column names conventions to extract a sub-matrix defined by expressions containing wild-card characters:

              Code:
              view net describe mluwild, from("http://digital.cgdev.org/doc/stata/MO/Misc")
              Examples in the help file for -mluwild- are:
              Example 1: Extracting columns with names beginning with the letter "p" from an arbitrary Stata matrix
              Example 2: Extracting coefficients for all levels of a factor variable from the matrix returned by a single-equation estimation command
              Example 3: ... or from all equations of a multi-equation estimation command
              Example 4: Select a sub-matrix based on the equation part of the row or column name

              Comment

              Working...
              X