Announcement

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

  • Add summary statistics in a graph

    Hey,

    I am plotting the graph of an event study. I want to plot in the graph some information like the mean of the dependent variable, the avg effect of the treatment, etc.

    For example, suppose I have the following depend variable:

    set obs 100
    gen dep_var = rnormal()

    I would like to run:

    mean dep_var
    matrix mean = e(b)

    and plot the following in the graph:

    Mean depend variable = -.1053

    Where I use the matrix "mean" to obtain the value that is plotted.

    There is a way to do that?

  • #2
    Welcome to Statalist.

    You can save the value as a local variable and then apply that. Here is an example. Notice that there is also another option to add the value if it's a bar graph:

    Code:
    sysuse auto, clear
    mean mpg
    local m1 = e(b)[1,1]
    
    graph bar mpg, blabel(bar) note("Or, the mean can be put here: `m1'")
    And if it's a twoway graph, like a scatter plot, it's possible to add text by coordinates as well. See https://www.stata.com/manuals/g-3added_text_options.pdf.

    Comment


    • #3
      Originally posted by Ken Chui View Post
      Welcome to Statalist.

      You can save the value as a local variable and then apply that. Here is an example. Notice that there is also another option to add the value if it's a bar graph:

      Code:
      sysuse auto, clear
      mean mpg
      local m1 = e(b)[1,1]
      
      graph bar mpg, blabel(bar) note("Or, the mean can be put here: `m1'")
      And if it's a twoway graph, like a scatter plot, it's possible to add text by coordinates as well. See https://www.stata.com/manuals/g-3added_text_options.pdf.
      Thanks so much! That was what I was looking for.
      Last edited by filipe cavalcanti; 17 Sep 2021, 22:02.

      Comment


      • #4
        Just a follow-up question: what if the graph is not displaying the round number? I'm using round(e(b)[1,1], 0.001) and the graph is showing a number like 9.54000000001

        Comment


        • #5
          Using round() in Stata is a poor way to get rounding to so many decimal places, puzzling though that may seem. This is often explained here and indeed elsewhere. round() is generally fine for getting integer results.

          The underlying reason is that Stata works in binary and many, indeed most, simple decimals just can't be held exactly in binary. Usually this is hidden from you by default display formats, but it has to be true at machine level.

          Which of 0.1 0.2 ... 0.8 0.9 can be held exactly in binary? Only 0.5 which is, naturally, 1/2. So 1 out of 9 possibilities.

          Which of 0.01 0.02 .... 0.98 0.99 can be held exactly in binary? Only 0.25, 0.50, 0.75, which are 1/4, 1/2, 3/4. So 3 out of 99 possibilities.

          And so on. (The (finite) number of bits you throw at the problem is an important side-issue. The point is that even 0.1 can never be held exactly in binary.)

          Rounding in Stata -- meaning choosing a particular number of decimal places -- is in Stata a string operation and should be approached as a matter of specifying a display format.

          Backing up, Ken Chui advised


          You can save the value as a local variable and then apply that
          where he means "local macro". In Stata "local variable" is what many users say if they are thinking of other languages, which is fine among consenting adults, but it's not official terminology.

          However, sometimes the local macro is just an extra stage that can be cut out. One of Ken's examples can be rewritten as


          Code:
          sysuse auto, clear  
          su mpg, meanonly    
          graph bar mpg, blabel(bar) note("Or, the mean can be put here: `r(mean)'")
          No local macro needed! Admittedly, that looks like a local macro, but it is best thought of as a local macro persona possessed by an r-class result.

          However, there is a sting. You must use the returned result while it is still available, as it will get overwritten by the next r-class command.

          To move to #4 here is the best way to get 3 decimal places


          Code:
          sysuse auto, clear  
          su mpg, meanonly  
          local mean : di %4.3f  r(mean)  
          graph bar mpg, blabel(bar) note("Or, the mean can be put here: `mean'")
          Now in that case a local macro is useful. You could do this in other ways.


          Code:
          sysuse auto, clear  
          su mpg, meanonly  
          graph bar mpg, blabel(bar) note("Or, the mean can be put here: `: di %4.3f r(mean)'")
          As always, the choice between being clear and being clever is tricky.



          Last edited by Nick Cox; 18 Sep 2021, 04:25.

          Comment


          • #6
            Thanks!

            Comment

            Working...
            X