跳到正文
cccskills
免费在 GitHub 公开

plm

R panel data models with plm: within (FE), random (RE), between, first difference, Fama-MacBeth. Hausman test, panel unit root tests. Includes estimatr for robust estimation and lme4 for mixed effects. Use when execution language is R. Python equivalent: linearmodels. For high- dimensional FE use fixest; for cross-sectional OLS/GLM use r-stats.

中文简介正在准备中。当前显示原文说明。

DAAF-Contribution-Community/daaf星标 238(GitHub 238 + cccskills 0)

实测评分 未实测

查看安装方法

包含的文件(8)

  • SKILL.md15.3 KB
  • references/diagnostics.md9.6 KB
  • references/gotchas.md11.1 KB
  • references/iv.md9.7 KB
  • references/mixed-effects.md11.1 KB
  • references/models.md12.1 KB
  • references/quickstart.md11.4 KB
  • references/robust.md8.8 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

plm Skill

R's canonical panel data modeling package. Covers within (FE), random effects (RE), between, first difference, and pooled OLS estimators via plm(), plus Fama-MacBeth via pmg() (requires a reversed, time-first index — see models.md). Provides the Hausman test (phtest()) for FE vs RE selection, LM tests for unobserved effects (plmtest()), panel serial correlation tests (pbgtest(), pwartest(), pbsytest()), cross-sectional dependence tests (pcdtest()), and panel unit root tests (purtest(), cipstest()). Includes estimatr for robust/cluster-robust estimation (lm_robust(), iv_robust()) and lme4 for mixed-effects models (lmer(), glmer()). Use when execution language is R and the analysis involves panel data requiring RE, between, first-difference, or Hausman testing. Python equivalent: linearmodels. For high-dimensional fixed effects, IV with FE, or DiD, use fixest. For cross-sectional OLS/GLM without panel structure, use r-stats.

What is plm?

plm (Croissant & Millo, 2008) is the standard R package for linear panel data models. It provides a unified formula interface that mirrors base R's lm() but adds panel-aware estimation:

  • Panel-aware data: pdata.frame() declares entity and time identifiers
  • Five estimators: within (FE), random (RE), between, first difference (fd), pooling -- selected via the model= argument to plm()
  • Fama-MacBeth: pmg(model = "mg") with a reversed (time-first) index — index = c("time", "entity") — so the per-group regressions are cross-sectional per period, not per-entity time series (see models.md)
  • Hausman test: phtest() for FE vs RE model selection
  • Dynamic panels: pgmm() for Arellano-Bond and Blundell-Bond GMM
  • Panel IV: plm() with instruments argument for 2SLS within panels
  • Rich diagnostics: serial correlation, cross-sectional dependence, unit root tests, all panel-specific

This skill also covers two companion packages:

PackageVersionRole
estimatr1.0.6lm_robust() and iv_robust() with HC/CR standard errors
lme42.0-1lmer() for random intercepts/slopes (mixed-effects models)

Version Notes

This skill targets plm 2.6-7 (R 4.5.3). Key features:

  • pdata.frame() for explicit panel structure declaration
  • plm() formula interface: y ~ x1 + x2 | instruments
  • Default vcov is classical (homoskedastic) -- always specify robust/clustered vcov explicitly
  • pgmm() supports both Arellano-Bond ("ab") and Blundell-Bond ("bb") transformations

How to Use This Skill

Reference File Structure

FilePurposeWhen to Read
quickstart.mdpdata.frame, plm() basics, model= argument, summary, coeftestStarting with plm or panel data in R
models.mdwithin, random, between, fd, Fama-MacBeth, comparisonChoosing or switching between panel estimators
diagnostics.mdphtest() Hausman, plmtest(), pcdtest(), panel unit rootTesting assumptions, model selection
iv.mdPanel IV via plm() instruments, Sargan/Hansen, pgmm()IV estimation within panel structure
robust.mdestimatr lm_robust/iv_robust, HC/CR SEs, plm vcov optionsRobust or clustered standard errors
mixed-effects.mdlme4 lmer/glmer, random intercepts/slopes, ICCMixed-effects / multilevel models
gotchas.mdpdata.frame requirements, FE vs RE, fixest vs plm routingDebugging or package selection

Reading Order

  1. New to plm? Start with quickstart.md then models.md
  2. FE vs RE decision? Read quickstart.md then diagnostics.md
  3. Need robust SEs? Read robust.md
  4. Panel IV or GMM? Read iv.md
  5. Mixed-effects models? Read mixed-effects.md
  6. Coming from linearmodels (Python)? Read quickstart.md then gotchas.md

The reference-file routing in this skill applies to advisory and brainstorming turns as much as implementation. Recommending an estimator, reviewing a panel analysis plan, or answering a question that touches a routed topic calls for reading the routed reference file just as much as writing code does — the reference files carry curated caveats and environment-specific constraints (e.g., the Fama-MacBeth index reversal) that this overview and general knowledge lack.

Related Skills

SkillRelationship
linearmodelsPython equivalent for panel data. plm covers the same estimators (FE, RE, between, FD, Fama-MacBeth) with R-native formula syntax. Key API difference: plm uses pdata.frame() for panel declaration; linearmodels uses pandas MultiIndex
fixestPreferred for high-dimensional FE, FE + IV, DiD, fast FE demeaning, publication tables. Use plm when fixest cannot do what you need (RE, between, Hausman test, Fama-MacBeth, panel diagnostics, dynamic GMM)
r-statsBase R regression without panel structure. Use r-stats for cross-sectional OLS, GLM, classical tests. plm extends the lm() formula interface for panel data
data-scientistMethodology guidance -- load for "why and when" behind model choices
tidyverseData preparation before estimation; plm accepts data.frames directly from dplyr pipelines
gtPublication-quality tables -- plm model output feeds into modelsummary/gt for formatted regression tables

Quick Decision Trees

"I need a panel model in R"

What panel estimation method?
+-- Fixed effects (within estimator)
|   +-- 1-2 way FE, no IV --> plm (model = "within") or fixest feols
|   +-- 3+ way FE --> fixest (plm max 2-way via twoways)
|   +-- FE + IV combined --> plm with instruments or fixest 3-part formula
|   +-- FE + DiD --> fixest (plm has no DiD)
+-- Random effects (GLS) --> plm (model = "random")
|   --> ./references/models.md
+-- FE vs RE comparison --> plm phtest() (Hausman test)
|   --> ./references/diagnostics.md
+-- Between estimator --> plm (model = "between")
|   --> ./references/models.md
+-- First difference --> plm (model = "fd")
|   --> ./references/models.md
+-- Pooled OLS (panel-aware) --> plm (model = "pooling")
|   --> ./references/models.md
+-- Fama-MacBeth --> pmg(model = "mg") with REVERSED index c("time","entity")
|   --> ./references/models.md
+-- Dynamic panel (Arellano-Bond/Blundell-Bond) --> plm pgmm()
|   --> ./references/iv.md

"I need robust standard errors"

What inference?
+-- Heteroskedasticity-robust (HC) --> ./references/robust.md
|   +-- For plm models: vcovHC() from plm
|   +-- For lm models: estimatr::lm_robust()
+-- Clustered SEs --> ./references/robust.md
|   +-- For plm models: vcovHC(type = "HC1", cluster = "group")
|   +-- For lm models: estimatr::lm_robust(clusters = group)
+-- Newey-West (panel HAC) --> ./references/robust.md
|   +-- vcovNW() from plm
+-- Driscoll-Kraay --> ./references/robust.md
|   +-- vcovSCC() from plm
+-- Bootstrap --> sandwich::vcovBS

"I need panel diagnostics"

What diagnostic?
+-- FE vs RE decision --> ./references/diagnostics.md
|   +-- Hausman test: phtest()
+-- Serial correlation --> ./references/diagnostics.md
|   +-- Breusch-Godfrey: pbgtest()
|   +-- Wooldridge AR(1) FE test: pwartest() / first-difference: pwfdtest()
+-- Unobserved individual/time effects (LM) --> ./references/diagnostics.md
|   +-- plmtest() (Breusch-Pagan, Honda, GHM -- NOT a serial correlation test)
+-- Cross-sectional dependence --> ./references/diagnostics.md
|   +-- Pesaran CD: pcdtest()
+-- Unit root (stationarity) --> ./references/diagnostics.md
|   +-- purtest() (Levin-Lin-Chu, Im-Pesaran-Shin, etc.)
|   +-- cipstest() (Pesaran CIPS)
+-- Poolability (F-test for FE) --> ./references/diagnostics.md
|   +-- pooltest()

"Something isn't working"

Having issues?
+-- Error about pdata.frame or index --> ./references/gotchas.md
+-- Hausman test gives warnings --> ./references/gotchas.md
+-- Results differ from fixest --> ./references/gotchas.md
+-- Unbalanced panel handling --> ./references/gotchas.md
+-- Want to compare with linearmodels (Python) --> ./references/gotchas.md
+-- plm vs fixest: which to use? --> ./references/gotchas.md

File-First Execution in Research Workflows

Important: In DAAF research pipelines, plm regressions are executed through script files, not interactively. This ensures auditability and reproducibility.

The pattern:

  1. Write regression code to scripts/stage8_analysis/{step}_{task-name}.R
  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. All regression scripts must follow the Inline Audit Trail (IAT) standard -- see agent_reference/INLINE_AUDIT_TRAIL.md. For regression code, document model specification choices (why this estimator, why this clustering level, what identifying assumptions) with # INTENT:, # REASONING:, and # ASSUMES: comments.

See:

  • agent_reference/WORKFLOW_PHASE4_ANALYSIS.md -- Stage 8 (Analysis & Visualization)
  • agent_reference/INLINE_AUDIT_TRAIL.md -- IAT documentation standard

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


Quick Reference

Essential Setup

library(plm)
library(estimatr)    # lm_robust, iv_robust
library(lme4)        # lmer, glmer
library(lmtest)      # coeftest with custom vcov
library(arrow)       # For parquet I/O (DAAF convention)

Data Setup (pdata.frame)

# Declare panel structure -- entity and time identifiers
pdf <- pdata.frame(df, index = c("entity_id", "year"))

# Verify
pdim(pdf)            # Panel dimensions
is.pbalanced(pdf)    # Check balance

Core Operations

OperationCode
Panel FE (within)plm(y ~ x1 + x2, data = pdf, model = "within")
Two-way FEplm(y ~ x1 + x2, data = pdf, model = "within", effect = "twoways")
Random effectsplm(y ~ x1 + x2, data = pdf, model = "random")
Betweenplm(y ~ x1 + x2, data = pdf, model = "between")
First differenceplm(y ~ x1 + x2, data = pdf, model = "fd")
Pooled OLSplm(y ~ x1 + x2, data = pdf, model = "pooling")
Panel IVplm(y ~ x1 + x2 | z1 + z2, data = pdf, model = "within")
Hausman testphtest(fe_fit, re_fit)
Robust SEscoeftest(fit, vcov = vcovHC(fit, type = "HC1"))
Clustered SEscoeftest(fit, vcov = vcovHC(fit, cluster = "group"))
Driscoll-Kraaycoeftest(fit, vcov = vcovSCC(fit))
Summarysummary(fit)
Dynamic GMMpgmm(y ~ lag(y, 1) + x1 | lag(y, 2:99), data = pdf)

Formula Syntax

# Standard panel formula
y ~ x1 + x2

# Panel IV: instruments after |
y ~ x1 + x_endog | z1 + z2 + x1

# Interactions
y ~ x1 * x2          # x1 + x2 + x1:x2

# Transformations
y ~ log(x1) + I(x2^2)

# Suppress intercept (for within estimator, intercept is absorbed anyway)
y ~ x1 + x2 - 1

Post-Estimation

fit <- plm(y ~ x1 + x2, data = pdf, model = "within")

summary(fit)                          # Full summary
coef(fit)                             # Coefficients
vcov(fit)                             # Variance-covariance matrix
fixef(fit)                            # Fixed effects estimates
pFtest(fit, plm(y ~ x1 + x2, data = pdf, model = "pooling"))  # F-test for FE
within_intercept(fit)                 # Overall intercept for within model
broom::tidy(fit, conf.int = TRUE)     # Tidy output
broom::glance(fit)                    # Model-level statistics

Topic Index

TopicReference File
pdata.frame setup./references/quickstart.md
plm() basics./references/quickstart.md
model= argument./references/quickstart.md
summary() output./references/quickstart.md
pdim(), is.pbalanced()./references/quickstart.md
Comparison to linearmodels (Python)./references/quickstart.md
Within (FE) estimator./references/models.md
Random effects (RE)./references/models.md
Between estimator./references/models.md
First difference (FD)./references/models.md
Pooled OLS./references/models.md
Fama-MacBeth (pmg)./references/models.md
Two-way effects./references/models.md
Model comparison./references/models.md
Weighted estimation./references/models.md
Hausman test (phtest)./references/diagnostics.md
Poolability F-test./references/diagnostics.md
Serial correlation tests./references/diagnostics.md
Cross-sectional dependence./references/diagnostics.md
Panel unit root tests./references/diagnostics.md
Panel IV (plm instruments)./references/iv.md
Sargan/Hansen test./references/iv.md
Dynamic GMM (pgmm)./references/iv.md
Arellano-Bond test./references/iv.md
vcovHC (heteroskedasticity-robust)./references/robust.md
Clustered SEs./references/robust.md
Newey-West (vcovNW)./references/robust.md
Driscoll-Kraay (vcovSCC)./references/robust.md
estimatr lm_robust./references/robust.md
estimatr iv_robust./references/robust.md
lme4 lmer./references/mixed-effects.md
lme4 glmer./references/mixed-effects.md
Random intercepts/slopes./references/mixed-effects.md
ICC (intraclass correlation)./references/mixed-effects.md
Conditional vs marginal effects./references/mixed-effects.md
pdata.frame requirements./references/gotchas.md
Unbalanced panels./references/gotchas.md
FE vs RE choice./references/gotchas.md
fixest vs plm routing./references/gotchas.md
linearmodels vs plm comparison./references/gotchas.md

Citation

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

Croissant, Y. & Millo, G. (2008). "Panel Data Econometrics in R: The plm Package." Journal of Statistical Software, 27(2), 1-43.

For estimatr:

Blair, G., Cooper, J., Coppock, A., Humphreys, M., & Sonnet, L. (2022). estimatr: Fast Estimators for Design-Based Inference. R package. https://CRAN.R-project.org/package=estimatr

For lme4:

Bates, D., Machler, M., Bolker, B., & Walker, S. (2015). "Fitting Linear Mixed-Effects Models Using lme4." Journal of Statistical Software, 67(1), 1-48.

Cite when: plm is used for panel estimation (FE, RE, between, FD), Hausman testing, or panel diagnostics. Cite estimatr when HC/CR SEs are the primary inference approach. Cite lme4 when mixed-effects models are estimated. Do not cite when: Only loaded 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.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Guide for creating DAAF agent definition files. Covers 12-section template, hook registration, skills-in-frontmatter, integration checklist. Use when adding or revising agents. For SKILL.md files, use skill-authoring instead.

中文简介正在准备中。当前显示原文说明。

DAAF-Contribution-Community/daaf2382026年10月17日 更新

Researcher-facing analytic triangulation ("creative verification") for stress-testing provisional findings and designing pre-specified challenges before an analysis runs. Turns a researcher's concern or focal claim into competing explanations, bounded discriminating tests, audited diagnostic artifacts, and evidence-calibrated interpretations in a hypothesis/evidence ledger — behavioral, output-level verification replacing line-by-line code review. Covers 12 challenge families organized by Shadish-Cook-Campbell validity type, named sensitivity statistics, pre-committed interpretation rules, anti-fishing guardrails, and interactive diagnostic-artifact QA. Use in Ad Hoc Collaboration to verify, challenge, or stress-test a result, or to design discriminating tests before an analysis runs. Not code-change verification (verify skill / code review) and not final adjudication or re-execution (data-verifier, Reproducibility Verification) — prospective, hypothesis-driven challenge design of analytic claims.

中文简介正在准备中。当前显示原文说明。

DAAF-Contribution-Community/daaf2382026年10月17日 更新

In-situ smoke testing that verifies a LIVE DAAF deployment functions end-to-end in whatever provider route it is configured for. Auto-detects the active install route (anthropic-subscription, openrouter, chatgpt-subscription shim, openai-api shim) and runs tiered probes: Tier 0 free preflight (route/env coherence, hooks, statuslines, shim health); Tier 1 one live round-trip (~cents); Tier 2 a six-probe functional battery (dispatch, coding, web, skill loading, isolation-strip, nested-dispatch deny); Tier D a zero-cost deterministic battery (bats, Pester, lint, safety-hook tests). Use after install, after a rebuild or update, after any provider/config change (new API key, model remap, shim change), for pre-release validation, or when asked "is my install working" or to "smoke test the deployment/config." Requires the DAAF_DEV=1 dev image; a contributor tool, not end-user-facing. NOT DAAFBench (model behavioral-adherence evaluation under benchmarks/) and NOT scripts/smoke_tests/ (R/Python library-package smoke tests).

中文简介正在准备中。当前显示原文说明。

DAAF-Contribution-Community/daaf2382026年10月17日 更新

Operational framework for the DAAF orchestrator. Defines engagement modes, confirmation protocol, subagent dispatch, context budget, and reference-loading. Loaded exclusively by the orchestrator — not for subagents or user questions.

中文简介正在准备中。当前显示原文说明。

DAAF-Contribution-Community/daaf2382026年10月17日 更新

Data science methodology and method-selection routing for quantitative research. Covers EDA, data validation, descriptive analysis, causal inference (IV, DiD, RD, synthetic control), clustering/PCA/UMAP, supervised ML, geospatial analysis, network analysis, and visualization design. Contains the canonical method-to-library routing tree, routed by execution language — Python: statsmodels (OLS/GLM/time series), pyfixest (FE/DiD), linearmodels (RE/GMM/SUR), svy (complex surveys), scikit-learn (clustering/prediction ML), geopandas (spatial), igraph (network analysis); R: r-stats (OLS/GLM/time series), fixest (FE/DiD), plm (panel/RE/IV), survey-r (complex surveys), tidymodels (ML), sf-terra (spatial), igraph-r (network analysis). Load the routed tool-specific skill before giving tool-specific advice or writing code — library skills encode environment constraints and curated caveats absent from general knowledge.

中文简介正在准备中。当前显示原文说明。

DAAF-Contribution-Community/daaf2382026年10月17日 更新

Interpretation guidance for Urban Institute Education Data Portal datasets. The Portal is a curation layer over federal data: lowercase variable names, integer-encoded categoricals, standardized missing codes (-1 missing, -2 not applicable, -3 suppressed). Covers year definitions, grade encoding (grade=-1 is Pre-K, not missing), suppression rates, ODC-By licensing, and cross-source join identifiers. Load before analyzing any Portal data. Routes to source-specific deep-dive skills for individual datasets.

中文简介正在准备中。当前显示原文说明。

DAAF-Contribution-Community/daaf2382026年10月17日 更新

查看 DAAF-Contribution-Community 的全部技能

报告此技能的问题