chi_square_test
Test independence of two categorical variables using chi-square, with automatic numeric binning, Fisher exact correction for small samples, and Cramér's V effect size.
Instructions
chi_square_test —— 统计推断组 · 卡方独立性检验(工具 8,核心实现)。
docstring = agent 使用说明书,与 statlab_mcp/docs/design/04_inference_batch2.md 同步维护。
参数: file_path (str): 本地数据文件(csv/tsv/xlsx/json) col_a / col_b (str): 两列类别变量(均须存在;数值列自动等宽分箱 ≤8 箱并注明)
流程(确定性): 1. 分类化:数值列 pd.cut 等宽分箱(箱数=min(8, max(2, 唯一值数)))并注明; 类别唯一值 =1 -> error;>50 -> error(防列联表爆炸) 2. pd.crosstab 列联表;scipy.stats.chi2_contingency(含期望频数表) 3. >20% 单元格期望频数 <5:2x2 -> scipy.stats.fisher_exact(statistic=OR、df=null); 非 2x2 -> 中文报错引导合并类别 4. 效应量 Cramér's V = sqrt(chi2/(n*(min(rows,cols)-1)))(chi2 来自 chi2_contingency, fisher 路径同样给出并注明基于卡方近似) 5. 结论固定模板;summary 注明"关联≠因果"
示例: chi_square_test("samples/clean.csv", col_a="category", col_b="category") inline 数据: 本工具支持可选 inline_data 参数(v1.2.0 起):与 file_path 二选一, 支持 records 数组或 {"header": [...], "rows": [[...], ...]} 对象两种形态; 规模上限/类型域/data_source 来源标注见 statlab_mcp/docs/SPEC.md 第 12 节。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| col_a | No | ||
| col_b | No | ||
| file_path | No | ||
| inline_data | No |