Announcement

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

  • Assign ereturn matrix component to macro

    I'd like to assign the mean and standard error (SE) of a variable to macros. I can do this using the return values from the summarize command, though I have to calculate the SE by hand:
    Code:
    sysuse auto, clear
    summarize mpg
    return list
    local mean = `r(mean)'
    local se = `r(sd)'/sqrt(`r(N)')
    di "The mean mpg is `mean' with se `se'."
    I should also be able to do it after the mean command, since that outputs the SE explicitly. However, the SE is not in the return list. Instead, it's the square root of component [1,1] in the ereturn macro e(V). How do I get it from e(V) into a local macro called se?
    Code:
    mean mpg
    ereturn list
    Also: why does one of these otherwise similar commands use return values while the other one uses ereturn values? I think I need a broader education on ereturn vs return and how to manipulate macros vs scalars....

  • #2
    Estimation commands will store their results in e. mean here is an estimation command. In addition, some generate a results table stored in r, which may be easier to work with. A recent thread highlights that the results table has not been well documented by StataCorp.

    Code:
    mean mpg
    local mean= e(b)[1,1]
    di "`mean'"
    mat list r(table)
    However, the SE is not in the return list. Instead, it's the square root of component [1,1] in the ereturn macro e(V). How do I get it from e(V) into a local macro called se?
    Code:
    local se= r(table)[3, 1]
    local se2= r(table)["se","mpg"]
    Last edited by Andrew Musau; 12 Aug 2020, 13:13.

    Comment


    • #3
      What Andrew shows is a novel feature of Stata 16. In older versions of Stata these system matrices cannot be manipulated quite like normal Stata matrices.

      Here is what happens if you try Andrew's code on Staa 15.1 and below:

      Code:
      . local mean= e(b)[1,1]
      invalid syntax
      r(198);
      
      . matlist e(b)
      
                   |       mpg 
      -------------+-----------
                y1 |   21.2973 
      
      . local mean= el(e(b),1,1)
      matrix operators that return matrices not allowed in this context
      r(509);
      
      . mat e = e(b)
      
      . local mean= e[1,1]
      
      . di "`mean'"
      21.2972972972973
      
      . local se= r(table)[3, 1]
      invalid syntax
      r(198);
      
      . mat table = r(table)
      
      . local se= table[3, 1]
      
      . dis `se'
      31.666438
      
      .

      Comment


      • #4
        Joro Kolev, pre-Stata 16 syntax for matrix extraction is horrible, especially if you have to refer to the column or row names. See post #11 of the following link. I am glad that StataCorp made improvements in the current version.

        https://www.statalist.org/forums/for...pecific-output
        Last edited by Andrew Musau; 12 Aug 2020, 16:28.

        Comment

        Working...
        X