The observations table has one row per subject with row_cat ('A' or 'B') and col_cat ('X' or 'Y'). Build the 2x2 contingency table and compute each cell's expected count as
E=grand totalrow total×column total
Return one row with the Pearson statistic
χ2=cells∑E(O−E)2
rounded to 4 decimals as chi_square, and (rows - 1) * (cols - 1) as df, where O is the observed count in a cell.
Input Table(s)
observations
| Column | Type | Meaning |
|---|
| row_cat | VARCHAR(5) | Row attribute of the subject, 'A' or 'B' |
| col_cat | VARCHAR(5) | Column attribute of the subject, 'X' or 'Y' |
There is no key column. Each row is one subject, and the same (row_cat, col_cat) pair can appear many times.
Output Contract
-
Exactly one row with the columns
chi_square
and
df
, in that order.
-
chi_square
is rounded to 4 decimals. Round only the final sum.
-
df
is an integer, the number of row levels minus 1 times the number of column levels minus 1.
-
The result has a single row, so no ordering is needed.
Example
Input observations:
| row_cat | col_cat |
|---|
| B | Y |
| B | Y |
| B | X |
| A | Y |
| B | Y |
| A | Y |
| A | X |
| A | Y |
| A | Y |
| A | Y |
| A | X |
| B | X |
| B | Y |
| A | X |
| B | X |
Expected output:
The cell counts are A,X = 3, A,Y = 5, B,X = 3 and B,Y = 4, so the row totals are 8 and 7, the column totals are 6 and 9, and the grand total is 15. The expected counts are 3.2, 4.8, 2.8 and 4.2. Each cell is off by 0.2, and the four contributions 0.04/3.2, 0.04/4.8, 0.04/2.8 and 0.04/4.2 sum to about 0.044643.
Constraints and Clarifications
-
row_cat
is always
'A'
or
'B'
,
col_cat
is always
'X'
or
'Y'
, and neither is NULL.
-
Both row levels and both column levels appear at least once, so every expected count is positive. A single cell may still have zero subjects.
-
Repeated rows are separate subjects and each one counts.
-
Report the uncorrected Pearson statistic, with no continuity correction.