Build With npksum
npksum, custom kernels, kernel sums, lower-level np
npksum() evaluates kernel-weighted sums that can be combined into custom estimators and test statistics without dropping immediately into C.
Why npksum matters
Use it when the required object is not already exposed by a higher-level estimator.
In practice, it can be used to:
- build custom kernel estimators,
- form weighted kernel sums,
- experiment with leave-one-out constructions,
- prototype new statistics and test objects.
A simple local-constant example
A concise illustration recovers the local-constant conditional-mean estimator
\[ \widehat g(x)= \frac{\sum_i K_h(x-X_i)Y_i}{\sum_i K_h(x-X_i)} \]
from two kernel sums.
## Recover a local-constant fit directly from the underlying kernel sums
library(np)
data(cps71, package = "np")
fit_lc <- npksum(txdat = cps71$age,
tydat = cps71$logwage,
bws = 2)$ksum / npksum(txdat = cps71$age,
bws = 2)$ksum
plot(cps71$age, cps71$logwage, xlab = "Age", ylab = "log(wage)", cex = 0.1)
lines(cps71$age, fit_lc, col = "blue")The numerator is a response-weighted kernel sum; the denominator normalizes the weights at each evaluation point.
Numeric bws input is materialized against the supplied data before dispatch. This matters for custom mixed-data calculations: continuous, unordered, and ordered columns retain their data-aware kernel types rather than being treated as a default all-continuous bandwidth object.
What to keep in mind
npksum() supplies kernel sums rather than a complete estimator. A custom construction should therefore:
- check your variable classes,
- check your bandwidth choices,
- verify the object you are constructing really corresponds to the statistic you intend,
- validate on small known cases first.
When should I use npksum?
Use it when:
- you are prototyping a custom estimator or test,
- you need direct access to weighted kernel sums,
- you want to expose the mechanics of a standard kernel estimator.
For standard estimation, prefer higher-level functions such as npreg(), npudens(), npcdens(), and npqreg().