npregbw | R Documentation |
npregbw
computes a bandwidth object for a
p
-variate kernel regression estimator defined over mixed
continuous and discrete (unordered, ordered) data using expected
Kullback-Leibler cross-validation, or least-squares cross validation
using the method of Racine and Li (2004) and Li and Racine (2004).
npregbw(...)
## S3 method for class 'formula'
npregbw(formula, data, subset, na.action, call, ...)
## S3 method for class 'NULL'
npregbw(xdat = stop("invoked without data 'xdat'"),
ydat = stop("invoked without data 'ydat'"),
bws,
...)
## Default S3 method:
npregbw(xdat = stop("invoked without data 'xdat'"),
ydat = stop("invoked without data 'ydat'"),
bws,
bandwidth.compute = TRUE,
nmulti,
remin,
itmax,
ftol,
tol,
small,
lbc.dir,
dfc.dir,
cfac.dir,
initc.dir,
lbd.dir,
hbd.dir,
dfac.dir,
initd.dir,
lbc.init,
hbc.init,
cfac.init,
lbd.init,
hbd.init,
dfac.init,
scale.init.categorical.sample,
regtype,
bwmethod,
bwscaling,
bwtype,
ckertype,
ckerorder,
ukertype,
okertype,
...)
## S3 method for class 'rbandwidth'
npregbw(xdat = stop("invoked without data 'xdat'"),
ydat = stop("invoked without data 'ydat'"),
bws,
bandwidth.compute = TRUE,
nmulti,
remin = TRUE,
itmax = 10000,
ftol = 1.490116e-07,
tol = 1.490116e-04,
small = 1.490116e-05,
lbc.dir = 0.5,
dfc.dir = 3,
cfac.dir = 2.5*(3.0-sqrt(5)),
initc.dir = 1.0,
lbd.dir = 0.1,
hbd.dir = 1,
dfac.dir = 0.25*(3.0-sqrt(5)),
initd.dir = 1.0,
lbc.init = 0.1,
hbc.init = 2.0,
cfac.init = 0.5,
lbd.init = 0.1,
hbd.init = 0.9,
dfac.init = 0.375,
scale.init.categorical.sample = FALSE,
...)
formula |
a symbolic description of variables on which bandwidth selection is to be performed. The details of constructing a formula are described below. |
data |
an optional data frame, list or environment (or object
coercible to a data frame by |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
na.action |
a function which indicates what should happen when the data contain
|
call |
the original function call. This is passed internally by
|
xdat |
a |
ydat |
a one (1) dimensional numeric or integer vector of dependent data, each
element |
bws |
a bandwidth specification. This can be set as a |
... |
additional arguments supplied to specify the bandwidth type, kernel types, selection methods, and so on, detailed below. |
regtype |
a character string specifying which type of kernel regression
estimator to use. |
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
|
bandwidth.compute |
a logical value which specifies whether to do a numerical search for
bandwidths or not. If set to |
ckertype |
character string used to specify the continuous kernel type.
Can be set as |
ckerorder |
numeric value specifying kernel order (one of
|
ukertype |
character string used to specify the unordered categorical kernel type.
Can be set as |
okertype |
character string used to specify the ordered categorical kernel type.
Can be set as |
nmulti |
integer number of times to restart the process of finding extrema of
the cross-validation function from different (random) initial
points. Defaults to |
remin |
a logical value which when set as |
itmax |
integer number of iterations before failure in the numerical
optimization routine. Defaults to |
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 |
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 |
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 |
lbc.dir,dfc.dir,cfac.dir,initc.dir |
lower bound, chi-square
degrees of freedom, stretch factor, and initial non-random values
for direction set search for Powell's algorithm for |
lbd.dir,hbd.dir,dfac.dir,initd.dir |
lower bound, upper bound, stretch factor, and initial non-random values for direction set search for Powell's algorithm for categorical variables. See Details |
lbc.init, hbc.init, cfac.init |
lower bound, upper bound, and
non-random initial values for scale factors for |
lbd.init, hbd.init, dfac.init |
lower bound, upper bound, and non-random initial values for scale factors for categorical variables for Powell's algorithm. See Details |
scale.init.categorical.sample |
a logical value that when set
to |
npregbw
implements a variety of methods for choosing
bandwidths for multivariate (p
-variate) regression data defined
over a set of possibly continuous and/or discrete (unordered, ordered)
data. The approach is based on Li and Racine (2003) who employ
‘generalized product kernels’ that admit a mix of continuous
and discrete data types.
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
.
npregbw
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 frame xdat
may be a mix of
continuous (default), unordered discrete (to be specified in the data
frame xdat
using factor
), and ordered discrete
(to be specified in the data frame xdat
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
is a univariate response, and
explanatory data
is a
series of variables specified by name, separated by
the separation character '+'. For example, y1 ~ x1 + x2
specifies that the bandwidths for the regression of response y1
and
nonparametric regressors 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.
The use of compactly supported kernels or the occurrence of small bandwidths during cross-validation can lead to numerical problems for the local linear estimator when computing the locally weighted least squares solution. To overcome this problem we rely on a form or ‘ridging’ proposed by Cheng, Hall, and Titterington (1997), modified so that we solve the problem pointwise rather than globally (i.e. only when it is needed).
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.
npregbw
returns a rbandwidth
object, with the
following components:
bw |
bandwidth(s), scale factor(s) or nearest neighbours for the
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 k
th nearest
neighbors are returned for the continuous variables while the discrete
kernel bandwidths are returned for the discrete variables. Bandwidths
are stored under the component name bw
, with each
element i
corresponding to column i
of input data
xdat
.
The functions predict
, summary
, and plot
support
objects of this class.
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 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))
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.
Cheng, M.-Y. and P. Hall and D.M. Titterington (1997), “On the shrinkage of local linear curve estimators,” Statistics and Computing, 7, 11-17.
Hall, P. and Q. Li and J.S. Racine (2007), “Nonparametric estimation of regression functions in the presence of irrelevant regressors,” The Review of Economics and Statistics, 89, 784-789.
Hurvich, C.M. and J.S. Simonoff and C.L. Tsai (1998), “Smoothing parameter selection in nonparametric regression using an improved Akaike information criterion,” Journal of the Royal Statistical Society B, 60, 271-293.
Li, Q. and J.S. Racine (2007), Nonparametric Econometrics: Theory and Practice, Princeton University Press.
Li, Q. and J.S. Racine (2004), “Cross-validated local linear nonparametric regression,” Statistica Sinica, 14, 485-512.
Pagan, A. and A. Ullah (1999), Nonparametric Econometrics, Cambridge University Press.
Racine, J.S. and Q. Li (2004), “Nonparametric estimation of regression functions with both categorical and continuous data,” Journal of Econometrics, 119, 99-130.
Wang, M.C. and J. van Ryzin (1981), “A class of smooth estimators for discrete distributions,” Biometrika, 68, 301-309.
npreg
## Not run:
# EXAMPLE 1 (INTERFACE=FORMULA): For this example, we compute a
# Bivariate nonparametric regression estimate for Giovanni Baiocchi's
# Italian income panel (see Italy for details)
data("Italy")
attach(Italy)
# Compute the least-squares cross-validated bandwidths for the local
# constant estimator (default)
bw <- npregbw(formula=gdp~ordered(year))
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Supply your own bandwidth...
bw <- npregbw(formula=gdp~ordered(year), bws=c(0.75),
bandwidth.compute=FALSE)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Treat year as continuous and supply your own scaling factor c in
# c sigma n^{-1/(2p+q)}
bw <- npregbw(formula=gdp~year, bws=c(1.06),
bandwidth.compute=FALSE,
bwscaling=TRUE)
summary(bw)
# Note - see also the example for npudensbw() for more extensive
# multiple illustrations of how to change the kernel function, kernel
# order, bandwidth type and so forth.
detach(Italy)
# EXAMPLE 1 (INTERFACE=DATA FRAME): For this example, we compute a
# Bivariate nonparametric regression estimate for Giovanni Baiocchi's
# Italian income panel (see Italy for details)
data("Italy")
attach(Italy)
# Compute the least-squares cross-validated bandwidths for the local
# constant estimator (default)
bw <- npregbw(xdat=ordered(year), ydat=gdp)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Supply your own bandwidth...
bw <- npregbw(xdat=ordered(year), ydat=gdp, bws=c(0.75),
bandwidth.compute=FALSE)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Treat year as continuous and supply your own scaling factor c in
# c sigma n^{-1/(2p+q)}
bw <- npregbw(xdat=year, ydat=gdp, bws=c(1.06),
bandwidth.compute=FALSE,
bwscaling=TRUE)
summary(bw)
# Note - see also the example for npudensbw() for more extensive
# multiple illustrations of how to change the kernel function, kernel
# order, bandwidth type and so forth.
detach(Italy)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.