Announcement

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

  • How to -display- elements of a matrix

    How do I -display- elements of a matrix, such as e(b)?

    display e(b)

    is "invalid syntax" while Stata 14 responds to

    display e(b)[1,1]

    with "matrix operators that return matrices not allowed in this context". I am also interested in putting e(b) in the -post- command, but get the same message.

  • #2
    Code:
    matrix list e(b)

    Comment


    • #3
      The problem is not so much with display but with the fact that e(b) is a "special" matrix. If you first turn it into a "normal" Stata matrix then it works:

      Code:
      sysuse auto
      regress price mpg i.rep78
      matrix b = e(b)
      di b[1,1]
      di e(b)[1,1]
      The same is true for post.
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Maarten is right, this is the case for all "special" matrix that Stata returns, you cannot directly display its elements.

        However, there is an alternative way of doing that (instead of generating a new matrix), using el() function as suggested William Lisowski in this post.

        Code:
        sysuse auto, clear
        regress price mpg i.rep78
        local b1=el(r(table),1,1)
        di `b1'
        This is more elegant if you want to display b(1,1) to store it in a local afterward. Here you can store it before (without) displaying it.

        Note that this doesn't work for matrix e(b), but the matrix e(b) is actually the first row of matrix r(table)
        Best,
        Charlie

        Comment


        • #5
          Where can we read more about the difference between "normal" and "special" matrices?

          Comment


          • #6
            Well, rather than invest your time in learning this distinction, I suggest you instead invest some money in getting a more modern version of Stata than version 14. Since at least version 17, and I think it was earlier, you can work with these "special" matrices as if they were "regular" matrices.

            Code:
            . version
            version 18.5
            
            . sysuse auto, clear
            (1978 automobile data)
            
            . regress price mpg headroom trunk
            
                  Source |       SS           df       MS      Number of obs   =        74
            -------------+----------------------------------   F(3, 70)        =      7.46
                   Model |   153861671         3  51287223.7   Prob > F        =    0.0002
                Residual |   481203725        70  6874338.93   R-squared       =    0.2423
            -------------+----------------------------------   Adj R-squared   =    0.2098
                   Total |   635065396        73  8699525.97   Root MSE        =    2621.9
            
            ------------------------------------------------------------------------------
                   price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
                     mpg |  -224.3597   65.27511    -3.44   0.001    -354.5468   -94.17263
                headroom |   -659.463   484.5101    -1.36   0.178    -1625.788    306.8619
                   trunk |   126.6049   107.2399     1.18   0.242    -87.27846    340.4882
                   _cons |   11175.77   2431.134     4.60   0.000     6327.029    16024.52
            ------------------------------------------------------------------------------
            
            . display e(b)[1, 3]
            126.6048
            Now, even with this change in Stata's behavior, remember that whatever appears in r() goes away as soon as another command puts something in r(). So for "special" matrices returned in r() you must either manipulate them before another command overwrites r(), or you must save it as a "regular" matrix before that happens. Since most commands do overwrite r() this means that you have to either manipulate or capture those results very quickly, usually immediately after the command that generates them.

            The same is true of e(), but because only estimation commands overwrite e(), you typically have more time to get to those results.

            Note: I just now noticed that you are not the person who started this thread and was using version 14. Anyway, if you are using version 17 or later (and I think this worked in version 16, too, but I don't remember it clearly) there really isn't any point to learning about the "special" matrices any more. If you are using a version older than that, if you have the money to buy a more recent version, I really do think that upgrading makes more sense than learning about an obscure feature of an old version of Stata that you can always work around as shown in #3.

            Comment


            • #7
              From

              Code:
              help whatsnew15to16
              1.3.15 What's new in programming
              [...]
              8. Matrix subscripting and extraction are now allowed with r() and e() matrices, including e(b), e(V), and e(Cns).
              [...]
              9. Matrix functions returning scalars now work with r() and e() matrices.
              The matrices e(b), e(V), and e(Cns) remain "special" in the sense that the only way to create (or change) them is ereturn post.

              Comment

              Working...
              X