CHISQN_q
Updated 2024-02-13 20:07:40.787000
Syntax
SELECT [westclintech].[wct].[CHISQN_q] (
<@Actual_range_RangeQuery, nvarchar(4000),>)
Description
Use the scalar function CHISQN_q to calculate the chi-square (χ2) statistic for normalized tables. This function calculates the chi-square statistic by finding the difference between each observed and theoretical frequency for each possible outcome, squaring them, dividing each by the theoretical frequency, and taking the sum of the results. A second important part of determining the test statistic is to define the degrees of freedom of the test: this is essentially the number of squares errors involving the observed frequencies adjusted for the effect of using some of those observations to define the expected frequencies.
CHISQN_q automatically calculates the expected results.
The value of the chi-square statistic is:
\chi^2=\sum_{i=1}^r\sum_{j=1}^c\frac{\left(O_{i,j} - E_{i,j}\right)^2}{E_{i,j}}
Where
{
"columns": [
{
"field": "column 1",
"maxWidth": 100
},
{
"field": "column 2",
"maxWidth": 225
}
],
"rows": [
{
"column 1": "r",
"column 2": "is the number of columns"
},
{
"column 1": "c",
"column 2": "is the number of columns"
},
{
"column 1": "O",
"column 2": "is the Observed result"
},
{
"column 1": "E",
"column 2": "is the Expected result"
} ]
}
Arguments
@Actual_range_RangeQuery
the select statement, as text, used to determine the values to be used in the CHISQN_q calculation.
Return Type
float
Remarks
CHISQN_q is designed for normalized tables. For de-normalized tables, use the CHISQ_q function.
CHISQN_q automatically calculates the expected values. If you want to enter the expected values use the CHISQN2_q function.
No GROUP BY is required for this function even though it produces aggregated results.
Examples
In this hypothetical situation, we want to determine if there is an association between population density and the preference for a sport from among baseball, football, and basketball. We will use the CHISQN_q function to calculate the chi-squared statistic.
CREATE TABLE #chin
(
[Sport] [varchar](20) NOT NULL,
[Locale] [varchar](20) NOT NULL,
[Result] [float] NOT NULL
);
INSERT INTO #CHIN
VALUES
('Basketball', 'Rural', 28);
INSERT INTO #CHIN
VALUES
('Basketball', 'Suburban', 35);
INSERT INTO #CHIN
VALUES
('Basketball', 'Urban', 54);
INSERT INTO #CHIN
VALUES
('Baseball', 'Rural', 60);
INSERT INTO #CHIN
VALUES
('Baseball', 'Suburban', 43);
INSERT INTO #CHIN
VALUES
('Baseball', 'Urban', 35);
INSERT INTO #CHIN
VALUES
('Football', 'Rural', 52);
INSERT INTO #CHIN
VALUES
('Football', 'Suburban', 48);
INSERT INTO #CHIN
VALUES
('Football', 'Urban', 28);
SELECT wct.CHISQN_q('SELECT Sport, Locale, Result from #chin');
This produces the following result.
{"columns":[{"field":"column 1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"column 1":"22.451703426585"}]}
We could have also invoked the function by passing the data directly to it.
SELECT wct.CHISQN_q('SELECT ''Basketball'', ''Rural'', 28 UNION ALL
SELECT ''Basketball'', ''Suburban'', 35 UNION ALL
SELECT ''Basketball'', ''Urban'', 54 UNION ALL
SELECT ''Baseball'', ''Rural'', 60 UNION ALL
SELECT ''Baseball'', ''Suburban'', 43 UNION ALL
SELECT ''Baseball'', ''Urban'', 35 UNION ALL
SELECT ''Football'', ''Rural'', 52 UNION ALL
SELECT ''Football'', ''Suburban'', 48 UNION ALL
SELECT ''Football'', ''Urban'', 28');
This produces the following result
{"columns":[{"field":"column 1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"column 1":"22.451703426585"}]}