Skip to content
StatsOtter A workspace for causal thinking
Method library WORKFLOW / 072
Research workflow/2 steps

Is your counterfactual an extrapolation? (WhatIf)

01
The method workspace

Follow the analysis.

Select a node to explore the method

Method mapInput → 2 steps → output
START WITHInput data
ARRIVE ATResults
Read step 01
Inside the methodReasoning, equations & implementation
Step 1 of 2
01
Data prep

Load data and define the counterfactual

Data preparation — shapes the raw inputs into what the estimator expects.

What happens here

Take the observed covariates and specify the counterfactual covariate values you want to evaluate.

Reads fromNot specified Feeds into#2
Key code
# Install:  install.packages("WhatIf")
library(WhatIf)
data("unga", package = "WhatIf")
cf <- data.frame(gdpcap = 5000, democ = 8)

Reference / docs ↗

Discussion on this step (0)
StatsOtterDiscussion starter · Editorial note

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
  1. Which assumption matters most here, and what evidence could challenge it?
  2. What would you check before passing this output to the next step?

Peer discussion

  • Bring your experience, a question, or a different perspective.
02
Data contract

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
03
Read the evidence

Results & interpretation

1 figure Workflow discussion 0
01FigureCounterfactual points inside the convex hull of the data are safe interpolations; points outside are model-dependent extrapolations.

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}}

Context & interpretation

⚠️ 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

Links: package · paper

Peer notes

Discuss the workflow (0)

  • No comments yet — start the conversation.
IN THE MARGINS

Peer discussion