Announcement

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

  • Histogram overlaid by a normal curve

    I'm trying to draw histograms of IQ by sex overlaid by normal curves with mean 100 and sd 15.

    This works beautifully:

    graph twoway (histogram iq, by(, legend(off)) by(sex, cols(1)) xtit(IQ) ytit(Density) xlab(20(20)180) legend(off)) (function normalden(x,100,15), range(20 180) xsize(3.5) ysize(4.5))

    Except the y-axis is density and I want percent.

    I can get percent with this:

    histogram iq, percent normal by(, legend(off)) by(sex, cols(1)) xtit(IQ) xlab(20(20)180) xsize(3.5) ysize(4.5)

    But, I can't specify the mean and sd of the normal--they are drawn from the data.

    Is there a way to draw percent histograms overlaid by normal curves with my specified mean 100 and sd 15?

  • #2
    I am not a Stata graphics expert, but it seems that you want some sort of overlay. Based on my copy of A Visual Guide to Stata Graphics, you might try something like the following:

    twoway (histogram iq, etc etc) (line x y)

    where line shows your normal curve. I don't have experience with that approach though. If you work out the proper code please share!

    Comment


    • #3
      Liz: David has pointed to a good solution. Here are some details in an example. Superimpose a Gaussian with mean 25 and SD 5.

      Code:
       
      sysuse auto
      histogram mpg , percent width(2) addplot(function 100 * 2 *  normalden(x,25,  5), ra(mpg)) legend(order(2 "Gaussian with mean 20, SD 5") pos(1) ring(0))
      The two factors needed are bin width (here 2) and 100 to scale from probability per unit width to percent per unit width. You don't have to draw the curve over the same limits as the variable; you can superimpose your own limits.

      Click image for larger version

Name:	liz.png
Views:	1
Size:	39.0 KB
ID:	700168

      Comment


      • #4
        Thanks Nick, this is perfect. I had stumbled on addplot but couldn't figure out how to rescale normalden. I knew I needed to multiply by 100 but didn't catch on to needing the bin width, too. I have about 40 of these to draw and want them to have the same x-axis. Since some of the variables will have values in the upper tail, I'm using range 20 to 80 which should cover all possibilites. I must confess, I was focused on solving this with twoway because Nick always says most any graph problem can be solved with it. :-)

        This came up because a colleague was drawing this graph with SAS but it wasn't at all as attractive as what I was sure could be done in Stata. On the other hand, when he requested percent or frequency, SAS rescaled the normalden curve automatically.

        Here is the code that works:

        histogram iq, percent width(10) by(, note(, size(zero) color(edkbg))) by(, legend(off)) by(sex, cols(1)) addplot(function 100*10*normalden(x,100,15), range(20 180)) ytitle(Percent) xtitle(IQ) xlab(20(20)180) xsize(3.5) ysize(4.5)

        And here's how it looks:

        Click image for larger version

Name:	IQ.png
Views:	7
Size:	36.6 KB
ID:	701051
        Attached Files

        Comment

        Working...
        X