npcdensbw computes a conbandwidth object for
  estimating the conditional density of a $p+q$-variate kernel
  density estimator defined over mixed continuous and discrete
  (unordered, ordered) data using either the normal-reference
  rule-of-thumb, likelihood cross-validation, or least-squares cross
  validation using the method of Hall, Racine, and Li (2004).npcdensbw(...)## S3 method for class 'formula':
npcdensbw(formula, data, subset, na.action, call, \dots)
## S3 method for class 'NULL':
npcdensbw(xdat = stop("data 'xdat' missing"),
          ydat = stop("data 'ydat' missing"),
          bws, ...)
## S3 method for class 'conbandwidth':
npcdensbw(xdat = stop("data 'xdat' missing"),
          ydat = stop("data 'ydat' missing"),
          bws,
          bandwidth.compute = TRUE,
          fast = FALSE,
          auto = TRUE,
          nmulti,
          remin = TRUE,
          itmax = 10000,
          ftol = 1.19209e-07,
          tol = 1.49012e-08,
          small = 2.22045e-16,
          ...)
## S3 method for class 'default':
npcdensbw(xdat = stop("data 'xdat' missing"),
          ydat = stop("data 'ydat' missing"),
          bws,
          bandwidth.compute = TRUE,
          fast,
          auto,
          nmulti,
          remin,
          itmax,
          ftol,
          tol,
          small,
          bwmethod,
          bwscaling,
          bwtype,
          cxkertype,
          cxkerorder,
          cykertype,
          cykerorder,
          uxkertype,
          uykertype,
          oxkertype,
          oykertype,
          ...)
as.data.frame) containing the variables
    in the model. If not found in data, the variables are taken from
   np when a bandwidth search has been implied by a call to
    another function. It is not recommended that the user set this.conbandwidth
    object returned from a previous invocation, or as a $p+q$-vector
    of bandwidths, with each element $i$ up to $i=p$
    corresponding to the bandwidth for column $i$ in TRUE.cv.ml specifies likelihood cross-validation,
    cv.ls specifies least-squares cross-validation, and
    normal-reference just computes the TRUE the
    supplied bandwidths are interpreted as `scale factors'
    ($c_j$), otherwise when the value is FALSE they are
    interpreted as `raw bandwidths' ($h_j$ for continuous data
    typesconbandwidth object. Defaults to fixed. Option
    summary:
fixed: compute fixedFALSE, a conbandwidth object
    will be returned with bandwidths set to those specified
    in bws. Defaults to <xdat.  Can be set as gaussian,
    epanechnikov, or uniform. Defaults to gaussian.xdat (one of
    (2,4,6,8)). Kernel order specified along with a
  uniform continuous kernel type will be ignored. Defaults to
  2.ydat.
    Can be set as gaussian, epanechnikov, or
    uniform. Defaults to gaussian.ydat (one of
    (2,4,6,8)). Kernel order specified along with a
  uniform continuous kernel type will be ignored. Defaults to
  2.aitchisonaitken or
    liracine. Defaults to aitchisonaitken.aitchisonaitken.wangvanryzin or
    liracine. Defaults to wangvanryzin.wangvanryzin.TRUE the search routine
    restarts from located minima for a minor gain in accuracy. Defaults
    to TRUE. Defaults to TRUE.10000.1.19e-07
      (FLT_EPSILON).1.49e-08
      (sqrt(DBL_EPSILON)).2.22e-16 (DBL_EPSILON).npcdensbw returns a conbandwidth object, with the
  following components:xdatydatbwtype is set to fixed, an object containing
  bandwidths (or scale factors if bwscaling = TRUE) is
  returned. If it is set to generalized_nn or adaptive_nn,
  then instead the $k$th nearest neighbors are returned for the
  continuous variables while the discrete kernel bandwidths are returned
  for the discrete variables.  The functions predict, summary and plot support
  objects of type conbandwidth.
data.frame function to construct your input data and not
  cbind, since cbind will typically not work as
  intended on mixed data types and will coerce the data to the same
  type.  Caution: multivariate data-driven bandwidth selection methods are, by
  their nature, computationally intensive. Virtually all methods
  require dropping the $i$th observation from the data set, computing an
  object, repeating this for all observations in the sample, then
  averaging each of these leave-one-out estimates for a given
  value of the bandwidth vector, and only then repeating this a large
  number of times in order to conduct multivariate numerical
  minimization/maximization. Furthermore, due to the potential for local
  minima/maxima, restarting this procedure a large number of times may
  often be necessary. This can be frustrating for users possessing
  large datasets. For exploratory purposes, you may wish to override the
  default search tolerances, say, setting ftol=.01 and tol=.01 and
  conduct multistarting (the default is to restart min(5, ncol(xdat,ydat))
  times) as is done for a number of examples. Once the procedure
  terminates, you can restart search with default tolerances using those
  bandwidths obtained from the less rigorous search (i.e., set
  bws=bw on subsequent calls to this routine where bw is
  the initial bandwidth object).  A version of this package using the
  Rmpi wrapper is under development that allows one to deploy
  this software in a clustered computing environment to facilitate
  computation involving large datasets.
npcdensbw implements a variety of methods for choosing
  bandwidths for multivariate distributions ($p+q$-variate) defined
  over a set of possibly continuous and/or discrete (unordered, ordered)
  data. The approach is based on Li and Racine (2004) who employ
  The cross-validation methods employ multivariate numerical search algorithms (direction set (Powell's) methods in multidimensions).
Bandwidths can (and will) differ for each variable which is, of course, desirable.
Three classes of kernel estimators for the continuous data types are available: fixed, adaptive nearest-neighbor, and generalized nearest-neighbor. Adaptive nearest-neighbor bandwidths change with each sample realization in the set, $x_i$, when estimating the density at the point $x$. Generalized nearest-neighbor bandwidths change with the point at which the density is estimated, $x$. Fixed bandwidths are constant over the support of $x$.
  npcdensbw may be invoked either with a formula-like
  symbolic 
  description of variables on which bandwidth selection is to be
  performed or through a simpler interface whereby data is passed
  directly to the function via the xdat and ydat
  parameters. Use of these two interfaces is mutually exclusive.
  Data contained in the data frames xdat and ydat may be a
  mix of continuous (default), unordered discrete (to be specified in
  the data frames using factor), and ordered discrete (to be
  specified in the data frames using ordered). Data can be
  entered in an arbitrary order and data types will be detected
  automatically by the routine (see np for details).
  Data for which bandwidths are to be estimated may be specified
  symbolically. A typical description has the form dependent data
  ~ explanatory data,
  where dependent data and explanatory data are both
  series of variables specified by name, separated by 
  the separation character '+'. For example, y1 + y2 ~ x1 + x2
  specifies that the bandwidths for the joint distribution of variables
  y1 and y2 conditioned on x1 and x2 are to
  be estimated. See below for further examples. 
A variety of kernels may be specified by the user. Kernels implemented for continuous data types include the second, fourth, sixth, and eighth order Gaussian and Epanechnikov kernels, and the uniform kernel. Unordered discrete data types use a variation on Aitchison and Aitken's (1976) kernel, while ordered data types use a variation of the Wang and van Ryzin (1981) kernel.
  Hall, P. and J.S. Racine and Q. Li (2004), 
Li, Q. and J.S. Racine (2007), Nonparametric Econometrics: Theory and Practice, Princeton University Press.
Pagan, A. and A. Ullah (1999), Nonparametric Econometrics, Cambridge University Press.
Scott, D.W. (1992), Multivariate Density Estimation. Theory, Practice and Visualization, New York: Wiley.
Silverman, B.W. (1986), Density Estimation, London: Chapman and Hall.
  Wang, M.C. and J. van Ryzin (1981), 
bw.nrd, bw.SJ, hist,
  npudens, npudist# EXAMPLE 1 (INTERFACE=FORMULA): For this example, we compute the
# likelihood cross-validated bandwidths (default) using a second-order
# Gaussian kernel (default). Note - this may take a minute or two
# depending on the speed of your computer.
data("Italy")
attach(Italy)
bw <- npcdensbw(formula=gdp~ordered(year))
# The object bw can be used for further estimation using
# npcdens(), plotting using plot() etc. Entering the name of
# the object provides useful summary information, and names() will also
# provide useful information.
summary(bw)
# Note - see the example for npudensbw() for multiple illustrations
# of how to change the kernel function, kernel order, and so forth.
detach(Italy)
# EXAMPLE 1 (INTERFACE=DATA FRAME): For this example, we compute the
# likelihood cross-validated bandwidths (default) using a second-order
# Gaussian kernel (default). Note - this may take a minute or two
# depending on the speed of your computer.
data("Italy")
attach(Italy)
bw <- npcdensbw(xdat=ordered(year), ydat=gdp)
# The object bw can be used for further estimation using
# npcdens(), plotting using plot() etc. Entering the name of
# the object provides useful summary information, and names() will also
# provide useful information.
summary(bw)
# Note - see the example for npudensbw() for multiple illustrations
# of how to change the kernel function, kernel order, and so forth.
detach(Italy)Run the code above in your browser using DataLab