skills/43-wentorai-research-plugins/skills/analysis/econometrics/iv-regression-guide/SKILL.md
Apply instrumental variables, 2SLS, and address endogeneity issues
npx skillsauth add brycewang-stanford/Awesome-Agent-Skills-for-Empirical-Research iv-regression-guideInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
A skill for applying instrumental variables (IV) estimation to address endogeneity in regression models. Covers the logic of IV, two-stage least squares (2SLS), instrument validity tests, weak instrument diagnostics, and reporting standards.
Ordinary Least Squares assumes: E[u | X] = 0
(Regressors are uncorrelated with the error term)
This assumption is violated when:
- Omitted variable bias: A confound affects both X and Y
- Simultaneity: X affects Y and Y affects X
- Measurement error: X is measured with noise
Consequence: OLS estimates are biased and inconsistent.
No amount of data will fix this.
An instrumental variable Z satisfies two conditions:
1. Relevance: Z is correlated with the endogenous regressor X
Cov(Z, X) != 0
2. Exclusion: Z affects Y ONLY through X (not directly)
Cov(Z, u) = 0
Z --> X --> Y
Z -/-> Y (no direct path)
Stage 1: Regress the endogenous variable on the instrument(s)
X = gamma_0 + gamma_1 * Z + controls + v
Save the fitted values: X_hat
Stage 2: Regress the outcome on the fitted values
Y = beta_0 + beta_1 * X_hat + controls + e
The coefficient beta_1 is the IV estimate of the causal effect.
from linearmodels.iv import IV2SLS
import pandas as pd
def run_2sls(data: pd.DataFrame, dependent: str,
endogenous: str, instruments: list[str],
controls: list[str] = None) -> dict:
"""
Run a 2SLS instrumental variables regression.
Args:
data: DataFrame with all variables
dependent: Name of the dependent variable (Y)
endogenous: Name of the endogenous regressor (X)
instruments: List of instrument variable names (Z)
controls: List of exogenous control variable names
"""
controls = controls or []
exog_str = " + ".join(["1"] + controls) if controls else "1"
endog_str = endogenous
instr_str = " + ".join(instruments)
formula = f"{dependent} ~ {exog_str} + [{endog_str} ~ {instr_str}]"
model = IV2SLS.from_formula(formula, data)
result = model.fit(cov_type="robust")
return {
"coefficients": dict(result.params),
"std_errors": dict(result.std_errors),
"p_values": dict(result.pvalues),
"f_statistic_first_stage": result.first_stage.diagnostics,
"summary": str(result.summary)
}
library(ivreg)
# 2SLS estimation
iv_model <- ivreg(
log(wage) ~ education + experience | parent_education + experience,
data = df
)
summary(iv_model, diagnostics = TRUE)
def check_weak_instruments(first_stage_f: float) -> dict:
"""
Evaluate instrument strength using first-stage F-statistic.
Args:
first_stage_f: F-statistic from the first-stage regression
"""
return {
"f_statistic": first_stage_f,
"rule_of_thumb": (
"Strong instruments" if first_stage_f > 10
else "Potentially weak instruments"
),
"interpretation": (
"Stock & Yogo (2005) suggest F > 10 as a minimum for "
"one endogenous variable. For more precise thresholds, "
"consult the Stock-Yogo critical values table based on "
"the number of instruments and desired maximal bias."
),
"if_weak": [
"Use LIML (Limited Information Maximum Likelihood) instead of 2SLS",
"Report Anderson-Rubin confidence intervals (robust to weak IV)",
"Consider finding stronger instruments",
"Use the Lee et al. (2022) tF procedure for valid inference"
]
}
When you have more instruments than endogenous variables, the Hansen J test (or Sargan test) checks whether the extra instruments are valid:
H0: All instruments are valid (uncorrelated with the error)
H1: At least one instrument is invalid
If p < 0.05: Reject -> at least one instrument may violate exclusion
If p > 0.05: Fail to reject -> instruments appear valid
(but this test has low power)
Research Question | Endogenous Var | Instrument
---------------------------|---------------|------------------
Returns to education | Years of school| Quarter of birth (Angrist & Krueger)
Effect of institutions | Institutions | Settler mortality (Acemoglu et al.)
Colonial origins of trade | Trade openness | Geography (Frankel & Romer)
Effect of military service | Veteran status | Draft lottery number (Angrist)
Price elasticity of demand | Price | Supply shifters (cost, weather)
1. Justify instrument choice with economic/theoretical reasoning
2. Report first-stage regression results:
- Coefficient of Z on X with standard error
- First-stage F-statistic
3. Report second-stage (2SLS) results:
- IV coefficient with robust standard errors
- Compare with OLS estimate (discuss direction of bias)
4. Report diagnostic tests:
- Weak instrument test (F-statistic or Kleibergen-Paap)
- Overidentification test if applicable (Hansen J)
- Endogeneity test (Hausman or Durbin-Wu-Hausman)
5. Discuss threats to instrument validity
- Can the exclusion restriction be challenged?
- Are there plausible alternative channels?
Always present both OLS and IV estimates side by side. The comparison helps readers understand the direction and magnitude of endogeneity bias and assess whether the IV correction is meaningful.
development
Conduct rigorous thematic analysis (TA) of qualitative data following Braun and Clarke's (2006) six-phase framework. Use whenever the user mentions 'thematic analysis', 'TA', 'Braun and Clarke', 'qualitative coding', 'identifying themes', or asks for help analysing interviews, focus groups, open-ended survey responses, or transcripts to identify patterns. Also trigger for questions about inductive vs theoretical coding, semantic vs latent themes, essentialist vs constructionist epistemology, building a thematic map, or writing up a qualitative findings section. Covers all six phases, the four upfront analytic decisions, the 15-point quality checklist, and the five common pitfalls. Produces a Word document write-up and an annotated thematic map. Does NOT cover IPA, grounded theory, discourse analysis, conversation analysis, or narrative analysis — use a different method for those.
development
Guide users through writing a systematic literature review (SLR) following the PRISMA 2020 framework. Use this skill whenever the user mentions 'systematic review', 'systematic literature review', 'SLR', 'PRISMA', 'PRISMA 2020', 'PRISMA flow diagram', 'PRISMA checklist', or asks for help writing, structuring, or auditing a literature review that follows reporting guidelines. Also trigger when the user asks about inclusion/exclusion criteria for a review, search strategies for databases like Scopus/WoS/PubMed, study selection processes, risk of bias assessment, or narrative synthesis for a review paper. This skill covers the full PRISMA 2020 checklist (27 items), produces a Word document manuscript in strict journal article format, generates an annotated PRISMA flow diagram, and enforces APA 7th Edition referencing throughout. It does NOT cover meta-analysis or statistical pooling. By Chuah Kee Man.
testing
Performs placebo-in-time sensitivity analysis with hierarchical null model and optional Bayesian assurance. Use when checking model robustness, verifying lack of pre-intervention effects, or estimating study power.
data-ai
Fit, summarize, plot, and interpret a chosen CausalPy experiment. Use after the causal method has been selected, including when configuring PyMC/sklearn models and scale-aware custom priors.