科研 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.
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 to | Why not this skill |
|---|---|---|
| Whether the scale is valid/trustworthy at all (reliability≠validity discipline) | alterlab-ssci-measurement-gate | Gate decision, upstream of fitting. |
| Writing / wording the questionnaire items | alterlab-survey-design | Instrument construction. |
| Plain regression / descriptive stats | alterlab-statistical-analysis | No latent structure. |
| A causal treatment-effect estimate | alterlab-causal-inference | Different 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) forsemopy,factor_analyzer,pingouin, andgirth; ifuv/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_analyzeris 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做验证性因子分析并检验多组测量不变性。
所属插件
安装该插件会一并获得同领域的 13 个 Skill / MCP。
同插件的其他科研 Skill
- 临床量表评分解读Scores PHQ-9, GAD-7, PCL-5, BDI-II, and SWLS, computing severity, prorated scores, and the Reliable Change Index. Use when batch-scoring clinical scales or testing pre-post change.
- 实验设计与顺序平衡Guides within- vs between-subjects design, Latin-square and ABBA counterbalancing, confound control, and preregistration before data collection. Use when planning an experiment’s design.
- 眼动数据分析Processes raw eye-tracking data: IVT fixation detection, AOI dwell time, scanpath similarity, heatmaps, pupillometry baseline correction. Use for gaze or pupil data from eye trackers.
- 评分者间信度分析Computes inter-rater/inter-coder reliability with Cohen’s/weighted/Fleiss’ kappa, Krippendorff’s alpha, ICC, and Gwet’s AC1. Use when raters coded the same cases and agreement must be reported.
- 心理学元分析Pools effect sizes across studies with Cohen’s d, Hedges’ g, random-effects pooling, forest/funnel plots, and publication-bias tests. Use when synthesizing results from multiple studies.
- 多层线性混合效应模型Fits multilevel/mixed-effects models with pymer4 and lme4: ICC, random slopes, likelihood-ratio comparison, within-person centering for ESM data. Use for nested or repeated-measures designs.
- 心理学研究功效分析Computes power, sample size, or minimum detectable effect for t-tests, ANOVA, correlation, regression, and simulation-based designs including mediation. Use when planning a study’s sample size.
- 心理测量学分析Runs CTT item stats, Cronbach’s alpha, EFA, CFA, 2PL IRT, measurement invariance, and Mantel-Haenszel DIF detection in Python. Use when validating a scale’s reliability, structure, or item fairness.
- PsychoPy行为实验设计Builds PsychoPy experiments: monitor calibration, stimulus presentation, response timing, BIDS events.tsv output, Pavlovia deployment checklist. Use when implementing a stimulus-response task.
- 反应时数据分析Cleans RT data and fits ex-Gaussian distributions, EZ-diffusion parameters, and Bayesian drift-diffusion models with PyMC. Use for RT outliers, distribution shape, or decision-model parameters.
- 单被试实验设计分析Analyzes single-case ABAB, multiple-baseline, and alternating-treatment designs with visual analysis, Tau-U, PND, and randomization tests. Use for studies with few participants observed across phases.
- 问卷量表翻译审查Reviews OB, management, and psychology survey scales for translation equivalence and adaptation validity, producing a Chinese dashboard report. Use for pre-launch questionnaire quality checks.
常见问题
结构方程与测量模型拟合能做什么?
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 收录的每个版本都会记录内容摘要与权限声明,当前版本已通过安全审核。