寻求R中Person-fit统计分析的Python等效可导入模块
Python中做Person-fit统计分析的实用工具(适配Jupyter Notebook)
先明确:你说的Person-fit是针对单个被试的反应模式是否符合IRT模型假设的分析,和整体模型的goodness of fit完全不是一个概念,别搞混了。下面是几个能在Jupyter里直接用的Python工具:
1. irtm 库
- 专门做IRT相关的Person-fit分析,支持Z3、Z4、对数似然Z值这些常用统计量,和R里的常见指标对应得上。
- 安装:在Jupyter的终端或者代码块里跑
!pip install irtm - 简单用例:
from irtm import person_fit # 你得先准备好被试的反应矩阵,还有IRT模型的项目参数(难度、区分度之类的) fit_results = person_fit(your_response_matrix, your_item_params, model='2PL') # 结果里会包含每个被试的Person-fit统计量,直接打印或者转成DataFrame查看都行 print(fit_results)
2. pyirt 库
- 既能拟合IRT模型,又内置了Person-fit分析功能,能算出infit、outfit这些R里
mirt包常用的指标。 - 安装:
!pip install pyirt - 快速上手片段:
from pyirt import irt # 先拟合IRT模型(这里用EAP估计为例) irt_model = irt.EAPModel(your_response_matrix, n_items=your_item_count) irt_model.train() # 直接获取infit和outfit统计量 infit_scores, outfit_scores = irt_model.person_fit()
3. 自定义实现(如果现成库不够用)
- 要是上述库满足不了你的需求,也可以自己基于IRT的似然公式写Person-fit统计量。核心就是对比被试实际反应的似然和模型期望的差异,转成标准化分数。比如:
import numpy as np def compute_person_fit(response_matrix, item_diffs, item_discs, theta_estimates): fit_scores = [] for resp, theta in zip(response_matrix, theta_estimates): # 按2PL模型计算每个项目的期望反应概率 probs = 1 / (1 + np.exp(-item_discs * (theta - item_diffs))) # 计算实际反应的对数似然 log_like = np.sum(resp * np.log(probs) + (1 - resp) * np.log(1 - probs)) # 这里可以参考IRT文献里的公式,把对数似然转换成Z-score这类拟合指标 # 省略标准化步骤,你可以根据需要补充 fit_scores.append(your_z_score_calculation(log_like)) return fit_scores
注意:大部分Python工具都是针对IRT框架下的Person-fit分析,如果你的数据是用经典测验理论(CTT)处理的,可能需要调整统计量的计算逻辑,但IRT是当前Person-fit分析的主流场景。
内容的提问来源于stack exchange,提问作者Chuks
相关产品推荐
相关产品推荐

