| npcdensbw | R Documentation |
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, ...)
## S3 method for class 'conbandwidth'
npcdensbw(xdat = stop("data 'xdat' missing"),
ydat = stop("data 'ydat' missing"),
bws,
bandwidth.compute = TRUE,
cfac.dir = 2.5*(3.0-sqrt(5)),
scale.factor.init = 0.5,
dfac.dir = 0.25*(3.0-sqrt(5)),
dfac.init = 0.375,
dfc.dir = 3,
ftol = 1.490116e-07,
scale.factor.init.upper = 2.0,
hbd.dir = 1,
hbd.init = 0.9,
initc.dir = 1.0,
initd.dir = 1.0,
invalid.penalty = c("baseline","dbmax"),
itmax = 10000,
lbc.dir = 0.5,
scale.factor.init.lower = 0.1,
lbd.dir = 0.1,
lbd.init = 0.1,
memfac = 500,
nmulti,
penalty.multiplier = 10,
nomad.remin = FALSE,
powell.remin = TRUE,
bwsolver = c("powell", "mads", "mads+powell"),
scale.init.categorical.sample = FALSE,
scale.factor.search.lower = NULL,
cvls.quadrature.grid = NULL,
cvls.quadrature.extend.factor = NULL,
cvls.quadrature.points = NULL,
cvls.quadrature.ratios = NULL,
small = 1.490116e-05,
tol = 1.490116e-04,
transform.bounds = FALSE,
...,
nomad.opts = list())
## Default S3 method:
npcdensbw(xdat = stop("data 'xdat' missing"),
ydat = stop("data 'ydat' missing"),
bws,
bandwidth.compute = TRUE,
bwmethod,
bwscaling,
bwtype,
cfac.dir,
scale.factor.init,
cxkerbound,
cxkerlb,
cxkerorder,
cxkertype,
cxkerub,
cykerbound,
cykerlb,
cykerorder,
cykertype,
cykerub,
dfac.dir,
dfac.init,
dfc.dir,
ftol,
scale.factor.init.upper,
hbd.dir,
hbd.init,
initc.dir,
initd.dir,
invalid.penalty,
itmax,
lbc.dir,
scale.factor.init.lower,
lbd.dir,
lbd.init,
memfac,
nmulti,
oxkertype,
oykertype,
penalty.multiplier,
nomad.remin = FALSE,
powell.remin,
bwsolver = c("powell", "mads", "mads+powell"),
scale.init.categorical.sample,
scale.factor.search.lower = NULL,
cvls.quadrature.grid = c("hybrid", "uniform", "sample"),
cvls.quadrature.extend.factor = 1,
cvls.quadrature.points = c(100L, 50L),
cvls.quadrature.ratios = c(0.20, 0.55, 0.25),
small,
tol,
transform.bounds,
uxkertype,
uykertype,
regtype = c("lc", "ll", "lp"),
basis = c("glp", "additive", "tensor"),
degree = NULL,
degree.select = c("manual", "coordinate", "exhaustive"),
search.engine = c("nomad+powell", "cell", "nomad"),
nomad = FALSE,
nomad.nmulti = 0L,
degree.min = NULL,
degree.max = NULL,
degree.start = NULL,
degree.restarts = 0L,
degree.max.cycles = 20L,
degree.verify = FALSE,
bernstein.basis = FALSE,
...,
nomad.opts = list())
These arguments identify the data, formula interface, and whether bandwidths are supplied or computed.
bandwidth.compute |
a logical value which specifies whether to do a numerical search for
bandwidths or not. If set to |
bws |
a bandwidth specification. This can be set as a |
call |
the original function call. This is passed internally by
|
data |
This argument belongs to the formula interface (including supported
formula-bearing bandwidth-object refits). Native calls must supply their
vectors, matrices or data frames directly, or use an optional data frame, list or environment (or object
coercible to a data frame by |
formula |
a symbolic description of variables on which bandwidth selection is to be performed. The details of constructing a formula are described below. |
na.action |
a function which indicates what should happen when the data contain
|
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
xdat |
a |
ydat |
a |
These arguments control automatic local-polynomial degree search when regtype="lp".
degree.max |
optional scalar or integer vector giving upper bounds for automatic
degree search over continuous |
degree.max.cycles |
positive integer giving the maximum number of coordinate-search
sweeps over the degree vector. Ignored for |
degree.min |
optional scalar or integer vector giving lower bounds for automatic
degree search over continuous |
degree.restarts |
non-negative integer giving the number of additional deterministic
restarts used by coordinate search with |
degree.select |
character string controlling local-polynomial degree handling when
|
degree.start |
optional starting degree vector for automatic coordinate search. If
omitted, the search starts from the degree-zero local-constant
baseline on the continuous |
degree.verify |
logical value indicating whether a coordinate-search solution should
be exhaustively verified over the admissible degree grid after the
heuristic phase completes. Available only for
|
These arguments choose the selection criterion and the way continuous bandwidths are represented.
bwmethod |
which method to use to select
bandwidths. |
bwscaling |
a logical value that when set to |
bwtype |
character string used for the continuous variable bandwidth type,
specifying the type of bandwidth to compute and return in the
|
These controls set categorical search starts and categorical direction-set initialization.
dfac.dir |
stretch factor for direction set search for Powell's algorithm for categorical variables. See Details |
dfac.init |
non-random initial values for scale factors for categorical variables for Powell's algorithm. See Details |
hbd.dir |
upper bound for direction set search for Powell's algorithm for categorical variables. See Details |
hbd.init |
upper bound for scale factors for categorical variables for Powell's algorithm. See Details |
initd.dir |
initial non-random values for direction set search for Powell's algorithm for categorical variables. See Details |
lbd.dir |
lower bound for direction set search for Powell's algorithm for categorical variables. See Details |
lbd.init |
lower bound for scale factors for categorical variables for Powell's algorithm. See Details |
scale.init.categorical.sample |
a logical value that when set
to |
These controls set Powell direction-set initialization for continuous variables.
cfac.dir |
stretch factor for direction set search for Powell's algorithm for |
dfc.dir |
chi-square degrees of freedom for direction set search for Powell's algorithm for |
initc.dir |
initial non-random values for direction set search for Powell's algorithm for |
lbc.dir |
lower bound for direction set search for Powell's algorithm for |
These controls choose and parameterize bounded support for continuous kernels.
cxkerbound |
character string controlling continuous-kernel support handling for
|
cxkerlb |
numeric scalar/vector of lower bounds for continuous |
cxkerub |
numeric scalar/vector of upper bounds for continuous |
cykerbound |
character string controlling continuous-kernel support handling for
|
cykerlb |
numeric scalar/vector of lower bounds for continuous |
cykerub |
numeric scalar/vector of upper bounds for continuous |
These controls define deterministic and random continuous scale-factor starts and the lower admissibility floor for fixed-bandwidth search.
scale.factor.init |
deterministic initial scale factor for continuous fixed-bandwidth
search. Defaults to |
scale.factor.init.lower |
lower endpoint for random continuous scale-factor starts. Defaults
to |
scale.factor.init.upper |
upper endpoint for random continuous scale-factor starts. Defaults
to |
scale.factor.search.lower |
optional nonnegative scalar giving the hard lower admissibility
bound for continuous fixed-bandwidth search candidates. Defaults to
|
These controls choose continuous, unordered, and ordered kernels for xdat and ydat.
cxkerorder |
numeric value specifying kernel order for
|
cxkertype |
character string used to specify the continuous kernel type for
|
cykerorder |
numeric value specifying kernel order for
|
cykertype |
character string used to specify the continuous kernel type for
|
oxkertype |
character string used to specify the ordered categorical kernel
type for |
oykertype |
character string used to specify the ordered categorical kernel
type for |
uxkertype |
character string used to specify the unordered categorical kernel
type for |
uykertype |
character string used to specify the unordered categorical kernel
type for |
These controls tune quadrature for bounded continuous-response least-squares cross-validation.
cvls.quadrature.extend.factor |
a positive finite scalar controlling the finite numerical integration
window used by bounded conditional-density |
cvls.quadrature.grid |
character string specifying the one-dimensional bounded |
cvls.quadrature.points |
a two-element integer vector giving the bounded |
cvls.quadrature.ratios |
a three-element non-negative numeric vector summing to one, giving the
uniform, ranked sample- |
When response-side bounds are set explicitly to fixed infinite endpoints,
bounded cv.ls uses a finite numerical quadrature surrogate over the
data range extended by cvls.quadrature.extend.factor. In that edge
case, callers who want tighter agreement with the ordinary unbounded
convolution route should set cvls.quadrature.points explicitly.
These arguments control the local-polynomial estimator, basis, and fixed degree specification.
basis |
character string specifying the polynomial basis used when
|
bernstein.basis |
logical value controlling the stabilized representation for
|
degree |
integer scalar or integer vector of polynomial degrees for
continuous |
regtype |
character string specifying the conditional local method used for
the |
These arguments control the optional NOMAD direct-search route for local-polynomial degree and bandwidth search.
nomad |
logical or character shortcut for the recommended automatic local-polynomial
NOMAD route. When |
nomad.nmulti |
non-negative integer controlling the inner
the native crs NOMAD C API multistart count used within each outer
NOMAD restart when |
nomad.opts |
optional named list of native NOMAD parameters forwarded to the
crs NOMAD C API for NOMAD-backed bandwidth searches.
|
nomad.remin |
logical flag controlling the optional second NOMAD hot start. When
|
search.engine |
character string controlling the automatic local-polynomial search
backend when |
These controls set optimizer tolerances, restart behavior, invalid-candidate penalties, memory blocking, and bounded search transformations.
ftol |
fractional tolerance on the value of the cross-validation function
evaluated at located minima (of order the machine precision or
perhaps slightly larger so as not to be diddled by
roundoff). Defaults to |
invalid.penalty |
a character string specifying the penalty
used when the optimizer encounters invalid bandwidths.
|
itmax |
integer number of iterations before failure in the numerical
optimization routine. Defaults to |
memfac |
The algorithm to compute the least-squares objective function uses a block-based algorithm to eliminate or minimize redundant kernel evaluations. Due to memory, hardware and software constraints, a maximum block size must be imposed by the algorithm. This block size is roughly equal to memfac*10^5 elements. Empirical tests on modern hardware find that a memfac of 500 performs well. If you experience out of memory errors, or strange behaviour for large data sets (>100k elements) setting memfac to a lower value may fix the problem. |
nmulti |
integer number of times to restart the process of finding extrema of the cross-validation function from different (random) initial points |
penalty.multiplier |
a numeric multiplier applied to the
baseline penalty when |
powell.remin |
logical flag controlling Powell restart-from-minimum behavior. For
ordinary fixed-degree Powell-style search, |
bwsolver |
bandwidth optimizer for fixed-degree searches. The default
|
small |
a small number used to bracket a minimum (it is hopeless to ask for
a bracketing interval of width less than sqrt(epsilon) times its
central value, a fractional width of only about 10-04 (single
precision) or 3x10-8 (double precision)). Defaults to |
tol |
tolerance on the position of located minima of the cross-validation
function (tol should generally be no smaller than the square root of
your machine's floating point precision). Defaults to |
transform.bounds |
a logical value that when set to |
These arguments collect remaining controls passed through S3 methods.
... |
additional arguments supplied to specify the bandwidth type, kernel types, selection methods, and so on, detailed below. |
Seeded temporary operations follow np_rng_scope.
Formula bandwidth objects retain the prepared training sample (after alignment,
subsetting and missing-value handling) and the resolved missing-value policy.
Later refits use that sample even if constructor variables are rebound.
Supplying explicit training data to the estimator replaces the training data;
evaluation via newdata does not. Retention requires storage proportional
to the sample size times the number of variables, not a pairwise weight matrix.
Older saved objects without this retained state still rely on their original
data bindings; rebuild their bandwidth object from the intended data before
changing those bindings. User-defined transformation and missing-value
functions should not rely on subsequently modified closure state.
Beta associated kernels may be selected independently with
cxkertype="beta" and cykertype="beta". Orders 2, 4, 6,
and 8, fixed/generalized-NN/adaptive-NN bandwidths,
cxkerbound/cykerbound equal to "fixed" or
"range", explicit bandwidth objects, and automatic
cv.ml/cv.ls search are supported. Normal-reference selection
is not. Beta kernels act on continuous coordinates alongside the
kernels for categorical coordinates. Local-constant, local-linear and
local-polynomial fits are supported. Local-constant gradients with respect to X are
available: a beta X coordinate uses its analytic evaluation-coordinate PDF
derivative, while the beta or legacy Y density factor is held fixed. Exact
matching beta-X endpoints use structural cancellation in the normalized
ratio and can yield signed infinity or NA when a finite derivative
does not exist. Bounded
beta-response cv.ls uses Y-support quadrature of the actual estimator
with cached X weights. Signed orders 4, 6, and 8 are not positivity-repaired.
The scale.factor.* controls are dimensionless search
controls. The package converts scale factors to bandwidths using the
estimator-specific scaling encoded in the bandwidth object, including
kernel order and the number of continuous variables relevant for the
estimator. Users should not pre-multiply these controls by sample-size
or standard-deviation factors.
scale.factor.init controls the deterministic first search
start. scale.factor.init.lower and
scale.factor.init.upper define the random multistart interval.
scale.factor.search.lower is the lower admissibility bound for
continuous fixed-bandwidth search candidates. The effective first
start is max(scale.factor.init, scale.factor.search.lower),
and the effective random-start lower endpoint is
max(scale.factor.init.lower, scale.factor.search.lower).
scale.factor.init.upper must be at least that effective lower
endpoint; the package errors rather than silently expanding the user's
interval.
When scale.factor.search.lower is NULL, an existing
bandwidth object's stored floor is inherited when available;
otherwise the package default 0.1 is used. Explicit bandwidths
supplied for storage with bandwidth.compute = FALSE are not
rewritten by the search floor.
For automatic fixed-bandwidth CVLS with a beta response kernel,
repeated complete observations at a declared response endpoint require an
explicitly positive resolved scale.factor.search.lower. The default
0.1 already satisfies this requirement. Endpoint repetitions can
cause the criterion to prefer vanishing bandwidths; this is a conservative
search-admissibility policy, not a claim that every such sample has a
divergent criterion. Manual bandwidth evaluation and nearest-neighbor
searches remain available. No positive floor is silently substituted for
an explicitly requested zero.
A selected scale factor at this floor is a boundary solution of the
constrained search, not evidence of an interior optimum. In particular,
endpoint observations can make beta CVLS sensitive to the chosen floor.
For a returned fixed-bandwidth object bw, inspect
bw$sfactor$x[bw$ixcon] and bw$sfactor$y[bw$iycon] alongside
bw$scale.factor.search.lower; physical bandwidths are on a
different scale and should not be compared directly with this dimensionless
bound. Near-bound solutions merit the same sensitivity consideration, but
proximity alone does not prove that the unconstrained criterion diverges.
Changing the floor changes the admissible search problem.
Categorical search-start controls such as dfac.init,
lbd.init, and hbd.init have separate semantics and are
not affected by scale.factor.search.lower.
For bwmethod="cv.ml", the ordinary contribution is based on
\log(\hat f_i). Compact-support kernels can yield
zero leave-one-out conditional-density estimates, and local-polynomial
or higher-order-kernel fits can yield negative estimates. In those
cases the package optimizes a guarded CVML criterion rather than the
literal log likelihood. Every represented strictly positive fitted value,
including a positive subnormal value retained in log form, uses the
ordinary branch -\log(\hat f_i). Let
\epsilon denote the positive machine floor used only to
encode an invalid nonpositive contribution. Exact zero and negative values
with magnitude no greater than \epsilon use
-\log(\epsilon); negative values of larger magnitude
use the smooth penalty
\log(-\hat f_i)-2\log(\epsilon).
The guarded count reported in bandwidth summaries records
objective evaluations containing an invalid nonpositive contribution; a
positive value is not guarded merely because it is small. The
bwmethod="cv.ls" criterion remains well defined for negative
kernel density estimates and is not modified by this CVML guard.
Documentation guide: see np.kernels for kernels, np.options for global options, and plot, plot.np for plotting options.
The bandwidth-selection argument surface is easiest to read by
decision group. Start with the data and bandwidth inputs
(xdat, ydat, bws, and
bandwidth.compute), then choose the bandwidth criterion and
representation (bwmethod, bwscaling, and
bwtype). Next choose continuous kernel and support controls
(cxker* and cyker*), categorical kernel controls
(uxkertype, uykertype, oxkertype, and
oykertype), and numerical search controls including
nmulti, tolerances, penalties, and the scale.factor.*
search-start and admissibility controls. Bounded continuous-response
cv.ls fits may also use the cvls.quadrature.*
controls. Local-polynomial and NOMAD controls
(regtype, basis, degree*,
search.engine, nomad, nomad.nmulti, and
bernstein.basis) are relevant when using the explicit
local-polynomial route.
For S3 plotting help, see plot.np. You can list
available plot methods with methods("plot").
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
‘generalized product kernels’ that admit a mix of continuous
and discrete data types.
The cross-validation methods employ multivariate numerical search
algorithms. For fixed local-constant/local-linear fits, and for
local-polynomial fits with degree.select="manual", bandwidth
search uses multidimensional Powell direction-set optimization.
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.
For unbounded Gaussian, Epanechnikov, and uniform kernels,
bwmethod="cv.ls" with bwtype="generalized_nn" integrates
the squared leave-one-out conditional density over the full response
support, allowing response bandwidths to vary with the response evaluation
point. This requires additional integration work compared with the
analytical convolutions available for fixed and adaptive nearest-neighbor
bandwidths.
This whole-response criterion also applies to beta predictor kernels and bounded Gaussian predictor kernels when the response domain is unbounded. Predictor boundary correction does not make the response bandwidth constant.
Ambiguous local QR rank in a conditional local-polynomial design uses the existing original-coordinate rank/ridge admission. If a numerical evaluation remains unusable, including a failed coefficient accuracy check or exhausted integration limits, it receives the existing invalid-evaluation penalty and search continues. A penalty is not a verified cross-validation score; the final selected bandwidth must pass the existing raw-objective validation.
Computation time depends on the kernels, nearest-neighbor counts, predictor
and response dimensions, and local-polynomial specification. Some
configurations can be substantially more expensive. Second-order kernels
are the usual practical choice; higher-order kernels remain supported.
The cvls.quadrature.* controls described above apply to bounded
response integration, not this unbounded generalized-neighbor calculation.
Exact ties can make the literal kth continuous nearest-neighbor
radius zero. Such a bandwidth is undefined and is rejected; see
np.kernels for the precise contract and remedies.
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.
Response expressions such as log(y) or I(y^2) are not
supported on the left-hand side and are rejected. Instead create, for
example, data$log_y <- log(data$y) and use log_y ~ x, or
supply the transformed response through ydat. Right-hand-side
offset() special terms are also unsupported and are rejected rather
than omitted. These checks do not evaluate response expressions, alter
stored/dynamically constructed formulas, or change supported explanatory
transformations.
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.
When regtype="lp" and degree.select != "manual",
npcdensbw can jointly determine the xdat-side local
polynomial degree vector and the fixed bandwidth coordinates entering
the conditional density criterion. With
search.engine="cell", the criterion is profiled over the
admissible degree grid using cached coordinate-wise or exhaustive
search. With search.engine="nomad" or
"nomad+powell", the criterion is optimized directly over the
joint degree/bandwidth space using the native crs NOMAD C API;
"nomad+powell" then performs one Powell hot start from the
NOMAD solution and keeps the better of the direct NOMAD and polished
answers. This polynomial-adaptive joint-search route is motivated by
Hall and Racine (2015) together with Li, Li, and Racine (under
revision). When bernstein.basis is not explicitly supplied,
the automatic search route defaults to bernstein.basis=TRUE
for numerical stability.
Setting nomad=TRUE is a convenience preset for this automatic
LP route, not a generic optimizer alias. For conditional density
bandwidth selection it expands any missing values to the equivalent
long-form call
npcdensbw(...,
regtype = "lp",
search.engine = "nomad+powell",
degree.select = "coordinate",
bernstein.basis = TRUE,
degree.min = 0L,
degree.max = 10L,
degree.verify = FALSE,
bwtype = "fixed")
Compatible explicit tuning arguments are respected. Incompatible
explicit settings fail fast so the shortcut never silently changes
user-selected semantics.
The character value nomad="auto" applies the same LP
shortcut but leaves search.engine and degree.select
eligible for data-driven resolution when both were not supplied
explicitly: scalar continuous degree searches use the exhaustive
degree-grid route, while higher-dimensional degree searches keep the
NOMAD/Powell route. Explicit search.engine or
degree.select choices are honored.
The optimizer invoked for search is Powell's conjugate direction
method which requires the setting of (non-random) initial values and
search directions for bandwidths, and, when restarting, random values
for successive invocations. Bandwidths for numeric variables
are scaled by robust measures of spread, the sample size, and the
number of numeric variables where appropriate. Two sets of
parameters for bandwidths for numeric can be modified, those
for initial values for the parameters themselves, and those for the
directions taken (Powell's algorithm does not involve explicit
computation of the function's gradient). The default values are set by
considering search performance for a variety of difficult test cases
and simulated cases. We highly recommend restarting search a large
number of times to avoid the presence of local minima (achieved by
modifying nmulti). Further refinement for difficult cases can
be achieved by modifying these sets of parameters. However, these
parameters are intended more for the authors of the package to enable
‘tuning’ for various methods rather than for the user themselves.
npcdensbw returns a conbandwidth object, with the
following components:
xbw |
bandwidth(s), scale factor(s) or nearest neighbours for the
explanatory data, |
ybw |
bandwidth(s), scale factor(s) or nearest neighbours for the
dependent data, |
fval |
objective function value at minimum |
if bwtype 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 kth nearest neighbors are returned for the
continuous variables while the discrete kernel bandwidths are returned
for the discrete variables.
Prediction follows a two-stage contract. Calling predict(bw)
on the returned bandwidth object is a convenience forwarder for
npcdens(bws = bw) and returns a fitted condensity
object; it does not return a numeric vector of conditional-density
estimates. Call fitted(fit) or
predict(fit, newdata = ...) on that fitted object to obtain
numeric results. The canonical workflow is
bw <- npcdensbw(y ~ x, data = mydata)
fit <- predict(bw) # equivalent to npcdens(bws = bw)
fitted(fit)
predict(fit, newdata = newdata)
The functions summary and plot also
support conbandwidth objects.
Local-polynomial search uses the numerical-ridging policies described in
npreg; see Seifert and Gasser (2000) there.
npcdensbw selects response and conditioning bandwidths, and
when requested local-polynomial degree/search metadata, for the
conditional density target f(y\mid x) estimated by
npcdens.
For book-length derivations, see Li and Racine (2007), Chapter 5 Conditional Density Estimation, especially Sections 5.1-5.4, and Chapter 4 Kernel Estimation with Mixed Data. The later workflow treatment is Racine (2019), Chapter 4 Conditional Probability Density and Cumulative Distribution Functions.
If you are using data of mixed types, then it is advisable to use the
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 ith 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(2, 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.
Tristen Hayfield tristen.hayfield@gmail.com, Jeffrey S. Racine racinej@mcmaster.ca
Aitchison, J. and C.G.G. Aitken (1976), “Multivariate binary discrimination by the kernel method,” Biometrika, 63, 413-420.
Hall, P. and J.S. Racine and Q. Li (2004), “Cross-validation and the estimation of conditional probability densities,” Journal of the American Statistical Association, 99, 1015-1026.
Hall, P. and J.S. Racine (2015), “Infinite Order Cross-Validated Local Polynomial Regression,” Journal of Econometrics, 185, 510-525.
Li, Q. and J.S. Racine (2007), Nonparametric Econometrics: Theory and Practice, Princeton University Press.
Li, A. and Q. Li and J.S. Racine (under revision), “Boundary Adjusted, Polynomial Adaptive, Nonparametric Kernel Conditional Density Estimation,” Econometric Reviews.
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), “A class of smooth estimators for discrete distributions,” Biometrika, 68, 301-309.
np.kernels, np.options, plot, plot.np
bw.nrd, bw.SJ, hist,
npudens, npudist
## Not run:
# 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")
Italy <- Italy[seq_len(min(300, nrow(Italy))), ]
with(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.
})
# 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")
Italy <- Italy[seq_len(min(300, nrow(Italy))), ]
with(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.
})
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.