Announcement

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

  • Help creating a frequency histogram with a proportion curve?

    Hello!

    I am trying to make a specific type of histogram in Stata 12.1 and am not sure how to get there.

    Thus far I have a histogram that looks like this:

    Graph.png

    Here is my code if it's necessary or there is interest:

    Code:
    histogram manres if manres<80, freq xtitle("Tuberculin Skin Reaction Size, millimeters") color(green) lcolor(black) xlabel(0(10)35) ylabel(0(50)200)

    I want to overlay a curve that communicates the proportion positive depending on the cutoff used. I saw an example of this graph from the figure below.

    Histogram.jpg



    Sorry that I couldn't make the photo focus in on the figure because I had to print screen the image. Hopefully that is not distracting.
    If you would like to see the paper in which this figure is displayed I am putting the citation below this message.
    In this figure they put the frequency on the left and the proportion positive depending on the cutoff of the TST used on the
    right hand side. Is a figure like this possible in Stata?


    Thanks for any help you can provide! Have a great weekend!
    Leo




    Figure 1A from
    Andrews, Jason R., Mark Hatherill, Hassan Mahomed, Willem A. Hanekom, Monica Campo, Thomas R. Hawn, Robin Wood, and Thomas J. Scriba. "The Dynamics of QuantiFERON-TB Gold In-Tube Conversion and Reversion in a Cohort of South African Adolescents." American journal of respiratory and critical care medicine (2015).





  • #2
    I'm pretty sure you'll find more ways to do that, here is one approach with twoway__histogram_gen. Just replace the var_to_use_for_proportion with your variable defining the proportion positive:

    Code:
    ​g manres2=manres if manres<80
    twoway__histogram_gen manres2, freq bin(20) gen(h x)
    su x if x<.
    local max=r(N)
    local a =x[1]
    di "`a'"
    forval i=2/`max' {
        local a = "`a',`=x[`i']'"
    }    
    egen bin=cut(manres2), at(`a')
    bys bin: egen proportion=mean(var_to_use_for_proportion)
    tw hist manres2, freq bin(20) yaxis(1) || line proportion bin, sort yaxis(2)

    Comment


    • #3
      Thanks so much. That did work. I have a couple of questions though on final graph which I am putting below:
      Click image for larger version

Name:	Newhisto.png
Views:	1
Size:	18.4 KB
ID:	1315885



      Here is my code:

      Code:
      bys bin: egen proportion=mean(proportioncutoff)
      tw hist manres2, freq bin(20) color(gs8) lcolor(black) yaxis(1) legend(off) ylabel(0(75)450) xlabel(0(5)40) || line proportion bin, sort yaxis(2) xtitle("Tuberculin Skin Reaction Size, millimeters")
      For some reason when I do this 0 is not added even though this is present in the "proportioncutoff" variable. it should be 100% for this value and the line should split to the top. Is there a reason this is not added and if so how can I make the line take into account 0 here. Thanks again for your help!

      Leo



      Comment


      • #4
        I can't say much without reproducible example, I can just guess that there is not specific bin for zero so you don't get the proportion just for zero but the mean proportion in the first bin of manres. type the following to see what the graph show:
        Code:
        tabstat proportioncutoff if !mi(manres2),by(bin) s(mean n)

        Comment

        Working...
        X