Bivariate kernel density estimation
kdens2 varnameY varnameX [if exp] [in range] [, n(#) xwidth(#) ywidth(#) saving(name) replace nodraw ]
kdens2 produces bivariate kernel density estimates using a Gaussian kernel and graphs the result using Adrian Mander's surface plot routine. If Y- and X-variable labels are available, they are used to label the graph.
n(#) specifies the number of points on the Y- and X-axes at which the density estimate is to be evaluated. The default is min(_N,50).
xwidth(#) specifies the halfwidth of the kernel, the width of the density window around each point of varnameX. If xwidth() is not specified, then the "optimal" width is used; see kdensity.
ywidth(#) specifies the halfwidth of the kernel, the width of the density window around each point of varnameY. If ywidth() is not specified, then the "optimal" width is used; see kdensity.
saving(name) causes the graph to be saved as name.gph. If this graph already exists, the replace option must be used. The option also causes the data underlying the graph to be written to name.dta. If this file already exists, the replace option must be used.
nodraw specifies that the 3D graph should not be drawn. However, if the saving option is invoked, the resulting data will be saved, and may be fed to another graphing routine such as Stata 12's twoway contour or twoway contourline.
. webuse grunfeld, clear
. gen linv = log(invest)
. label var linv "Log[Investment]"
. gen lmkt = log(mvalue)
. label var lmkt "Log[Mkt value]" . kdens2 linv lmkt
. kdens2 linv lmkt, n(100)
. kdens2 linv lmkt, xw(.5) yw(.5)
. kdens2 linv lmkt, saving(bivar) replace
Christopher F. Baum, Boston College, USA email@example.com
The graphics produced by this routne are generated by Adrian Mander's surface routine. Thanks to Friedrich Huebler and Maria Cecilia Calderon for identifying bugs and Tom Steichen for constructive suggestions.