Login     Register

        Contact Us     Search

XLeratorDB/statistics Documentation

SQL Server chi-squared statistic for normalized data


CHISQN_q

Updated: 6 August 2010

Use 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:


 
Where
                r              is the number of rows
                c              is the number of columns
                O             is the Observed result
                E              is the Expected result
 
Syntax
SELECT [wctStatistics].[wct].[CHISQN_q] (
   <@Actual_range_RangeQuery, nvarchar(4000),>)
Arguments
@Actual_range_RangeQuery
the select statement, as text, used to determine the values to be used in the CHISQN_q calculation.
Return Types
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.
·         For simpler queries, consider using the CHISQN 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 CHISQ 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
----------------------
22.451703426585
 
(1 row(s) affected)
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
 
----------------------
22.451703426585
 

(1 row(s) affected)



Copyright 2008-2024 Westclintech LLC         Privacy Policy        Terms of Service