Announcement

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

  • Histogram: Include variable in xlabel

    Hi all,

    I'm new to STATA. I just started using it for the first time yesterday. I have some experience coding in C++ and IDL.

    I have a small question related to inserting a variable in xlabel to automate a specific part of creating a histogram.

    Currently, in xlabel I have to manually write the mean (see code below). I'd like to know how I can put in a variable which represents the mean.

    It's a small thing, but I like to automate my code as far as possible because I eventually want to write a for loop to create many histograms.

    I tried using r(mean) after running the summarize function (e.g. replacing 50 with r(mean)), but it didn't work...

    Do you know an efficient way of solving my problem? Please ask if anything was unclear.

    Thanks in advance for any help.

    Code:
    ** from this I can get the mean manually
    sum hist_data 
    
    ** Create histogram
    histogram hist_data, bin(20) start(0) xaxis(1 2) xlabel(50 "mean", axis(2) grid gmax)


















  • #2

    Code:
    sum hist_data  
    histogram hist_data, bin(20) start(0) xaxis(1 2) xlabel(`r(mean)' "mean", axis(2) grid gmax)
    Tiny terminology traps:

    1. The mean used like that wouldn't be called a variable in Stata. It's just a stored result with macro persona.

    2. summarize is a command, not a function. Commands and functions are different beasts in Stata,

    Comment


    • #3
      Hi Nick,

      Thanks so much for your quick reply! Worked perfectly, I wasn't too far off!

      I have a follow up question: I want to display the mean subtracted by standard deviation. However, the code below gives me an error (to be honest, I didn't expect it to work).

      Code:
      sum hist_data    
       histogram hist_data, bin(20) start(0) xaxis(1 2) xlabel(`r(mean)' "mean" `r(mean)'-`r(sd)' "-1 s.d.", axis(2) grid gmax)

      My guess is that I need to create a new 'macro persona' (but are these only generated by commands?) which contains the number I want and then I call it with ` ' syntax.

      Tried something like this and varied the syntax a bit, but couldn't get it to work:

      Code:
      gen Num1 = r(mean) - r(sd)
      Any ideas? Thanks

      ps. Num1 pops up in my list of variables, so I'm guessing this is a variable





      Comment


      • #4
        There is scope to do calculations on the fly, so you could do this:

        Code:
        sum hist_data    
        histogram hist_data, bin(20) start(0) xaxis(1 2) xlabel(`r(mean)' "mean" `=`r(mean)'-`r(sd)'' "-1 s.d.", axis(2) grid gmax)
        See help macro

        I would tend to advise this at this stage

        Code:
        sum hist_data    
        local mM1sd = r(mean) - r(sd)
        histogram hist_data, bin(20) start(0) xaxis(1 2) xlabel(`r(mean)' "mean" `mM1sd'  "-1 s.d.", axis(2) grid gmax)
        See also http://www.stata.com/statalist/archi.../msg01258.html on terminology.
        Last edited by Nick Cox; 29 Apr 2016, 04:18.

        Comment

        Working...
        X