Is your counterfactual an extrapolation? (WhatIf)
Follow the analysis.
Select a node to explore the method
Load data and define the counterfactual
Data preparation — shapes the raw inputs into what the estimator expects.
Take the observed covariates and specify the counterfactual covariate values you want to evaluate.
# Install: install.packages("WhatIf")
library(WhatIf)
data("unga", package = "WhatIf")
cf <- data.frame(gdpcap = 5000, democ = 8)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Run the convex-hull / distance test
A pre-flight check — run this before trusting any estimate downstream.
whatif() reports convex-hull membership and Gower-distance support for each counterfactual point.
wi <- whatif(formula = ~ gdpcap + democ, data = unga, cfact = cf)
summary(wi)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Inputs & data
Observed covariates plus one or more counterfactual covariate combinations to evaluate.
Show data format & exampleHide example
| gdpcap | democ |
|---|---|
| 4200 | 6 |
| 8800 | 9 |
| 1500 | 2 |
| cfact: 5000 | 8 |
Result figure rendered by StatsOtter from the package's documented example — unofficial community showcase; all credit to the original authors.
The estimate
x_{\text{cf}}\in\mathrm{conv}(X)\iff \exists\,w_i\ge0,\ \textstyle\sum_i w_i=1:\ \sum_i w_i x_i=x_{\text{cf}}
⚠️ Unofficial community showcase of WhatIf (docs). Not affiliated with the authors — all credit to Heather Stoll, Gary King & Langche Zeng; this summarizes public documentation.
What it does. Before trusting a predicted counterfactual, WhatIf (King & Zeng 2006) asks whether that covariate combination is actually supported by the data or whether the answer is an extrapolation that depends entirely on the model's functional form.
How it works. whatif() checks whether each counterfactual point lies inside the convex hull of the observed covariates (an exact linear-programming test), and computes Gower-distance summaries — the share of data nearby and the mean distance — so you can rank counterfactuals from well-supported to far-off.
Assumptions. It is model-free: a purely geometric statement about how far a counterfactual sits from the data, used to gauge how model-dependent any downstream inference would be.
What you get — For each counterfactual: whether it is inside the convex hull, the percent of data nearby, and the mean distance to the data.
Example output
Call:
whatif(formula = ~gdpcap + democ, data = unga, cfact = ...)
Counterfactual 1: in convex hull = FALSE
Percent of data nearby = 12.4%
Mean distance to data = 0.318
Discuss the workflow (0)
Log in to join the discussion.