Classification and Modes

Practical route to nonparametric classification and conditional-mode workflows in np.
Keywords

npconmode, classification, conditional mode, mode estimation, birthwt

For a categorical response, npconmode() estimates the class probabilities \(p_y(x)=P(Y=y\mid X=x)\) and predicts the conditional mode

\[ m(x)=\underset{y}{\operatorname{arg\,max}}\;p_y(x). \]

This provides a nonparametric classifier without imposing a link function or linear index.

If you want a minimal downloadable script for the classification route, start with np_classification_quickstart.R.

The basic idea

The response may be binary, multinomial, or ordered, and the covariates may mix continuous, unordered, and ordered variables. Bandwidths and any local-polynomial degree search are inherited from the npcdensbw() conditional-density route.

A simple binary-outcome example

The older birthwt example remains a good illustration because it contains several categorical covariates that must be classed correctly.

## Class the categorical variables correctly, then fit parametric and np models
library(np)
data(birthwt, package = "MASS")

birthwt$low <- factor(birthwt$low)
birthwt$smoke <- factor(birthwt$smoke)
birthwt$race <- factor(birthwt$race)
birthwt$ht <- factor(birthwt$ht)
birthwt$ui <- factor(birthwt$ui)
birthwt$ftv <- factor(birthwt$ftv)

model_logit <- glm(low ~ smoke + race + ht + ui + ftv + age + lwt,
  family = binomial(link = logit),
  data = birthwt)

model_np <- npconmode(low ~ smoke + race + ht + ui + ftv + age + lwt,
  data = birthwt)

summary(model_np)

The default route uses the usual local-constant conditional-density fit. You can also allow the bandwidth search to choose a local-polynomial degree when there is at least one continuous conditioning variable:

model_np_nomad <- npconmode(low ~ smoke + race + ht + ui + ftv + age + lwt,
  data = birthwt,
  ## nomad = "auto" uses exhaustive search for p = 1 and NOMAD otherwise.
  nomad = "auto")

summary(model_np_nomad)

For non-local-constant routes, npconmode works with the full set of fitted probabilities over the discrete response support before choosing the modal outcome. The reported modal class is therefore based on probabilities that are non-negative and sum to one across the observed support.

The returned object also retains the conditional-density bandwidth object and mirrors the selected regression type, degree, NOMAD shortcut metadata, and search-engine information used to build the classifier. If you want to inspect the fitted class-probability matrix directly, set probabilities = TRUE.

Class-probability uncertainty

Probability plots can display asymptotic pointwise, simultaneous, and Bonferroni variability bands for one named response level over the declared grid. The target is the smooth class probability, not the discrete winning class label, and coverage is not joint across response levels.

## Reuse the selected bandwidths and retain probabilities and their SEs
model_np_prob <- npconmode(bws = model_np$bws,
  probabilities = TRUE,
  se = TRUE)
plot(model_np_prob, errors = "asymptotic", band = "all")

Eligible fixed surfaces draw the same available asymptotic interval families. Repeated conditioning-variable selectors are rejected rather than used to construct a misaligned surface. If clipping or probability projection makes a joint calculation nonregular, the simultaneous limits remain unavailable while valid pointwise and Bonferroni output is preserved.

Comparing with a parametric classifier

One natural comparison is a confusion matrix against a parametric logit model.

## Compare the parametric confusion matrix with the np conditional-mode result
pred_logit <- factor(ifelse(fitted(model_logit) > 0.5, "1", "0"),
  levels = levels(birthwt$low))

cm_logit <- table(birthwt$low, pred_logit)

cm_logit
model_np$confusion.matrix
model_np_nomad$confusion.matrix

mean(pred_logit == birthwt$low)
model_np$CCR.overall
model_np_nomad$CCR.overall

This compares a flexible conditional-mode classifier with a familiar parametric benchmark on the same response and covariates.

Practical notes

  • Class categorical variables before fitting.
  • Start with a modest problem size when exploring a specification.
  • Preserve the default search tolerances unless a deliberate exploratory run requires otherwise.
  • Examine fitted probabilities as well as the confusion matrix.

The fitted mode, class-probability matrix, and probability effects all use the same mixed-data conditional-density fit. The mode reports the most likely class; the probabilities show how strongly the fitted distribution supports that choice.

Back to top