liveBook · Manning

K-nearest neighbors

As explained in Causal Inference for Data Science

K-nearest neighbors is a prediction method that uses the outcomes of the closest training observations to predict an input's outcome.

K-nearest neighbors (kNN) predicts an outcome from the outcomes of the k most similar observations. In causal matching, similarity is measured using propensity scores. section 5.3.4

A causal matching workflow uses kNN to improve on single-patient matches:

  • Separate observations into treated and control datasets. section 5.3.4
  • Train outcome-prediction models on each group, using propensity score as the feature and outcome O as the target. section 5.3.4
  • Choose k as a hyperparameter, using cross-validation rather than relying on one potentially extreme match. section 5.3.4
  • Predict counterfactual outcomes across groups, calculate individual treatment effects, and average them to estimate the ATE. section 5.3.4

Treatment-effect matching

For a treated patient with propensity score sᵢ, use the kNN trained on controls to predict the expected untreated outcome at sᵢ. Reverse the process for control patients, then average the resulting treatment effects. section 5.3.4

Table 5.4 Treatment data to be used with kNN, Dt·n_t is the number of patients in the treatment group.
O
S
o 1
s 1
…
…
O n_t
s n_t

Inspect the treatment dataset containing outcomes and propensity scores used to train one kNN model. section 5.3.4

Table 5.5 Control data to be used with kNN, Dc·n_c is the number of patients in the control group.
O
S
o 1
s 1
…
…
O n_c
s n_c

Inspect the corresponding control dataset used to train the comparison kNN model. section 5.3.4

How Causal Inference for Data Science covers it

How deep it goes
Explains separate-group models, counterfactual predictions, averaging, and cross-validated k selection; stops short of broader algorithmic internals.
Examples
Uses treated and control patient datasets containing outcomes and propensity scores, with individual treatment effects averaged into an ATE.
What it assumes
Assumes familiarity with propensity scores, treatment groups, outcomes, and the adjustment formula.
What you can do after
Apply kNN to estimate counterfactual outcomes and calculate an average treatment effect from treated and control data.
How it connects
Connects propensity scores, matching, supervised learning, cross-validation, counterfactual outcomes, and the adjustment formula.

Also explained in

Compare how 3 books explain k-nearest neighbors →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Causal Inference for Data Science. The section links point to the parts of the book it draws on.