科研 Skill

结构方程与测量模型拟合

alterlab-sem-psychometrics

Fits CFA, SEM, EFA, and IRT models with semopy and factor_analyzer, reporting fit indices, omega reliability, and invariance. Use for latent-variable, factor-loading, or invariance requests.

发布者 Codar安全审核通过v0.1.0更新于 2026-11-18

SKILL.md

SEM & Psychometrics — Fit the Measurement Model, Judge It Honestly

Skill type: ANALYSIS MODULE. Fits CFA / SEM / EFA / IRT and tests measurement invariance. The discipline: report fit against real cutoffs, report reliability with omega (not alpha alone), and establish invariance before comparing groups. Whether the scale should be trusted at all is logically the upstream call of a scale-validity gate such as alterlab-ssci-measurement-gate (from the sibling AlterLab social-science suite); that skill is not part of this plugin and may not be installed in this session. If it is unavailable, state that this skill only fits and reports the measurement model and does not itself certify construct validity, then proceed.

Core Mission

A MEASUREMENT MODEL IS ONLY DONE WHEN FIT, RELIABILITY (OMEGA), AND INVARIANCE ARE ALL REPORTED.

When to Use This Skill

  • "Run a confirmatory factor analysis on my scale."
  • "Fit a structural equation model / a latent-variable path model."
  • "Do an exploratory factor analysis — how many factors?"
  • "Fit an IRT model / get item difficulty and discrimination."
  • "Test measurement invariance across countries before I compare latent means."

Does NOT Trigger

Note: the "Route to" skills below (alterlab-ssci-measurement-gate, alterlab-survey-design, alterlab-statistical-analysis, alterlab-causal-inference) belong to the companion AlterLab social-science skill suite, not to this plugin. They may not be installed. If a named skill is not available in this session, say so, answer the in-scope part of the request yourself using ordinary statistical/methodological knowledge, and note that a dedicated skill for that sub-task would normally be used if present.

The request is really about…Route toWhy not this skill
Whether the scale is valid/trustworthy at all (reliability≠validity discipline)alterlab-ssci-measurement-gateGate decision, upstream of fitting.
Writing / wording the questionnaire itemsalterlab-survey-designInstrument construction.
Plain regression / descriptive statsalterlab-statistical-analysisNo latent structure.
A causal treatment-effect estimatealterlab-causal-inferenceDifferent problem.

Hand-off rule. When the request matches a row above and the named skill is installed in this session, open your answer by naming that skill by its exact id and saying in one line why it fits better, then stop applying this skill's procedure. If the named skill is not installed, do not pretend to call it — note its absence and either do the in-scope part of this skill's procedure or answer the out-of-scope part directly with general-purpose reasoning.

Running the code. In Claude Code, run analysis code in a throwaway environment instead of installing packages: uv run --python ">=3.11" --with "semopy>=2.3" --with pandas python cfa.py (add --with girth for IRT). This needs no install approval and leaves the user's environment untouched. If uv or the network is unavailable, say so and hand over the script; never report results you did not compute.

Verified calls (pinned)

CFA / SEM — semopy (v2.3):

from semopy import Model, calc_stats
desc = """
# measurement model
Trust =~ q1 + q2 + q3
Efficacy =~ q4 + q5 + q6
# structural path
Trust ~ Efficacy
"""
m = Model(desc); m.fit(data)          # obj='MLW' default; 'FIML' for missing
est = m.inspect(std_est=True)          # standardized loadings for omega
stats = calc_stats(m)                  # DataFrame: chi2, CFI, TLI, RMSEA, AIC, BIC ...

Note: calc_stats returns CFI/TLI/RMSEA but not SRMR — report the indices it gives and compute SRMR separately if required.

EFA — factor_analyzer (v0.5):

from factor_analyzer import FactorAnalyzer, calculate_kmo, calculate_bartlett_sphericity
kmo_all, kmo_model = calculate_kmo(X)          # sampling adequacy > .60
chi2, p = calculate_bartlett_sphericity(X)      # sphericity
fa = FactorAnalyzer(n_factors=3, rotation="promax"); fa.fit(X)
fa.loadings_; fa.get_factor_variance()          # (variance, proportional, cumulative)

ConfirmatoryFactorAnalyzer + ModelSpecificationParser.parse_model_specification_from_dict give a CFA alternative when semopy is unavailable.

factor_analyzer is unmaintained (0.5.1, Feb 2024) and breaks on current stacks: FactorAnalyzer.fit raises TypeError (force_all_finite) on scikit-learn ≥ 1.8, and ModelSpecificationParser fails under pandas 3 ("assignment destination is read-only"). calculate_kmo / calculate_bartlett_sphericity still work. Run it in an isolated env (uv run --with factor_analyzer --with "scikit-learn<1.8" --with "pandas<3" python efa.py), or do the EFA in R psych (fa(), KMO(), cortest.bartlett(), fa.parallel()) — say which you used.

Reliability — omega (not alpha alone): pingouin gives cronbach_alpha but no omega, and semopy does not return omega. Compute McDonald's omega from the standardized loadings with scripts/omega.py (ω = (Σλ)² / [(Σλ)² + Σ(1−λ²)]). Report alpha + omega together.

IRT: girth (twopl_mml, rasch_mml) for dichotomous 2PL/Rasch; py-irt for Bayesian/ scalable. For polytomous/multidimensional IRT with full fit statistics, R mirt is the practical standard — say so rather than forcing a thin Python path.

Fit cutoffs (Hu & Bentler 1999, as guidelines not gates)

CFI/TLI ≥ .95 · RMSEA ≤ .06 · SRMR ≤ .08. Use a two-index combination; do not chase a single number. Report the χ² and df alongside (χ² is sample-size sensitive, so not decisive).

Measurement invariance (before any group comparison)

Multi-group CFA sequence — configural → metric → scalar → strict — judged by ΔCFI ≤ .01 / ΔRMSEA ≤ .015, not only Δχ². Scalar invariance is the prerequisite for comparing latent means; without it a group "difference" may be a measurement artifact. Fit each nested model and compare.

Output Template

MODEL:        <CFA/SEM/EFA/IRT + the semopy/factor_analyzer spec>
FIT:          CFI= TLI= RMSEA= (chi2/df=)   vs Hu & Bentler cutoffs
RELIABILITY:  omega= (alpha= )              per factor, from standardized loadings
VALIDITY:     <AVE, HTMT if convergent/discriminant claimed>
INVARIANCE:   <configural|metric|scalar|strict established; ΔCFI reported>

References

  • references/fit_and_invariance.md — omega math, fit-index nuance, the invariance sequence, IRT notes.
  • scripts/omega.py — stdlib McDonald's omega from standardized loadings. Needs ≥ 2 standardized loadings, each finite and within [−1, 1] (|λ| > 1 = Heywood case, rejected); reverse-code negatively keyed items first — ω is the reliability of the sum as scored.

Prerequisites

  • A Python environment that can run uv run (or an equivalent isolated-env tool) for semopy, factor_analyzer, pingouin, and girth; if uv/network access is unavailable, say so and hand over the script rather than reporting unverified numbers.
  • The hand-off targets named above (alterlab-ssci-measurement-gate, alterlab-survey-design, alterlab-statistical-analysis, alterlab-causal-inference) are part of a different plugin (AlterLab social-science suite) and are not guaranteed to be installed alongside this skill.

Limitations

  • factor_analyzer is unmaintained and known to break under newer scikit-learn/pandas versions (see the EFA section above) — confirm the installed package behavior before trusting EFA output.
  • This skill fits and reports a measurement model; it does not itself decide whether a scale's validity evidence is adequate for publication — that judgment needs the measurement-gate skill (if installed) or explicit researcher sign-off.

Part of the AlterLab Academic Skills suite.

示例提示词

我想用semopy做验证性因子分析并检验多组测量不变性。

所属插件

心理学面向心理学研究:心理测量与量表修订、结构方程、多层模型、反应时与眼动分析、PsychoPy 实验、功效分析与心理学元分析。

安装该插件会一并获得同领域的 13 个 Skill / MCP。

同插件的其他科研 Skill

常见问题

结构方程与测量模型拟合能做什么?

Fits CFA, SEM, EFA, and IRT models with semopy and factor_analyzer, reporting fit indices, omega reliability, and invariance. Use for latent-variable, factor-loading, or invariance requests。

如何安装结构方程与测量模型拟合?

在科搭 Codar 左侧点击「插件」,在「组件」页签搜索「结构方程与测量模型拟合」并安装;在 Claude Code 或 Codex 中可以发送本页的安装提示词,或下载 Skill 包后解压到 ~/.claude/skills 或 ~/.codex/skills。

结构方程与测量模型拟合需要哪些权限?

该科研 Skill没有声明额外权限;执行具体操作时仍由 Agent 按自身规则请求确认。

结构方程与测量模型拟合经过官方审核吗?

是。SpeedSkills 收录的每个版本都会记录内容摘要与权限声明,当前版本已通过安全审核。