linearmodels

本条目面向分析数据与指标归因场景,提供“linearmodels”技能的目录入口。公开标签:learning、面板数据、工具变量、GMM。请通过固定上游版本核对实际功能、依赖与权限;此页为 Skillπ 独立编写的用途介绍和原站链接,不代表运行安全保证。原文与附件依各自许可开放。

仓库
brycewang-stanford/Auto-Empirical-Research-Skills
仓库最近推送
Stars
5,023
Forks
578
Issues / PRs
0

安装方式

先用 Prompt 核对来源,也可切换到安装命令或下载可用的安装包。

安装方式

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "linearmodels":https://skillpi.com/zh/skills/deepskill-93ff2dbb1eff9f04d727a2ead05252c934f1719a5ade4293e4d9bf2cf3c64dd5。安装前核对上游来源和版本:https://github.com/brycewang-stanford/Auto-Empirical-Research-Skills/blob/bfa0a6e5a843babbf45a1ef1663fd5dc4f07c0ba/skills/17-DAAF-Contribution-Community-daaf/dot-claude/skills/linearmodels/SKILL.md。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

此命令从 GitHub 来源仓库获取指定技能。运行前请先查看源文件。

运行工具
npx skills add https://github.com/brycewang-stanford/Auto-Empirical-Research-Skills/tree/bfa0a6e5a843babbf45a1ef1663fd5dc4f07c0ba/skills/17-DAAF-Contribution-Community-daaf/dot-claude/skills/linearmodels --skill linearmodels
许可协议: —

来源与配置

查看 GitHub 源码

brycewang-stanford/Auto-Empirical-Research-Skills固定提交 bfa0a6e5a843babbf45a1ef1663fd5dc4f07c0ba

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

本条目面向分析数据与指标归因场景,提供“linearmodels”技能的目录入口。公开标签:learning、面板数据、工具变量、GMM。请通过固定上游版本核对实际功能、依赖与权限;此页为 Skillπ 独立编写的用途介绍和原站链接,不代表运行安全保证。原文与附件依各自许可开放。

↓ 0 下载 ↗ 0 分享点击

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
---
name: linearmodels
description: >-
  Panel data, IV/GMM, system regression. PanelOLS (FE/RE), BetweenOLS, Fama-MacBeth, IV2SLS/LIML/GMM, SUR, 3SLS, Driscoll-Kraay SEs. Use for RE/between, system estimation, or GMM. Complements pyfixest (FE + DiD) and statsmodels (GLM + time series).
metadata:
  audience: research-coders
  domain: python-library
  library-version: "7.0"
  skill-last-updated: "2026-03-27"
---

# linearmodels Skill

linearmodels: panel data, IV/GMM, system regression, and asset pricing models in Python. Covers PanelOLS (FE/RE), BetweenOLS, FirstDifferenceOLS, Fama-MacBeth, IV2SLS/LIML/GMM, SUR, IV3SLS, and Driscoll-Kraay SEs. Use for random effects estimation, between or first-difference panel models, system estimation (SUR, 3SLS), LIML/GMM instrumental variables, Fama-MacBeth regressions, or Driscoll-Kraay standard errors. Complements pyfixest (high-dimensional FE + DiD) and statsmodels (GLM + time series).

Comprehensive skill for panel data estimation, instrumental variables, system regression, and asset pricing with linearmodels (Kevin Sheppard). Use decision trees below to find the right guidance, then load detailed references.

## What is linearmodels?

linearmodels extends statsmodels with specialized model classes for structured data:
- **Panel data**: PanelOLS (fixed effects), RandomEffects, BetweenOLS, FirstDifferenceOLS, PooledOLS, FamaMacBeth
- **Instrumental variables**: IV2SLS, IVLIML (k-class), IVGMM, IVGMMCUE (continuously updating), AbsorbingLS
- **System estimation**: SUR (Seemingly Unrelated Regression), IV3SLS, IVSystemGMM
- **Asset pricing**: LinearFactorModel, LinearFactorModelGMM, TradedFactorModel
- **Rich inference**: Driscoll-Kraay, clustered (1- and 2-way), HAC kernels (Bartlett, Parzen, Quadratic Spectral)
- **Dual API**: Formula-based (via formulaic) and array-based interfaces

## How to Use This Skill

### Reference File Structure

| File | Purpose | When to Read |
|------|---------|--------------|
| `quickstart.md` | Installation, MultiIndex setup, formula vs array API, first model | Starting with linearmodels |
| `panel-models.md` | PanelOLS, RandomEffects, BetweenOLS, FD, Pooled, FamaMacBeth | Panel data estimation |
| `iv-models.md` | IV2SLS, IVLIML, IVGMM, IVGMMCUE, AbsorbingLS | IV / GMM estimation |
| `system-models.md` | SUR, IV3SLS, IVSystemGMM, cross-equation constraints | System estimation |
| `asset-pricing.md` | LinearFactorModel, TradedFactorModel, GMM estimation | Asset pricing tests |
| `covariance-inference.md` | All SE types, Driscoll-Kraay, clustering, GMM weights | Choosing standard errors |
| `gotchas.md` | MultiIndex requirement, pyfixest/statsmodels boundary, limits | Debugging issues |

### Reading Order

1. **New to linearmodels?** Start with `quickstart.md` then `panel-models.md`
2. **Need IV/GMM?** Read `quickstart.md` then `iv-models.md`
3. **System estimation (SUR/3SLS)?** Read `quickstart.md` then `system-models.md`
4. **Asset pricing?** Read `quickstart.md` then `asset-pricing.md`
5. **Choosing SEs?** Read `covariance-inference.md`
6. **Coming from pyfixest?** Read `quickstart.md` then `gotchas.md`

## Related Skills

| Skill | Relationship |
|-------|-------------|
| `pyfixest` | Preferred for high-dimensional FE, FE + IV, DiD, fast demeaning, publication tables. Use linearmodels when pyfixest cannot do what you need (RE, system models, LIML/GMM, Fama-MacBeth) |
| `statsmodels` | Foundation library. Use statsmodels for GLM, time series, diagnostics. linearmodels extends statsmodels for panel/IV/system models |
| `svy` | Survey-weighted regression with complex survey designs. linearmodels supports `weights` for population/precision weighting in panel models, but this is NOT equivalent to design-based survey inference — it does not handle stratification, clustering as a design feature, or replicate weights. If your data comes from a complex probability survey, use `svy` |
| `data-scientist` | Methodology guidance — load for "why and when" behind model choices |
| `polars` | Data preparation before estimation; convert to pandas with `.to_pandas()` before passing to linearmodels |

## Quick Decision Trees

### "I need a panel model"

```
What panel estimation method?
├─ Fixed effects (within estimator)
│   ├─ 1-2 way FE, no IV → linearmodels PanelOLS or pyfixest feols
│   ├─ 3+ way FE → pyfixest (linearmodels max 2-way in PanelOLS)
│   ├─ FE + IV combined → pyfixest (linearmodels has no Panel IV)
│   └─ FE + DiD → pyfixest (linearmodels has no DiD)
├─ Random effects (GLS) → linearmodels RandomEffects
│   └─ → ./references/panel-models.md
├─ FE vs RE comparison → linearmodels (run both, compare)
│   └─ → ./references/panel-models.md
├─ Between estimator → linearmodels BetweenOLS
│   └─ → ./references/panel-models.md
├─ First difference → linearmodels FirstDifferenceOLS
│   └─ → ./references/panel-models.md
├─ Pooled OLS (panel-aware SEs) → linearmodels PooledOLS
│   └─ → ./references/panel-models.md
└─ Fama-MacBeth → linearmodels FamaMacBeth
    └─ → ./references/panel-models.md
```

### "I need IV / GMM estimation"

```
What IV method?
├─ 2SLS (standard IV)
│   ├─ With fixed effects → pyfixest (linearmodels has no Panel IV)
│   └─ Without FE → linearmodels IV2SLS or pyfixest
│       └─ → ./references/iv-models.md
├─ LIML / k-class (better finite-sample) → linearmodels IVLIML
│   └─ → ./references/iv-models.md
├─ GMM-IV (efficient, overidentified) → linearmodels IVGMM
│   └─ → ./references/iv-models.md
├─ Continuously updating GMM → linearmodels IVGMMCUE
│   └─ → ./references/iv-models.md
└─ High-dimensional absorbed FE (OLS) → linearmodels AbsorbingLS
    └─ → ./references/iv-models.md
```

### "I need system estimation"

```
System of equations?
├─ Multiple equations, correlated errors → SUR
│   └─ → ./references/system-models.md
├─ Multiple equations + endogenous variables → IV3SLS
│   └─ → ./references/system-models.md
├─ System GMM → IVSystemGMM
│   └─ → ./references/system-models.md
├─ Cross-equation parameter restrictions → LinearConstraint
│   └─ → ./references/system-models.md
└─ Not sure which → Start with SUR
    └─ → ./references/system-models.md
```

### "Something isn't working"

```
Having issues?
├─ TypeError about DataFrame index → ./references/gotchas.md
├─ Need FE + IV in one model → ./references/gotchas.md
├─ Need 3+ way fixed effects → ./references/gotchas.md
├─ Constant term confusion → ./references/gotchas.md
├─ Formula parsing errors → ./references/gotchas.md
├─ Want to compare with pyfixest → ./references/gotchas.md
└─ SUR performance issues → ./references/gotchas.md
```

## File-First Execution in Research Workflows

**Important:** In data research pipelines (see `CLAUDE.md`), linearmodels estimation is executed through **script files**, not interactively. This ensures auditability and reproducibility.

**The pattern:**
1. Write model code to `scripts/stage8_analysis/{step}_{task-name}.py`
2. Execute via Bash with automatic output capture wrapper script
3. Validation results get automatically embedded in scripts as comments
4. If failed, create versioned copy for fixes

Closely read `agent_reference/SCRIPT_EXECUTION_REFERENCE.md` for the mandatory file-first execution protocol covering complete code file writing, output capture, and file versioning rules.

The examples below show linearmodels syntax. In research workflows, wrap them in scripts following the file-first pattern.

---

## Quick Reference

### Essential Imports

```python
from linearmodels.panel import PanelOLS, RandomEffects, BetweenOLS
from linearmodels.panel import FirstDifferenceOLS, PooledOLS, FamaMacBeth
from linearmodels.iv import IV2SLS, IVLIML, IVGMM, IVGMMCUE, AbsorbingLS
from linearmodels.system import SUR, IV3SLS, IVSystemGMM
from linearmodels.panel import compare  # Panel model comparison tables
```

### Data Setup (Critical — MultiIndex Required for Panel Models)

```python
import pandas as pd

# Panel data MUST have a MultiIndex with (entity, time)
df = df.set_index(["entity_id", "year"])

# Verify the index
print(f"Index names: {df.index.names}")
print(f"Index levels: {df.index.nlevels}")
```

### Core Operations

| Operation | Code |
|-----------|------|
| Panel FE (formula) | `PanelOLS.from_formula("y ~ x1 + x2 + EntityEffects", data=df).fit()` |
| Panel FE (array) | `PanelOLS(df.y, df[["x1","x2"]], entity_effects=True).fit()` |
| Two-way FE | `PanelOLS.from_formula("y ~ x1 + EntityEffects + TimeEffects", data=df).fit()` |
| Random effects | `RandomEffects.from_formula("y ~ 1 + x1 + x2", data=df).fit()` |
| Between OLS | `BetweenOLS.from_formula("y ~ 1 + x1 + x2", data=df).fit()` |
| First difference | `FirstDifferenceOLS.from_formula("y ~ x1 + x2", data=df).fit()` |
| Fama-MacBeth | `FamaMacBeth.from_formula("y ~ 1 + x1 + x2", data=df).fit()` |
| IV / 2SLS | `IV2SLS.from_formula("y ~ 1 + exog + [endog ~ inst]", data=df).fit()` |
| LIML | `IVLIML.from_formula("y ~ 1 + exog + [endog ~ inst]", data=df).fit()` |
| Clustered SEs | `mod.fit(cov_type="clustered", cluster_entity=True)` |
| Driscoll-Kraay | `mod.fit(cov_type="kernel", kernel="bartlett", bandwidth=5)` |
| Summary | `results.summary` |
| Model comparison | `compare({"FE": fe_res, "RE": re_res})` |

### Formula Syntax

```python
# Panel FE keywords (appear in formula, not after |)
"y ~ x1 + x2 + EntityEffects"               # Entity FE
"y ~ x1 + x2 + EntityEffects + TimeEffects"  # Two-way FE
"y ~ x1 + x2 + TimeEffects"                  # Time FE only

# IV bracket notation
"y ~ 1 + exog + [endog ~ instrument1 + instrument2]"

# Suppress intercept
"y ~ x1 + x2 - 1"
```

## Topic Index

| Topic | Reference File |
|-------|---------------|
| Installation | `./references/quickstart.md` |
| MultiIndex data setup | `./references/quickstart.md` |
| Formula vs array API | `./references/quickstart.md` |
| First model | `./references/quickstart.md` |
| Syntax comparison (pyfixest, statsmodels) | `./references/quickstart.md` |
| PanelOLS (entity/time effects) | `./references/panel-models.md` |
| RandomEffects | `./references/panel-models.md` |
| BetweenOLS | `./references/panel-models.md` |
| FirstDifferenceOLS | `./references/panel-models.md` |
| PooledOLS | `./references/panel-models.md` |
| FamaMacBeth | `./references/panel-models.md` |
| FE vs RE decision | `./references/panel-models.md` |
| Variance decomposition | `./references/panel-models.md` |
| Weighted panel estimation | `./references/panel-models.md` |
| R-squared types (within, between, overall) | `./references/panel-models.md` |
| IV2SLS | `./references/iv-models.md` |
| IVLIML and k-class estimators | `./references/iv-models.md` |
| IVGMM (1-step, 2-step, iterative) | `./references/iv-models.md` |
| IVGMMCUE | `./references/iv-models.md` |
| AbsorbingLS (high-dim FE OLS) | `./references/iv-models.md` |
| First-stage diagnostics | `./references/iv-models.md` |
| Overidentification tests | `./references/iv-models.md` |
| SUR (Seemingly Unrelated Regression) | `./references/system-models.md` |
| IV3SLS | `./references/system-models.md` |
| IVSystemGMM | `./references/system-models.md` |
| Cross-equation constraints | `./references/system-models.md` |
| LinearFactorModel | `./references/asset-pricing.md` |
| TradedFactorModel | `./references/asset-pricing.md` |
| Factor model GMM | `./references/asset-pricing.md` |
| Driscoll-Kraay SEs | `./references/covariance-inference.md` |
| Clustered SEs (entity, time, both) | `./references/covariance-inference.md` |
| HAC / kernel covariance | `./references/covariance-inference.md` |
| GMM weight matrices | `./references/covariance-inference.md` |
| Debiased inference | `./references/covariance-inference.md` |
| MultiIndex requirement | `./references/gotchas.md` |
| Maximum 2-way FE limit | `./references/gotchas.md` |
| No Panel IV | `./references/gotchas.md` |
| pyfixest vs linearmodels boundary | `./references/gotchas.md` |
| statsmodels vs linearmodels boundary | `./references/gotchas.md` |
| Constant term handling | `./references/gotchas.md` |

## Citation

When this library is used as a primary analytical tool, include in the report's
Software & Tools references:

> Sheppard, K. linearmodels: Econometric models for panel data, IV/GMM, and system regression [Computer software]. https://bashtage.github.io/linearmodels/

**Cite when:** linearmodels is used for panel estimation (RE, between), IV/GMM, Fama-MacBeth, or system regression (SUR, 3SLS).
**Do not cite when:** Only imported but no estimation performed.

For method-specific citations (e.g., individual estimators or techniques),
consult the reference files in this skill and `agent_reference/CITATION_REFERENCE.md`.

版本历史

  1. v2026.07 GitHub ↗
    原版更新说明

    ## Highlights - **First tagged release.** Every gate is green: `make check` (validate + 183 unit tests + eval-harness + numeric benchmarks), validate-catalog, quality-evals, and OpenSSF Scorecard on `main`. - **Rigor double loop at 13 method families.** Each family closes taxonomy tag → eval scenario (does the agent avoid the method's known traps?) → deterministic numeric benchmark (does it recover the true effect?). New this cycle: heterogeneous treatment effects (CATE) and quantile treatment effects (QTE). - **Paper-WorkFlow flagship at 29/29 executable rigor gates**, now merged to its `main` with a drift-gated README badge — the credibility artifact is checked by CI, so it can never silently go stale ([RIGOR.md](https://github.com/brycewang-stanford/Paper-WorkFlow/blob/main/RIGOR.md)). - **1,150 vendored skills / 69 collections**, all license-audited, security-scanned, tracked in a machine-readable catalog with CI-enforced freshness, and importable as a single whole-repo router skill. - **Hardened weekly upstream syncs**: synced trees must pass the full gate before merge; this cycle both sync PRs were verified, patched (StatsPAI's SkillOpt execution-gate card restored), and merged. - **First-party plugin marketplace** (Python / R / Stata empirical-analysis plugins + guide) shipped in the previous cycle, now under a tagged version. ## Known Follow-Ups - Ecosystem benchmark comparison (AERS vs econometrics agents on the shared benchmark suite) — [PLAN-2026-07 week 3](https://github.com/brycewang-stanford/Auto-Empirical-Research-Skills/blob/main/docs/PLAN-2026-07.md). - Decomposition (Oaxaca–Blinder) and shift-share (Bartik) method families (weeks 3–4). - GitHub Pages catalog site, README demo screenshots, five-language README consistency script (week 4). ## Catalog - Top-level collections: **69** - `SKILL.md` files: **1150** - Cataloged agent tools: **335** ([`tools/CATALOG.md`](../tools/CATALOG.md)) ## Methodological rigor - Method families in the coverage map: **13** ([`RIGOR_COVERAGE.md`](RIGOR_COVERAGE.md)) - Eval scenarios: **26** (138 rubric items, 122 auto-checkable) ([`../eval-harness/`](../eval-harness/README.md)) - Benchmark tasks: **13** (70 gold checks, 58 required) ([`../benchmark/`](../benchmark/README.md)) ### Method families (skills tagged) - `iv`: 27 - `rdd`: 25 - `panel-fe`: 19 - `staggered-did`: 17 - `bayesian`: 13 - `did`: 10 - `event-study`: 10 - `matching`: 10 - `synthetic-control`: 10 - `cate`: 8 - `dml`: 8 - `survival`: 3 - `quantile`: 2 ### Eval scenarios by category - `causal-identification`: 14 - `citation-hygiene`: 1 - `reproducibility`: 2 - `research-integrity`: 1 - `runtime-safety`: 1 - `statistical-validity`: 2 - `writing-compliance`: 3 - `writing-style`: 2 ## License buckets - AGPL-3.0: 1 - Apache-2.0: 1 - CC-BY-NC-4.0 (non-commercial): 1 - CC-BY-SA-4.0: 1 - CC-BY-SA-4.0 (repository default): 4 - GPL-3.0: 1 - MIT: 35 - MIT Non-Commercial: 1 - Mixed (per-skill; see folder): 2 - UNKNOWN - check upstream: 22 ## Quality gates - `make check` — catalog/provenance/audit/eval freshness, repo hygiene, Paper-WorkFlow gates, stdlib unit tests, eval-harness lint + graded smoke, and the strict numeric benchmark (reference-gold drift fails the build). - External links and OpenSSF Scorecard run on their scheduled workflows.

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分 100。前四项来自 DeepSkill 收录的质量报告。

场景价值85/100
评分依据

提供针对面板数据、工具变量、系统回归等高级计量经济学方法的详细指导

触发精准90/100
评分依据

description 字段精准描述了 Skill 的核心功能和适用场景

结构规范95/100
评分依据

结构清晰,包含多个章节和明确的阅读顺序

执行可靠90/100
评分依据

提供了详细的代码示例、数据设置要求和核心操作

证据可验 (前四项均值)90/100
计算方式

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

文档引用的域名或地址

这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。

  • bashtage.github.io

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

risk · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。

来源与使用声明 SkillPi 的部分技能资料来自公开来源或创作者提交。第三方内容的权利归各自权利人所有。安装或运行前,请查看原站的许可、权限要求和使用风险。

来源与使用

请按详情页的作者、原始来源和许可证核对使用条件,运行前检查技能需要的权限。目录收录不等于已验证安装或安全性。