Announcement

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

  • An immediate histogram command?

    (Cross-posted from the v17 wishlist, https://www.statalist.org/forums/for...72#post1601072, at the suggestion of Nick Cox)

    Stata does not currently have an immediate version of –twoway histogram–. For example, imagine
    Code:
    twoway histi .4 0 .3 1 .2 2 .1 3, [standard twoway hist options]
    However, using –twoway scatteri– along with –recast– can deliver pretty much what's desired:
    Code:
    twoway scatteri .4 0 .3 1 .2 2 .1 3, recast(bar) ysc(r(0 .4))
    As with –twoway histogram– you'll probably want to prettify your visualization with other options and can use the –twoway bar– options to do so in the –scatteri– command.

    This suffices for my immediate (no pun intended) purposes, but if others have devised other solutions then please do share your ideas.

  • #2
    Thanks. I started writing something to do what you want, but got diverted because I don't much like your syntax going back and forth between values and probabilities. I would tend to get tripped up there and enter them out of order.

    But I have a positive suggestion of slightly different kind.

    I remembered something I published a while back but almost never use myself, except that now I am re-discovering that it helps here.


    Code:
    . search seqvar, sj
    
    Search of official help files, FAQs, Examples, and Stata Journals
    
    SJ-8-2  gr0034  . . . . . . . . . .  Speaking Stata: Between tables and graphs
            (help labmask, seqvar if installed) . . . . . . . . . . . .  N. J. Cox
            Q2/08   SJ 8(2):269--289
            outlines techniques for producing table-like graphs
    seqvar as published in 2008 puts integer sequences into variables, so this works

    .
    Code:
     seqvar values = 0/3
    and notice how a numlist notation (no credit to me, all credit to the company) is what you really, really want (cue: very surprising Spice Girls allusion) for more complicated examples.


    For your probabilities seqvar won't play but a very quick hack to cut out the integer restriction produced seqvar2

    Then I would recommend firing up twoway bar -- not histogram or twoway histogram -- because (a) you know the probabilities (frequencies) already and don't need them to be calculated and (b) it does what (I think) you want.


    Code:
    . seqvar2 prob = .4 .3 .2 .1
    
    . twoway bar prob values
    
    . twoway bar prob values, base(0)
    
    . twoway bar prob values, base(0) ytitle(Probability) barw(0.9)
    .

    This won't work unless you, the reader, install seqvar and then hack it to seqvar2.

    In all fairness I should underline that typing the values and probabilities directly into the Data Editor will often be about as much work (and it may be easier to edit small errors).

    At this moment I am tempted to release a more general version of seqvar -- the name numvar appears to be available -- unless and until I discover a compelling reason to restrict it to integers.


    Comment


    • #3
      Is anyone else as surprised as I am about Nick Cox referencing the Spice Girls in this response?

      Comment


      • #4
        I am more surprised than you are, regardless of how surprised you are.

        Comment

        Working...
        X