Iv Regression Guide logo

Iv Regression Guide

Community
wentorai
iv-regression-guide

Apply instrumental variables, 2SLS, and address endogeneity issues

Overview

Publisherwentorai
Repositoryresearch-plugins
Skill nameiv-regression-guide
Stars
294
Forks
42
Bundled files
Instructions only
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by wentorai on GitHub. Read the source before you install it.

Installation

Install the Iv Regression Guide AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/wentorai/research-plugins.git /tmp/research-plugins
mkdir -p .claude/skills
cp -r /tmp/research-plugins/skills/analysis/econometrics/iv-regression-guide .claude/skills/iv-regression-guide
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Iv Regression Guide in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Iv Regression Guide on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Iv Regression Guide is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Instrumental Variables Regression Guide

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.

The Endogeneity Problem

Why OLS Fails

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.

The IV Solution

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)

Two-Stage Least Squares (2SLS)

How 2SLS Works

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.

Implementation in Python

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

Implementation in R

r
library(ivreg)

# 2SLS estimation
iv_model <- ivreg(
  log(wage) ~ education + experience | parent_education + experience,
  data = df
)

summary(iv_model, diagnostics = TRUE)

Instrument Validity Tests

First-Stage F-Statistic (Relevance)

python
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"
        ]
    }

Overidentification Test (Exclusion Restriction)

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)

Classic IV Examples

Famous Instruments in Economics

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)

Reporting IV Results

Required Elements

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.

Frequently asked questions

What does the Iv Regression Guide AI skill do?

Apply instrumental variables, 2SLS, and address endogeneity issues

Why use Iv Regression Guide on TypingMind?

Because you install it once and use it with any model. Iv Regression Guide is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Iv Regression Guide in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/wentorai/research-plugins/tree/main/skills/analysis/econometrics/iv-regression-guide. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Iv Regression Guide?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Iv Regression Guide?

As many as you like. As long as a model supports skills, you can use Iv Regression Guide with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Iv Regression Guide AI skill free?

Yes. It is published on GitHub by wentorai under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇