Bookmark and Share

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

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

Re: st: strange kdensity behavior

From   Rebecca Pope <>
Subject   Re: st: strange kdensity behavior
Date   Fri, 31 May 2013 15:29:43 -0500

Thanks Austin and Nick.

I missed the nuance that the -addplot- option used -twoway kdensity-.
Setting the number of estimation points does indeed ensure equivalent
results. That said, no amount of adjusting the estimation points seems
to improve the fit of the curve for std. dev. > 1. I think I'll just
stick to generating the density for my reference distributions from
their pdf's.

For posterity, Nick's suggestion to -generate- the density and
re-graph produces "prettier" graphs if the -at(varname)- option is
specified for -kdensity-. This means, however, that the -n()- option
can't be used.

Building on the code I already posted, this just illustrates the
"zoom" that I mentioned earlier and shows the difference between the
pdf and the kernel density estimate.

/* True Lognormal pdf evaluated at drawn value of X */
gen pdfx = 1/(lognorm*sqrt(2*_pi*`sig'^2))*exp(-((ln(lognorm)-`mu')^2)/(2*`sig'^2))

/* Obtain estimates of the density */
kdensity lognorm, generate(dens) at(lognorm) nograph
sort lognorm
twoway (line dens lognorm if lognorm <= 5, lpattern(dash)) (line pdfx
lognorm if lognorm <= 5), ///
 ytitle(Density) legend(order(1 "Kernel Density Estimate" 2 "Lognormal pdf"))


Thanks again to both of you.


On Fri, May 31, 2013 at 1:13 PM, Nick Cox <> wrote:
> I guess you'll need to -generate- the density and the corresponding
> variable values, then  re-graph.
> Nick
> On 31 May 2013 17:15, Rebecca Pope <> wrote:
>> As an add-on question, is there a way to "zoom in" on the lower end of
>> the plot? If I use -if-, -kdensity- calculates the density over that
>> range (which I don't want).
>> To see this:
>> kdensity lognorm if lognorm<=10, addplot((kdensity lognorm if
>> lognorm<=5, lpattern(dash))) ///
>> legend(order(1 "Default Plot" 2 "Added Plot")) name(plot2)
>> You'll notice that the greatest change is in Default Plot.
>> If I use the x-axis options, it just changes the labeling without
>> excluding the higher values of X.
>> I'm running Stata 12.1 on Windows 7. Stata is updated to 20 Mar 13 &
>> when I checked for updates, nothing new showed up.
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

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