Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Adding normal density to overlayed histograms


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: Adding normal density to overlayed histograms
Date   Thu, 21 Oct 2010 10:03:40 +0200

Am Mittwoch, den 20.10.2010, 21:39 -0700 schrieb Dorothy Bridges:
> Hi everyone,
> 
> I am overlaying two histograms and would like Stata to add a normal
> density curve for each.
> 
> hist x, normal addplot(hist x2)
> 
> works fine, but
> 
> hist x, normal addplot(hist x2, normal)
> 
> tells me that normal is not an option.  Any ideas as to why this is happening?


This might seem to be overly complicated, but something like this can be
also produced with -graph twoway-. On the plus side it gives you more
leverage to design the figure and arguably makes a clearer code:

------------------------------------------------------

sysuse auto, clear

sum mpg, meanonly
local min = r(min)
local max = r(max)

sum mpg if for
local m1 = r(mean)
local s1 = r(sd)

sum mpg if !for
local m2 = r(mean)
local s2 = r(sd)

graph twoway ///
  || hist mpg if for, bstyle(p1)  ///
  || hist mpg if !for, bstyle(p2) ///
  || function y1 = normalden(x,`m1',`s2') ///
     , range(`min' `max') lstyle(p1) ///
  || function y2 = normalden(x,`m2',`s2') ///
     , range(`min' `max') lstyle(p2)
  
----------------------------------------------------------  

Leaving aside the question whether overlaying histograms makes sense,
what I am trying to say is: -graph twoway- rocks!

Many regards

Uli




















> 
> Thanks,
> Dorothy
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index