% 
\name{npplregbw}
\alias{npplregbw}
\alias{npplregbw.default}
\alias{npplregbw.formula}
\alias{npplregbw.plbandwidth}

\title{Partially Linear Kernel Regression Bandwidth Selection with Mixed Data Types}

\description{
  \code{npplregbw} computes a bandwidth object for a partially linear
  kernel regression estimate of a one (1) dimensional dependent variable
  on \eqn{p+q}-variate explanatory data, using the model \eqn{Y = X\beta
  + \Theta (Z) + \epsilon}{Y = XB + theta(Z) + epsilon} given a set of
  estimation points, training points (consisting of explanatory data and
  dependent data), and a bandwidth specification, which can be a
  \code{rbandwidth} object, or a bandwidth vector, bandwidth type and
  kernel type.
}

\usage{
npplregbw(\dots)

\method{npplregbw}{formula}(formula, data, subset, na.action, call, \dots)


\method{npplregbw}{default}(xdat = stop("invoked without data `xdat'"),
          ydat = stop("invoked without data `ydat'"),
          zdat = stop("invoked without data `zdat'"),
          bandwidth.compute = TRUE,
          bws,
          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,
          scale.factor.search.lower = NULL,
          ftol,
          itmax,
          nmulti,
          nomad.remin,
          powell.remin,
          small,
          tol,
          \dots)

\method{npplregbw}{plbandwidth}(xdat = stop("invoked without data `xdat'"),
          ydat = stop("invoked without data `ydat'"),
          zdat = stop("invoked without data `zdat'"),
          bws,
          nmulti,
          \dots)

}
\arguments{
\subsection{Data, Bandwidth Inputs And Formula Interface}{
These arguments identify the linear, nonparametric, formula, and bandwidth inputs.
}

  \item{bandwidth.compute}{
    a logical value which specifies whether to do a numerical search for
    bandwidths or not. If set to \code{FALSE}, a \code{plbandwidth} object
    will be returned with bandwidths set to those specified
    in \code{bws}. Defaults to \code{TRUE}.
  }

  \item{bws}{
    a bandwidth specification. This can be set as a \code{plbandwidth}
    object returned from an invocation of \code{npplregbw}, or as
    a matrix of bandwidths, where each row is a set of bandwidths for \eqn{Z},
    with a column for each variable \eqn{Z_i}{Z[i]}. In the first row
    are the bandwidths for the regression of \eqn{Y} on \eqn{Z}. The
    following rows contain the bandwidths for the regressions of the
    columns of \eqn{X} on \eqn{Z}.  If specified as a matrix,
    additional arguments will need to be supplied as necessary to
    specify the bandwidth type, kernel types, and so on.

    If left unspecified, \code{npplregbw} will search for optimal
    bandwidths using \code{\link{npregbw}} in the course of
    calculations. If specified, \code{npplregbw} will use the given
    bandwidths as the starting point for the numerical search for optimal
    bandwidths, unless you specify bandwidth.compute = FALSE. 
  }

  \item{call}{
    the original function call. This is passed internally by
    \code{\link{np}} when a bandwidth search has been implied by a call to
    another function. It is not recommended that the user set this.
  }

  \item{data}{
    an optional data frame, list or environment (or object
    coercible to a data frame by \code{\link{as.data.frame}}) containing the variables
    in the model. If not found in data, the variables are taken from
    \code{environment(formula)}, typically the environment from which the
    function is called.
  }

  \item{formula}{
    a symbolic description of variables on which bandwidth selection is
    to be performed. The details of constructing a formula are
    described below.
  }

  \item{na.action}{
    a function which indicates what should happen when the data contain
   \code{NA}s. The default is set by the \code{\link{na.action}} setting of options, and is
    \code{\link{na.fail}} if that is unset. The (recommended) default is
    \code{\link{na.omit}}.  
  }
  
  \item{subset}{
    an optional vector specifying a subset of observations to be used in
    the fitting process. 
  }

  \item{xdat}{
  a \eqn{p}-variate data frame of explanatory data (training data),
  corresponding to \eqn{X} in the model equation, whose linear
  relationship with the dependent data \eqn{Y} is posited.
  }

  \item{ydat}{
    a one (1) dimensional numeric or integer vector of dependent data, each
    element \eqn{i} corresponding to each observation (row) \eqn{i} of
  \code{xdat}. 
  }

  \item{zdat}{
    a \eqn{q}-variate data frame of explanatory data (training data),
    corresponding to \eqn{Z} in the model equation, whose relationship
    to the dependent variable is unspecified (nonparametric)
  }


\subsection{Automatic Degree Search Controls}{
These arguments control automatic local-polynomial degree search.
}

  \item{degree.max}{
    optional scalar or integer vector giving upper bounds for automatic
    degree search over continuous \code{zdat} predictors when
    \code{degree.select != "manual"}.
  }

  \item{degree.max.cycles}{
    positive integer giving the maximum number of coordinate-search
    sweeps over the degree vector. Ignored for \code{"manual"} and
    \code{"exhaustive"} degree selection.
  }

  \item{degree.min}{
    optional scalar or integer vector giving lower bounds for automatic
    degree search over continuous \code{zdat} predictors when
    \code{degree.select != "manual"}.
  }

  \item{degree.restarts}{
    non-negative integer giving the number of additional deterministic
    coordinate-search restarts. Ignored for \code{"manual"} and
    \code{"exhaustive"} degree selection.
  }

  \item{degree.select}{
    character string controlling local-polynomial degree handling for
    the nuisance regressions of \eqn{Y} on \eqn{Z} and each column of
    \eqn{X} on \eqn{Z}. \code{"manual"} (default) treats \code{degree}
    as fixed. \code{"coordinate"} performs cached coordinate-wise
    search over admissible degree vectors.
    \code{"exhaustive"} evaluates the full admissible degree grid when
    \code{search.engine="cell"}. For NOMAD-based search engines, any
    non-\code{"manual"} value requests a separate degree and bandwidth
    search for each nuisance regression.
  }

  \item{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 \code{zdat} predictors.
  }

  \item{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
    \code{search.engine="cell"}.
  }


\subsection{Continuous Scale-Factor Search Controls}{
These controls define lower admissibility bounds for continuous fixed-bandwidth search.
}

  \item{scale.factor.search.lower}{
    optional nonnegative scalar giving the hard lower admissibility
    bound for continuous fixed-bandwidth search candidates. Defaults to
    \code{NULL}. If \code{NULL}, an existing bandwidth object's stored
    value is inherited when available; otherwise the package default
    \code{0.1} is used. This floor applies to computed/search bandwidth
    candidates and to effective search starts only. It does not rewrite
    explicit bandwidths supplied for storage with
    \code{bandwidth.compute = FALSE}. Final fixed-bandwidth search
    candidates must also have a finite valid raw objective value.
  }

\subsection{Local-Polynomial Model Specification}{
These arguments control fixed local-polynomial specification for the nonparametric component.
}

  \item{degree}{
    for local-polynomial partially linear fits, polynomial degree
    specification for each continuous nonparametric regressor in
    \code{zdat}. When supplied with \code{degree.select="manual"},
    bandwidth optimization treats this input as fixed. A numeric vector
    is used as a common degree vector for every nuisance regression.
    To specify child-specific fixed degrees, supply either a matrix with
    \eqn{p+1} rows and one column per continuous \code{zdat} predictor,
    or a list with \eqn{p+1} entries. The child order matches the
    bandwidth matrix/object order: first the regression of \eqn{Y} on
    \eqn{Z} (row or entry \code{yzbw}), followed by the regressions of
    the columns of \eqn{X} on \eqn{Z}. Named matrix rows or list entries
    are accepted and reordered to this canonical order.
  }


\subsection{NOMAD Search Controls}{
These arguments control the optional NOMAD direct-search route for local-polynomial degree and bandwidth search.
}

  \item{nomad}{
    logical shortcut for the recommended automatic local-polynomial
    NOMAD route for the partially linear nuisance regressions. When
    \code{TRUE}, any missing values among \code{regtype},
    \code{search.engine}, \code{degree.select},
    \code{bernstein.basis}, \code{degree.min}, \code{degree.max},
    \code{degree.verify}, and \code{bwtype} are filled with
    \code{regtype="lp"}, \code{search.engine="nomad+powell"},
    \code{degree.select="coordinate"},
    \code{bernstein.basis=TRUE}, \code{degree.min=0L},
    \code{degree.max=10L}, \code{degree.verify=FALSE}, and
    \code{bwtype="fixed"}. Explicit incompatible settings error
    immediately; in particular, \code{nomad=TRUE} currently requires
    \code{regtype="lp"}, \code{bwtype="fixed"}, automatic degree
    search, \code{bernstein.basis=TRUE}, no explicit \code{degree},
    and \code{search.engine \%in\% c("nomad", "nomad+powell")}.
    This shortcut does not change the meaning of \code{nmulti} or
    \code{nomad.nmulti}: \code{nmulti} remains the outer restart count,
    while \code{nomad.nmulti} controls inner
    \code{crs::snomadr()} multistarts within each outer restart.
    Returned bandwidth objects retain this normalized preset metadata in
    \code{bw$nomad.shortcut} for a returned object \code{bw}; when
    available, \code{nomad.time} and \code{powell.time} record the
    direct-search and Powell-polish timing components. Each nuisance
    regression selects its own admissible degree and bandwidths.
  }

  \item{nomad.nmulti}{
    non-negative integer controlling the inner
    \code{crs::snomadr()} multistart count used within each outer
    NOMAD restart when \code{regtype="lp"} and automatic degree search
    uses \code{search.engine="nomad"} or \code{"nomad+powell"}.
    Defaults to \code{0L}, which preserves the current one-start-per-
    restart behavior. This does not replace \code{nmulti}:
    \code{nmulti} controls outer restarts, while \code{nomad.nmulti}
    controls inner NOMAD multistarts within each outer restart.
  }

  \item{nomad.remin}{
    logical flag controlling the optional second NOMAD hot start. When
    \code{TRUE}, NOMAD is restarted once from the best full candidate found,
    including both bandwidth and degree coordinates. Defaults to \code{FALSE};
    current simulation evidence favors the one-pass NOMAD default for routine
    use, while leaving this switch available for sensitivity checks.
  }


  \item{search.engine}{
    character string controlling the automatic local-polynomial search
    backend for each nuisance regression when
    \code{degree.select != "manual"}. \code{"nomad+powell"} (default)
    performs direct search over fixed bandwidths and the degree vector
    using \code{crs::snomadr()}, then applies one Powell hot start from
    the NOMAD solution, separately for each nuisance regression.
    \code{"nomad"} omits the Powell refinement. \code{"cell"} profiles
    the criterion over the admissible degree grid using repeated
    fixed-degree bandwidth solves.
    NOMAD-based search currently requires fixed-bandwidth child
    templates, \code{degree.verify=FALSE}, and the suggested package
    \pkg{crs} to be installed.
  }


\subsection{Numerical Search And Tolerance Controls}{
These controls set optimizer tolerances and restart behavior.
}

  \item{ftol}{
    tolerance on the value of the cross-validation function 
    evaluated at located minima. Defaults to \code{1.19e-07
      (FLT_EPSILON)}
  }

  \item{itmax}{
    integer number of iterations before failure in the numerical
    optimization routine. Defaults to \code{10000}
  }

  \item{nmulti}{
    integer number of times to restart the process of finding extrema of
    the cross-validation function from different (random) initial
    points. Defaults to \code{min(2,ncol(zdat))}.
  }

  \item{powell.remin}{
    logical flag controlling Powell restart-from-minimum behavior. For
    ordinary fixed-degree Powell-style search, \code{TRUE} restarts the
    local search from the located minimum. For \code{search.engine="nomad+powell"},
    this controls only the final Powell bandwidth-polish step. The default
    is \code{TRUE} for ordinary Powell routes and \code{FALSE} for the
    Powell polish after NOMAD unless explicitly supplied.
  }

  \item{small}{ a small number, at about the precision of the data type
    used. Defaults to \code{2.22e-16 (DBL_EPSILON)}

  }

  \item{tol}{
    tolerance on the position of located minima of the 
    cross-validation function. Defaults to \code{1.49e-08
      (sqrt(DBL_EPSILON))}
  }


\subsection{Additional Arguments}{
These arguments collect remaining controls passed through S3 methods.
}

  \item{\dots}{
    additional arguments supplied to specify the regression type,
    bandwidth type, kernel types, selection methods, and so on. To do
    this, you may specify any of \code{regtype}, 
    \code{bwmethod}, \code{bwscaling}, \code{bwtype}, \code{ckertype},
    \code{ckerorder}, \code{ukertype}, \code{okertype},
    \code{bernstein.basis}, and \code{basis}, as described in
    \code{\link{npregbw}}.
  }

}

\details{

The \code{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.

  \code{scale.factor.init} controls the deterministic first search
  start when that control is exposed. \code{scale.factor.init.lower}
  and \code{scale.factor.init.upper} define the random multistart
  interval when exposed. \code{scale.factor.search.lower} is the lower
  admissibility bound for continuous fixed-bandwidth search candidates.
  The effective first start is \code{max(scale.factor.init,
  scale.factor.search.lower)} when both controls are present, and the
  effective random-start lower endpoint is
  \code{max(scale.factor.init.lower, scale.factor.search.lower)}.
  \code{scale.factor.init.upper} must be at least that effective lower
  endpoint; the package errors rather than silently expanding the user's
  interval.

  When \code{scale.factor.search.lower} is \code{NULL}, an existing
  bandwidth object's stored floor is inherited when available;
  otherwise the package default \code{0.1} is used. Explicit bandwidths
  supplied for storage with \code{bandwidth.compute = FALSE} are not
  rewritten by the search floor.

  Categorical search-start controls such as \code{dfac.init},
  \code{lbd.init}, and \code{hbd.init} have separate semantics and are
  not affected by \code{scale.factor.search.lower}.

  Documentation guide: see \code{\link{npregbw}} for component
  nonparametric regression bandwidth controls, \code{\link{np.kernels}}
  for kernels, \code{\link{np.options}} for global options, and
  \code{\link{plot}}, \code{\link{plot.np}} for plotting options.

  The partially linear bandwidth-selection argument surface is easiest
  to read by decision group: linear \code{xdat} inputs,
  nonparametric \code{zdat} inputs, and existing bandwidth inputs;
  local-polynomial/NOMAD controls for the nonparametric component;
  numerical search and feasibility controls; formula-interface
  controls; and additional bandwidth, kernel, and support controls that
  are passed to the component \code{\link{npregbw}} searches.

  For S3 plotting help, see \code{\link{plot.np}}. You can list
  available plot methods with \code{methods("plot")}.

  \code{npplregbw} implements a variety of methods for nonparametric
 regression on multivariate (\eqn{q}-variate) explanatory 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
 \sQuote{generalized product kernels} that admit a mix of continuous and
 discrete data types.

 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, \eqn{x_i}{x[i]}, when estimating the
 density at the point \eqn{x}. Generalized nearest-neighbor bandwidths change
 with the point at which the density is estimated, \eqn{x}. Fixed bandwidths
 are constant over the support of \eqn{x}.

 \code{npplregbw} may be invoked \emph{either} with a formula-like
 symbolic 
 description of variables on which bandwidth selection is to be
 performed \emph{or} through a simpler interface whereby data is passed
 directly to the function via the \code{xdat}, \code{ydat}, and
 \code{zdat} 
 parameters. Use of these two interfaces is \bold{mutually exclusive}.

 Data contained in the data frame \code{zdat} may be a mix of continuous
 (default), unordered discrete (to be specified in the data frame
 \code{zdat} using \code{\link{factor}}), and ordered discrete (to be
 specified in the data frame \code{zdat} using
 \code{\link{ordered}}). Data can be entered in an arbitrary order and
 data types will be detected automatically by the routine (see
 \code{\link{np}} for details).

 Data for which bandwidths are to be estimated may be specified
 symbolically. A typical description has the form \code{dependent}
 \code{data} \code{~} \code{parametric} \code{explanatory} \code{data}
 \code{|} \code{nonparametric} \code{explanatory} \code{data},
 where \code{dependent} \code{data} is a univariate response, and
 \code{parametric} \code{explanatory} \code{data} and
 \code{nonparametric} \code{explanatory}
 \code{data} are both series of variables specified by name, separated by
 the separation character '+'. For example, \code{ y1 ~ x1 + x2 | z1 }
 specifies that the bandwidth object for the partially linear model with
 response \code{y1}, linear parametric regressors \code{x1} and
 \code{x2}, and 
 nonparametric regressor \code{z1} is 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.

 When the nonparametric component is estimated with
 \code{regtype="lp"} and \code{degree.select != "manual"},
 \code{npplregbw} determines the \code{zdat}-side degree vector and
 associated bandwidth coordinates separately for the nuisance
 regressions \eqn{E[Y \mid Z]} and \eqn{E[X_j \mid Z]}. With
 \code{search.engine="cell"}, the criterion is profiled over the degree
 grid using cached coordinate-wise or exhaustive search together with
 repeated fixed-degree bandwidth solves. With
 \code{search.engine="nomad"} or \code{"nomad+powell"}, each nuisance
 criterion is optimized directly over that child's degree/bandwidth
 space using \code{crs::snomadr()}; \code{"nomad+powell"} then performs
 one Powell hot start for that child and keeps the better of the direct
 NOMAD and polished solutions. For the nonparametric regression
 components, this
 polynomial-adaptive joint-search route follows Hall and Racine (2015).

 Setting \code{nomad=TRUE} is a convenience preset for this automatic
 LP route, not a generic optimizer alias. For partially linear
 regression it expands any missing values to the equivalent long-form
 call
 \preformatted{
   npplregbw(...,
             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.
}
\value{
  if \code{bwtype} is set to \code{fixed}, an object containing bandwidths
  (or scale factors if \code{bwscaling = TRUE}) is returned. If it is set to
  \code{generalized_nn} or \code{adaptive_nn}, then instead the \eqn{k}th nearest
  neighbors are returned for the continuous variables while the discrete
  kernel bandwidths are returned for the discrete variables. Bandwidths
  are stored in a list under the component name \code{bw}. Each element
  is an \code{rbandwidth} object. The first
  element of the list corresponds to the regression of \eqn{Y} on \eqn{Z}.  
  Each subsequent element is the bandwidth object corresponding to the
  regression of the \eqn{i}th column of \eqn{X} on \eqn{Z}. See examples
  for more information.
}

\section{Book And Method Pointers}{
  \code{npplregbw} selects the bandwidths and, for local-polynomial
  routes, child-specific degree/search metadata used by
  \code{\link{npplreg}}. The target model is
  \eqn{Y=X\beta+\Theta(Z)+\epsilon}; the bandwidth object controls the
  nuisance regressions \eqn{E[Y\mid Z]} and \eqn{E[X_j\mid Z]} used in
  the final partially linear solve.

  For book-length derivations, see Li and Racine (2007), Chapter 7
  \emph{Semiparametric Partially Linear Models}, especially Sections 7.1,
  7.2, and 7.4, and Racine (2019), Chapter 8 \emph{Semiparametric
  Conditional Mean Function Estimation}.
}

\references{

 Aitchison, J. and C.G.G. Aitken (1976), \dQuote{Multivariate binary
 discrimination by the kernel method,} Biometrika, 63, 413-420.

 Gao, Q. and L. Liu and J.S. Racine (2015), \dQuote{A partially linear
 kernel estimator for categorical data,} Econometric Reviews, 34 (6-10),
 958-977.

 Hall, P. and J.S. Racine (2015), \dQuote{Infinite Order Cross-Validated
 Local Polynomial Regression,} Journal of Econometrics, 185, 510-525.

 Li, Q. and J.S. Racine (2007), \emph{Nonparametric Econometrics: Theory
 and Practice,} Princeton University Press.

 Li, Q. and J.S. Racine (2004), \dQuote{Cross-validated local linear
   nonparametric regression,} Statistica Sinica, 14, 485-512.

 Pagan, A. and A. Ullah (1999), \emph{Nonparametric Econometrics,}
 Cambridge University Press.
 
 Racine, J.S. and Q. Li (2004), \dQuote{Nonparametric estimation of regression
 functions with both categorical and continuous data,} Journal of
 Econometrics, 119, 99-130.

 Robinson, P.M. (1988), \dQuote{Root-n-consistent semiparametric regression,}
 Econometrica, 56, 931-954. 
 
 Wang, M.C. and J. van Ryzin (1981), \dQuote{A class of smooth estimators
 for discrete distributions,}  Biometrika, 68, 301-309.

}
\author{
    Tristen Hayfield \email{tristen.hayfield@gmail.com}, Jeffrey S. Racine
    \email{racinej@mcmaster.ca}
}

\section{Usage Issues}{
  If you are using data of mixed types, then it is advisable to use the
  \code{\link{data.frame}} function to construct your input data and not
  \code{\link{cbind}}, since \code{\link{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, \emph{computationally intensive}. Virtually all methods
  require dropping the \eqn{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 \emph{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, \emph{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(zdat))
  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
  \code{bws=bw} on subsequent calls to this routine where \code{bw} is
  the initial bandwidth object).  A version of this package using the
  \code{Rmpi} wrapper is under development that allows one to deploy
  this software in a clustered computing environment to facilitate
  computation involving large datasets.
}

\seealso{
  \code{\link{np.kernels}}, \code{\link{np.options}}, \code{\link{plot}}, \code{\link{plot.np}}
  \code{\link{npregbw}}, \code{\link{npreg}} }
\examples{
\dontrun{
# EXAMPLE 1 (INTERFACE=FORMULA): For this example, we simulate an
# example for a partially linear model and perform bandwidth selection

set.seed(42)

n <- 250
x1 <- rnorm(n)
x2 <- rbinom(n, 1, .5)

z1 <- rbinom(n, 1, .5)
z2 <- rnorm(n)

y <- 1 + x1 + x2 + z1 + sin(z2) + rnorm(n)

X <- data.frame(x1, factor(x2))
Z <- data.frame(factor(z1), z2)

# Compute data-driven bandwidths... this may take a minute or two
# depending on the speed of your computer...

bw <- npplregbw(formula=y~x1+factor(x2)|factor(z1)+z2)

summary(bw)

# Note - the default is to use the local constant estimator. If you wish
# to use instead a local linear estimator, this is accomplished via
# npplregbw(xdat=X, zdat=Z, ydat=y, regtype="ll")

# Note - see the example for npudensbw() for multiple illustrations
# of how to change the kernel function, kernel order, and so forth.

# You may want to manually specify your bandwidths
bw.mat <- matrix(data =  c(0.19, 0.34,  # y on Z
                           0.00, 0.74,  # X[,1] on Z
                           0.29, 0.23), # X[,2] on Z
                ncol = ncol(Z), byrow=TRUE)

bw <- npplregbw(formula=y~x1+factor(x2)|factor(z1)+z2, 
                          bws=bw.mat, bandwidth.compute=FALSE)
summary(bw)

# Sleep for 5 seconds so that we can examine the output...

if (interactive()) Sys.sleep(5)

# You may want to tweak some of the bandwidths
bw$bw[[1]] # y on Z, alternatively bw$bw$yzbw
bw$bw[[1]]$bw <- c(0.17, 0.30)

bw$bw[[2]] # X[,1] on Z
bw$bw[[2]]$bw[1] <- 0.00054

summary(bw)

# EXAMPLE 1 (INTERFACE=DATA FRAME): For this example, we simulate an
# example for a partially linear model and perform bandwidth selection

set.seed(42)

n <- 250
x1 <- rnorm(n)
x2 <- rbinom(n, 1, .5)

z1 <- rbinom(n, 1, .5)
z2 <- rnorm(n)

y <- 1 + x1 + x2 + z1 + sin(z2) + rnorm(n)

X <- data.frame(x1, factor(x2))
Z <- data.frame(factor(z1), z2)

# Compute data-driven bandwidths... this may take a minute or two
# depending on the speed of your computer...

bw <- npplregbw(xdat=X, zdat=Z, ydat=y)

summary(bw)

# Note - the default is to use the local constant estimator. If you wish
# to use instead a local linear estimator, this is accomplished via
# npplregbw(xdat=X, zdat=Z, ydat=y, regtype="ll")

# Note - see the example for npudensbw() for multiple illustrations
# of how to change the kernel function, kernel order, and so forth.

# You may want to manually specify your bandwidths
bw.mat <- matrix(data =  c(0.19, 0.34,  # y on Z
                           0.00, 0.74,  # X[,1] on Z
                           0.29, 0.23), # X[,2] on Z
                ncol = ncol(Z), byrow=TRUE)

bw <- npplregbw(xdat=X, zdat=Z, ydat=y, 
                          bws=bw.mat, bandwidth.compute=FALSE)
summary(bw)

# Sleep for 5 seconds so that we can examine the output...

if (interactive()) Sys.sleep(5)

# You may want to tweak some of the bandwidths
bw$bw[[1]] # y on Z, alternatively bw$bw$yzbw
bw$bw[[1]]$bw <- c(0.17, 0.30)

bw$bw[[2]] # X[,1] on Z
bw$bw[[2]]$bw[1] <- 0.00054

summary(bw)
} % enddontrun
}
\keyword{nonparametric}
