Table vals holds numeric observations value for two groups, group_name = 'A' or 'B'. Run Mood's median test on them and return one row with chi_square, rounded to 4 decimals, and df = 1.
-
Compute the grand median of all values without using
PERCENTILE_CONT
.
-
Classify each row as above (
value
strictly greater than the grand median) or not-above.
-
Build the 2x2 contingency table of
group_name
by above/not-above.
-
Compute the Pearson chi-square statistic
SUM((observed - expected)^2 / expected)
over the four cells, with
expected = row_total * col_total / grand_total
.
Input Table(s)
Table vals (no key; values can repeat):
| Column | Type | Meaning |
|---|
group_name | VARCHAR(5) | Group: 'A' or 'B' |
value | NUMERIC(8,2) | The observation |
Output Contract
Exactly one row with these columns, in this order:
| Column | Definition | Rounding |
|---|
chi_square | Pearson chi-square statistic of the 2x2 table | 4 decimals |
df | Degrees of freedom, always 1 | integer |
The grand median is the middle value of all rows sorted by value, or the average of the two middle values when the row count is even. It is taken over both groups together. With O the observed count of a cell and E its expected count:
χ2=4 cells∑E(O−E)2,E=grand totalrow total×column total
No continuity correction is applied. The result is a single row, so no ordering is needed.
Example
Input vals:
| group_name | value |
|---|
| B | 55.91 |
| B | 46.51 |
| B | 84.31 |
| A | 43.63 |
| A | 27.47 |
| B | 64.68 |
| B | 63.97 |
| A | 52.21 |
| A | 51.95 |
| A | 54.48 |
| A | 27.63 |
| B | 57.92 |
| A | 48.27 |
| B | 76.69 |
Expected output:
Sorted, the 7th and 8th of the 14 values are 52.21 and 54.48, so the grand median is 53.345. Group A has one value above it (54.48) and six not above. Group B has six above and one not above (46.51). Every row and column total is 7, so each expected count is 3.5, and χ2=4⋅2.52/3.5≈7.1429.
Constraints and Clarifications
-
group_name
is always
'A'
or
'B'
, and both groups have rows.
-
value
is never NULL.
-
Values can repeat. A value equal to the grand median counts as not-above.
-
Every expected count is positive: both groups are present and at least one value is strictly above the grand median.