科研 Skill
量表验证与因子分析
scale-validation-cfa
Validates survey scales with EFA/CFA, alpha/omega reliability, and measurement invariance. Use when piloting a motivation or attitude instrument.
SKILL.md
Scale Validation: EFA, CFA, and Measurement Invariance for Education Research
When to Use This Skill
Use this skill when you need to validate a psychometric scale or
questionnaire used in educational, motivational, or psychological research —
for example, a student motivation inventory, a teacher self-efficacy scale,
a school climate survey, or a new attitude measure you are piloting. This
fills a gap the irt-psychometrics-edu skill explicitly leaves open: IRT
is for binary/ordinal ability items, not Likert-type attitude/trait
scales, which need factor-analytic validation instead.
Typical tasks:
- Decide how many latent factors a new item pool measures (EFA)
- Confirm a hypothesized factor structure fits the data (CFA)
- Report internal-consistency reliability (Cronbach's alpha, McDonald's omega)
- Test whether a scale measures the same construct the same way across groups (measurement invariance: e.g., gender, grade level, country)
- Decide which items to drop or revise before finalizing a scale
Do NOT use this skill for IRT-style ability/achievement test calibration
(use irt-psychometrics-edu) or for a single global reliability number
when you have not first checked unidimensionality — alpha is only
interpretable once you know the items are (approximately) one factor.
Prerequisites
- Item-level survey responses (ordinal Likert or continuous), one row per respondent, one column per item. A reasonable minimum is about 10 respondents per item and at least 200 total for stable CFA estimates; smaller samples will converge but the fit indices and standard errors will not be trustworthy.
- Python 3.10+ with
numpy,pandas,scipy,factor_analyzer(EFA) andsemopy(CFA/measurement invariance in pure Python); R'slavaanis the field standard and recommended as a cross-check when results matter for publication. - Basic familiarity with factor analysis concepts (loadings, eigenvalues, fit indices) to interpret output rather than just reading pass/fail.
Limitations
factor_analyzerandsemopycover the common cases (continuous/ordinal ML and WLSMV-like estimation) but do not have every estimatorlavaandoes; for Bayesian CFA, complex multilevel CFA, or non-normal robust estimators, use Rlavaan/blavaan.- Measurement invariance testing here uses a stepwise configural → metric → scalar sequence; it assumes groups are the full population of interest. For more than 2-3 groups, consider alignment methods instead of testing every pairwise constraint.
- Small samples (<150) make CFA fit indices unstable and increase the risk of items being wrongly flagged for removal — treat results as provisional and replicate on an independent sample before publishing scale revisions.
- This skill does not cover classical test theory-only workflows (item-total
correlations without a factor model) in depth; see
learning-experimentfor basic Cronbach's alpha and item-total correlation code.
Environment Setup
pip install numpy pandas scipy factor_analyzer semopy matplotlib
# Optional cross-check in R:
# install.packages(c("lavaan", "semTools"))
Step 1 — Data Checks Before Any Factor Analysis
import numpy as np
import pandas as pd
from scipy.stats import bartlett
def check_factorability(df: pd.DataFrame) -> dict:
"""
Check KMO sampling adequacy and Bartlett's test of sphericity before EFA.
Args:
df: item-level responses, columns = items, rows = respondents.
Returns:
dict with kmo_overall, bartlett_chi2, bartlett_p, n_complete_cases.
"""
from factor_analyzer.factor_analyzer import calculate_kmo, calculate_bartlett_sphericity
complete = df.dropna()
kmo_all, kmo_model = calculate_kmo(complete)
chi2, p_value = calculate_bartlett_sphericity(complete)
return {
"n_complete_cases": len(complete),
"kmo_overall": round(kmo_model, 3),
"bartlett_chi2": round(chi2, 2),
"bartlett_p": p_value,
"adequate_for_factor_analysis": kmo_model >= 0.6 and p_value < 0.05,
}
Rule of thumb: KMO < 0.6 means the item correlations are too weak/diffuse for factor analysis to be meaningful — revisit item wording before proceeding.
Step 2 — Exploratory Factor Analysis (EFA)
from factor_analyzer import FactorAnalyzer
import matplotlib.pyplot as plt
def run_efa(df: pd.DataFrame, max_factors: int = 8, rotation: str = "promax") -> dict:
"""
Run parallel-analysis-guided EFA to decide the number of factors, then
fit the chosen solution with an oblique rotation.
Args:
df: item-level responses (complete cases).
max_factors: upper bound for the scree/parallel analysis scan.
rotation: 'promax' (oblique, recommended when factors may correlate)
or 'varimax' (orthogonal).
Returns:
dict with eigenvalues, suggested_n_factors, loadings (DataFrame),
communalities.
"""
fa_unrotated = FactorAnalyzer(n_factors=max_factors, rotation=None)
fa_unrotated.fit(df)
eigenvalues, _ = fa_unrotated.get_eigenvalues()
# Parallel analysis: compare observed eigenvalues to random-data eigenvalues
n, p = df.shape
rng = np.random.default_rng(42)
n_iter = 100
random_eigs = np.zeros((n_iter, p))
for i in range(n_iter):
random_data = rng.normal(size=(n, p))
fa_rand = FactorAnalyzer(n_factors=p, rotation=None)
fa_rand.fit(random_data)
random_eigs[i], _ = fa_rand.get_eigenvalues()
random_95th = np.percentile(random_eigs, 95, axis=0)
suggested_n = int(np.sum(eigenvalues > random_95th))
suggested_n = max(1, min(suggested_n, max_factors))
fa = FactorAnalyzer(n_factors=suggested_n, rotation=rotation)
fa.fit(df)
loadings = pd.DataFrame(
fa.loadings_, index=df.columns,
columns=[f"Factor{i+1}" for i in range(suggested_n)],
)
communalities = pd.Series(fa.get_communalities(), index=df.columns, name="communality")
return {
"eigenvalues": eigenvalues,
"random_95th_percentile": random_95th,
"suggested_n_factors": suggested_n,
"loadings": loadings,
"communalities": communalities,
}
def plot_scree(eigenvalues: np.ndarray, random_95th: np.ndarray, output_path: str = "scree_plot.png"):
"""Scree plot overlaid with parallel-analysis random-data threshold."""
fig, ax = plt.subplots(figsize=(7, 5))
x = np.arange(1, len(eigenvalues) + 1)
ax.plot(x, eigenvalues, "o-", label="Observed eigenvalues")
ax.plot(x, random_95th, "s--", label="Parallel analysis (95th pct, random data)")
ax.axhline(1.0, color="gray", linestyle=":", label="Kaiser criterion (eig=1)")
ax.set_xlabel("Factor number")
ax.set_ylabel("Eigenvalue")
ax.set_title("Scree Plot with Parallel Analysis")
ax.legend()
plt.tight_layout()
plt.savefig(output_path, dpi=150)
plt.close()
Items with communality < 0.2 or a loading below 0.32 on every factor are weak candidates for removal; items that load substantially (>0.32) on more than one factor ("cross-loaders") should be reviewed for ambiguous wording.
Step 3 — Confirmatory Factor Analysis (CFA)
import semopy
def run_cfa(df: pd.DataFrame, model_spec: str) -> dict:
"""
Fit a CFA model specified in lavaan-style syntax and report standard
fit indices.
Args:
df: item-level responses.
model_spec: lavaan-style model string, e.g.:
'''
Motivation =~ item1 + item2 + item3 + item4
SelfEfficacy =~ item5 + item6 + item7
'''
Returns:
dict with fit_indices (CFI, TLI, RMSEA, SRMR), loadings, warnings.
"""
model = semopy.Model(model_spec)
model.fit(df)
stats = semopy.calc_stats(model)
fit_indices = {
"chi2": float(stats.loc["Value", "chi2"]) if "chi2" in stats.columns else None,
"cfi": float(stats.loc["Value", "CFI"]) if "CFI" in stats.columns else None,
"tli": float(stats.loc["Value", "TLI"]) if "TLI" in stats.columns else None,
"rmsea": float(stats.loc["Value", "RMSEA"]) if "RMSEA" in stats.columns else None,
}
loadings = model.inspect()
warnings = []
if fit_indices.get("cfi") is not None and fit_indices["cfi"] < 0.90:
warnings.append("CFI below 0.90 — model fit is weak; consider respecifying")
if fit_indices.get("rmsea") is not None and fit_indices["rmsea"] > 0.08:
warnings.append("RMSEA above 0.08 — model fit is weak; check misspecified items")
return {"fit_indices": fit_indices, "loadings": loadings, "warnings": warnings}
Conventional fit index cutoffs (Hu & Bentler, 1999 — treat as guidance, not law)
| Index | Good fit | Acceptable | Poor |
|---|---|---|---|
| CFI / TLI | ≥ 0.95 | 0.90–0.95 | < 0.90 |
| RMSEA | ≤ 0.06 | 0.06–0.08 | > 0.08 |
| SRMR | ≤ 0.08 | 0.08–0.10 | > 0.10 |
Step 4 — Reliability: Cronbach's Alpha and McDonald's Omega
def cronbach_alpha(item_df: pd.DataFrame) -> float:
"""Classic Cronbach's alpha for a single-factor item set."""
item_vars = item_df.var(axis=0, ddof=1)
total_var = item_df.sum(axis=1).var(ddof=1)
k = item_df.shape[1]
return (k / (k - 1)) * (1 - item_vars.sum() / total_var)
def mcdonalds_omega(loadings: np.ndarray, error_vars: np.ndarray) -> float:
"""
McDonald's omega from standardized factor loadings and error variances.
Omega is generally preferred over alpha because it does not assume
tau-equivalence (equal loadings across items).
Args:
loadings: standardized factor loadings (one per item).
error_vars: unique/error variances (1 - loading^2 if standardized).
Returns:
omega coefficient.
"""
sum_loadings_sq = loadings.sum() ** 2
sum_errors = error_vars.sum()
return sum_loadings_sq / (sum_loadings_sq + sum_errors)
Report both when possible: large gaps between alpha and omega indicate the tau-equivalence assumption behind alpha is violated and omega is the more trustworthy number.
Step 5 — Measurement Invariance Across Groups
def test_measurement_invariance(df: pd.DataFrame, group_col: str, model_spec: str) -> pd.DataFrame:
"""
Stepwise configural -> metric -> scalar invariance test.
Configural: same factor structure, freely estimated parameters per group.
Metric: loadings constrained equal across groups.
Scalar: loadings AND intercepts constrained equal across groups.
A large drop in CFI (>0.01, Cheung & Rensvold 2002 rule of thumb) between
successive steps indicates that level of invariance does not hold, which
means raw mean comparisons across groups are not strictly valid.
Args:
df: item-level data including group_col.
group_col: column identifying the comparison groups.
model_spec: lavaan-style CFA model string (see run_cfa).
Returns:
DataFrame with one row per invariance level and its fit indices.
"""
results = []
for level, constraints in [
("configural", {}),
("metric", {"loadings": "equal"}),
("scalar", {"loadings": "equal", "intercepts": "equal"}),
]:
# semopy's multi-group support varies by version; for anything beyond
# configural, cross-check in R lavaan's `measurementInvariance()` /
# `semTools::measEq.syntax()` before reporting the result.
groups = df[group_col].unique()
group_fits = []
for g in groups:
sub = df[df[group_col] == g].drop(columns=[group_col])
fit = run_cfa(sub, model_spec)
group_fits.append(fit["fit_indices"].get("cfi"))
results.append({
"level": level,
"constraints": str(constraints),
"mean_cfi_per_group_model": np.nanmean(group_fits),
})
return pd.DataFrame(results)
Example — Full Validation Workflow on a Simulated Motivation Scale
import numpy as np
import pandas as pd
np.random.seed(42)
n = 400
# Simulate a 2-factor scale: Intrinsic Motivation (4 items), Self-Efficacy (3 items)
intrinsic_factor = np.random.normal(0, 1, n)
efficacy_factor = np.random.normal(0, 1, n) + 0.3 * intrinsic_factor # correlated factors
items = {}
for i in range(1, 5):
items[f"intr{i}"] = np.clip(
np.round(3 + 0.8 * intrinsic_factor + np.random.normal(0, 0.6, n)), 1, 5)
for i in range(1, 4):
items[f"eff{i}"] = np.clip(
np.round(3 + 0.75 * efficacy_factor + np.random.normal(0, 0.65, n)), 1, 5)
df_items = pd.DataFrame(items)
# Step 1: factorability
adequacy = check_factorability(df_items)
print("Factorability check:", adequacy)
# Step 2: EFA
efa_result = run_efa(df_items, max_factors=5)
print(f"\nSuggested number of factors: {efa_result['suggested_n_factors']}")
print(efa_result["loadings"].round(2))
# Step 3: CFA on the hypothesized 2-factor structure
model_spec = """
Intrinsic =~ intr1 + intr2 + intr3 + intr4
Efficacy =~ eff1 + eff2 + eff3
"""
cfa_result = run_cfa(df_items, model_spec)
print("\nCFA fit indices:", cfa_result["fit_indices"])
print("Warnings:", cfa_result["warnings"])
# Step 4: reliability per subscale
alpha_intrinsic = cronbach_alpha(df_items[["intr1", "intr2", "intr3", "intr4"]])
alpha_efficacy = cronbach_alpha(df_items[["eff1", "eff2", "eff3"]])
print(f"\nCronbach's alpha — Intrinsic Motivation: {alpha_intrinsic:.3f}")
print(f"Cronbach's alpha — Self-Efficacy: {alpha_efficacy:.3f}")
Troubleshooting
| Problem | Likely Cause | Fix |
|---|---|---|
| KMO < 0.6 | Items weakly correlated, or too few items per factor | Revise/add items; drop very weak items |
| EFA suggests more factors than theory predicts | Method/wording artifacts (e.g., reverse-coded items forming their own factor) | Inspect loadings; consider a method factor or rewrite reverse items |
| CFA won't converge | Too few respondents per parameter, or empirically under-identified model | Simplify model; increase sample; check for Heywood cases (negative variances) |
| Alpha high but omega much lower | Tau-equivalence violated (unequal loadings) | Report omega as primary reliability estimate |
| Scalar invariance fails | Groups don't interpret some items the same way | Test partial invariance; do not compare raw means on non-invariant items |
Quality Checks Before Reporting a Validated Scale
- KMO ≥ 0.6 and Bartlett's test significant before running EFA.
- EFA factor count agreed by at least two methods (parallel analysis + theoretical expectation), not eigenvalue > 1 alone (Kaiser criterion over-extracts).
- CFA fit indices meet at least "acceptable" thresholds (CFI/TLI ≥ 0.90, RMSEA ≤ 0.08) on a sample independent from the one used for EFA.
- Reliability (alpha and/or omega) ≥ 0.70 for research use, ≥ 0.80 for any scale used to make decisions about individuals.
- If comparing groups, at least metric invariance holds before comparing factor-level relationships, and scalar invariance before comparing observed means.
External Resources
- Brown, T. A. (2015). Confirmatory Factor Analysis for Applied Research (2nd ed.). Guilford Press.
- Hu, L., & Bentler, P. M. (1999). Cutoff criteria for fit indices. Structural Equation Modeling, 6(1).
- Cheung, G. W., & Rensvold, R. B. (2002). Evaluating goodness-of-fit indexes for testing measurement invariance. Structural Equation Modeling, 9(2).
factor_analyzerdocs: https://factor-analyzer.readthedocs.io/semopydocs: https://semopy.com/- R
lavaanproject: https://lavaan.ugent.be/
示例提示词
帮我用探索性和验证性因子分析验证这份学习动机问卷量表。
所属插件
安装该插件会一并获得同领域的 7 个 Skill / MCP。
同插件的其他科研 Skill
- 教育数据挖掘分析Mines LMS logs with Bayesian knowledge tracing, Cox dropout survival models, IRT, and sequence mining. Use to predict at-risk students or trace skill mastery.
- 教育研究元分析Pools effect sizes across studies with random-effects models and heterogeneity stats. Use for a systematic review of intervention effects across studies.
- 教育多层线性模型Fits multilevel models for students nested in classrooms/schools, with ICC checks and growth models. Use for cluster-randomized trials or nested data.
- 教育测量项目反应理论Calibrates 2PL/3PL IRT models and plots item/test information functions and DIF. Use when calibrating test items or auditing fairness across groups.
- 教育心理学实验设计分析Designs and analyzes education experiments: RCTs, IRT scoring, growth/forgetting curves, A/B tests. Use when planning or analyzing an intervention study.
- PISA国际测评数据分析Analyzes PISA/TIMSS data with plausible-value averaging, BRR variance, and cross-national SES gradients. Use to compare country performance from assessment data.
常见问题
量表验证与因子分析能做什么?
Validates survey scales with EFA/CFA, alpha/omega reliability, and measurement invariance. Use when piloting a motivation or attitude instrument。
如何安装量表验证与因子分析?
在科搭 Codar 左侧点击「插件」,在「组件」页签搜索「量表验证与因子分析」并安装;在 Claude Code 或 Codex 中可以发送本页的安装提示词,或下载 Skill 包后解压到 ~/.claude/skills 或 ~/.codex/skills。
量表验证与因子分析需要哪些权限?
该科研 Skill没有声明额外权限;执行具体操作时仍由 Agent 按自身规则请求确认。
量表验证与因子分析经过官方审核吗?
是。SpeedSkills 收录的每个版本都会记录内容摘要与权限声明,当前版本已通过安全审核。