Announcement

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

  • Histogram: how to save bin, start and width information?

    Dear statalist,

    When Stata runs histogram, it provides a little note on-screen with the number of bins, start value and bin width, e.g.:

    Code:
    sysuse auto
    hist weight
    which then displays

    (bin=8, start=1760, width=385)

    I would like to put these values into local macros. It would be almost as good to put the whole string into a local, or to add the string to the graph as a note.

    Is this possible?

    I have a somewhat complicated workaround that recalculates these values but needless to say it would be simpler just to capture the work Stata is already doing.

    Best,
    BL

    PS - Stata 16.1 MP (17 Sep 2020) on W10

  • #2
    Not ideal, but you can log the session.

    Code:
    sysuse auto, clear
    preserve
    qui log using firstfile, name(log1) text replace
    hist weight
    log close log1
    insheet using "firstfile.log", clear
    keep if ustrregexm(v1, "(bin=[0-9]+, start=[0-9]+, width=[0-9]+)")
    local wanted = "`=v1[1]'"
    restore
    di "`wanted'"
    Res.:

    Code:
    . di "`wanted'"
    (bin=8, start=1760, width=385)

    Comment


    • #3
      Interesting question. Stata doesn't seem to save them anywhere.

      Not the question, but

      * I find I have a strong preference for "nice" bin edges so will not often accept histogram defaults any way. If pushed this view is more aesthetic than anything else, plus not wanting there to be little questions about why did you choose 385 as bin width. (Not a good answer: I just accepted the program default.)

      * There is a fair literature on how to choose the number of bins for histograms. In my reading it understates how often bin width is something you (should) choose knowing something about the variable and how it was measured. But it's impossible to automate that.

      * The same issue arises with kernel smoothers. I didn't usually like the results of e.g. lpoly at all which to my taste usually under-smoothed by default. It's best to play around and work out your own defaults.

      Really not the answer, but quantile plots do away with all these questions of start and width!
      Last edited by Nick Cox; 28 Sep 2020, 10:30.

      Comment

      Working...
      X