Skip to contents

Constructs the conditional density hat operator associated with npcdens bandwidth objects. The returned operator maps a right-hand side y to \(H y\); with y = 1 this reproduces the fitted conditional density.

Usage

npcdenshat(bws,
           txdat = stop("training data 'txdat' missing"),
           tydat = stop("training data 'tydat' missing"),
           exdat,
           eydat,
           y = NULL,
           output = c("matrix", "apply"),
           deriv = NULL,
           s = NULL)

Arguments

Data, Bandwidth Inputs And Formula Interface

These arguments identify the fitted bandwidth object, training data, and evaluation data.

bws

A fitted conditional density bandwidth object of class "conbandwidth".

exdat

Optional evaluation conditioning data. If omitted, the operator is built on the training conditioning data.

eydat

Optional evaluation response data. If omitted, the operator is built on the training response data.

txdat

Training conditioning data used to construct the operator.

tydat

Training response data used to construct the operator.

Operator Output

These arguments control whether the operator is returned as a matrix or applied directly.

deriv

Convenience alias for s.

output

Either "matrix" for the hat matrix or "apply" for direct application to y.

s

Optional first-derivative multi-index over continuous conditioning variables. By default the conditional-density operator itself is returned. When s requests one first derivative, the returned operator maps y to the corresponding npcdens(..., gradients = TRUE) conditioning-variable gradient component.

y

Optional right-hand side vector or matrix with one row per training observation.

Details

For the adaptive-nearest-neighbour local-constant (including degree-zero local-polynomial) ratio, every finite nonzero conditioning-weight sum is used without an epsilon floor. If all computed conditioning weights at an external evaluation row are zero, that row is returned as NA with a warning. This describes computed weights, not necessarily absence of geometric support. Zero response weights still give a valid zero numerator when the conditioning denominator is defined. Required internal and bootstrap ratios remain strict.

For output = "matrix", the return value is a matrix with class c("npcdenshat", "matrix") and attributes storing the bandwidth object, training data, evaluation data, and call metadata.

For output = "apply", the function returns H y directly. Matrix right-hand sides are applied column-wise.

The optional s argument follows the derivative multi-index convention used by npreghat for continuous conditioning variables. The default s = NULL returns the ordinary conditional-density hat operator. Supplying one first derivative, for example s = 1L in a univariate continuous-\(x\) problem or s = c(x2 = 1L) in a multivariate problem, returns the hat operator for that component of the conditional-density gradient. This derivative route is intended to match npcdens(..., gradients = TRUE)$congrad.

For local-constant beta X kernels, first derivatives use the analytic beta PDF derivative. Exact support-endpoint rows whose raw weights jump are returned as NA, because a response-free hat matrix cannot perform the response-specific numerator/denominator cancellation available to the fitted conditional-density estimator.

This helper is intended for object-fed repeated evaluation once a bandwidth object has already been constructed. It does not perform bandwidth selection.

Value

Either a hat matrix of class "npcdenshat" or the applied result H y, depending on output.

Examples

if (FALSE) { # \dontrun{
data(cps71)
tx <- data.frame(age = cps71$age)
ty <- data.frame(logwage = cps71$logwage)

bw <- npcdensbw(xdat = tx, ydat = ty, bwtype = "fixed",
                bandwidth.compute = FALSE, bws = c(1.0, 1.0))

H <- npcdenshat(bws = bw, txdat = tx, tydat = ty)
dens.hat <- npcdenshat(bws = bw, txdat = tx, tydat = ty,
                       y = rep(1, nrow(tx)),
                       output = "apply")
dens.core <- fitted(npcdens(bws = bw, txdat = tx, tydat = ty))

head(cbind(dens.core, dens.hat), n = 2L)

grad.hat <- npcdenshat(bws = bw, txdat = tx, tydat = ty,
                       y = rep(1, nrow(tx)),
                       output = "apply", s = 1L)
grad.core <- gradients(npcdens(bws = bw, txdat = tx, tydat = ty,
                               gradients = TRUE))[, 1L]
head(cbind(grad.core, grad.hat), n = 2L)
} # }