*! version 1.0 14apr2016

Title

mkdensity -- Plots kernel densities of several variables (wrapper of the kdensity command)

Syntax

mkdensity varlist [if] [in]
[weight]
[, options]

Main
kernel(mkdensity##kernel:kernel) specify kernel function;
default is kernel(epanechnikov)

bwidth(#) half-width of kernel

generate(newvar_x newvar_d) store the estimation points in variables name stub newvar_x and the density estimates in variables name stub newvar_d

n(#) estimate density using # points; default is min(N, 50)

over(varname) display densities of each category of varname in one plot

by(varname) combine plots by categories of varname into one graph

nograph suppress graph

xline(value) vertical line at value

save(name) save graph into name.png

title(string) title of graph into name.png

ycommon give y axes common scales when by() option is specified

xcommon give x axes common scales when by() option is specified

rows(#) | cols(#) display in # rows or # columns when by() option is specified

kernel

epanechnikov Epanechnikov kernel function; the default

epan2 alternative Epanechnikov kernel function

biweight biweight kernel function

cosine cosine trace kernel function

gaussian Gaussian kernel function

parzen Parzen kernel function

rectangle rectangle kernel function

triangle triangle kernel function

fweights, aweights, and iweights are allowed; see
weight.

Description

mkdensity produces kernel density estimates of several variables and
graphs the result. As a default, it plots the densities of the given variables
in one graph plot. It is also possible to handle panel data type structures
using the over() and/or the by() options. mkdensity uses the
kdensity command to calculate the underlying densities.

Options

Main

"kernel(mkdensity##kernel:kernel)" specifies the kernel function for use
in calculating the kernel density estimate. The default kernel is
the Epanechnikov kernel (epanechnikov).

bwidth(#) specifies the half-width of the kernel, the width of the
density window around each point. If bwidth() is not specified, the
"optimal" width is calculated and used; see [R] kdensity . The
optimal width is the width that would minimize the mean integrated squared
error if the data were Gaussian and a Gaussian kernel were used, so it is not
optimal in any global sense. In fact, for multimodal and highly skewed
densities, this width is usually too wide and oversmooths the
density (Silverman 1992).

"generate(newvar:newvar_x newvar_d)" stores the results of the
estimation. For each variable in the main varlist, the variable with name
starting with newvar_x will contain the points at which the densities are
estimated. Similarly, the variables with the beginning stub newvar_d will
contain the density estimates.

n(#) specifies the number of points at which the density estimate is to
be evaluated. The default is min(N,50), where N is the number of
observations in memory.

"over(varname)" displays densities of each category of varname in
one plot. For each variable in the main varlist, it calculates separate
densities for the subsamples identified by the distinct values (categories) of
varname. The option can be combined with the by() option.

"by(varname)" displays densities of each category of varname in
separate plots and combines these plots into one graph. For each variable in
the main varlist, it calculates separate densities for the subsamples
identified by the distinct values (categories) of varname. The option can
be combined with the over() option.

nograph suppresses the graph. This option is often used
with the generate() option.

Example

Setup

sysuse auto2, clear

Graph kernel density estimates for length and displacement
with a vertical line at 200

mkdensity length displacement, xline(200)

Graph kernel density estimates for price (both domestic and
foreign) in one plot

mkdensity price, over(foreign)

Graph kernel density estimates for domestic and foreign price
by different plots of repair record, combined into one graph

mkdensity price, over(foreign) by(rep78)

Graph kernel density estimates for for domestic and foreign price
and weight

mkdensity price weight, by(foreign)

Stored results

mkdensity stores the following in r():

Scalars

r(bwidth) kernel bandwidth

r(n) number of points at which the estimate was evaluated

r(scale) density bin width

Macros

r(kernel) name of kernel

Author

Szabolcs Lorincz

szabolcs@gmail.com

Reference

Silverman, B. W. 1992.
Density Estimation for Statistics and Data Analysis.
London: Chapman & Hall.