The c
^{2} test is used to determine whether an association (or relationship) between 2 categorical variables in a sample is likely to reflect a real association between these 2 variables in the population.
Note: In the case of 2 variables being compared, the test can also be interpreted as determining if there is a difference between the two variables.
The sample data is used to calculate a single number (or test statistic), the size of which reflects the probability (pvalue) that the observed association between the 2 variables has occurred by chance, ie due to sampling error.
Worked example
A group of students were classified in terms of personality (introvert or extrovert) and in terms of colour preference (red, yellow, green or blue) with the purpose of seeing whether there is an association (relationship) between personality and colour preference. Data was collected from 400 students and presented in the 2 (rows) x 4 (cols) contingency table below:
(Observed counts) 
Colours


Red

Yellow

Green

Blue

Totals

Introvert personality 
20

6

30

44

100

Extrovert personality 
180

34

50

36

300

Totals 
200

40

80

80

400

Suitable null and alternative hypotheses might be:
 H_{0}: Colour preference is not associated with personality, and
 H_{1}: Colour preference is associated with personality
To perform a chisquared test, the number of students expected in each cell of the table if the null hypothesis is true, is calculated.
Calculated
The following calculations are for demonstration and, hopefully, to aid understanding– a computer package will do the appropriate calculations.
The expected numbers (under the null hypothesis) in each cell are equal to
Thus for the introvert/red cell the expected number is
To calculate the chisquared (c2) statistic the value of
needs to be calculated for each cell in the table. For the introvert/red cell this is
The chisquare statistic is calculated to be total of these values
(Expected counts) 
Colours


Red

Yellow

Green

Blue

Totals

Introvert personality 
50

10

20

20

100

Extrovert personality 
150

30

60

60

300

Totals 
200

40

80

80

400

From these expected and the observed values the chisquared teststatistic is computed, and the resulting pvalue is examined.
Computer Output
Chisquared test in Minitab
Data should be entered in 2 columns, then select
Stat > Tables > Cross Tabulation… > ChiSquare Test
Alternatively, if the values in the contingency table have already been calculated, select
Stat>Tables>ChiSquare Test
ChiSquare Test: red, yellow, green, blue
(1 refers to Introverts, 2 refers to Extroverts)
Note: Interpret 0.000 as p < 0.001
Chisquared test in SPSS
Data should be entered in 2 columns, then select
Analyze >Descriptive Statistics>Crosstabs
SPSS can only be used for raw data
Some choices need to be made from the Statistics and Cells buttons in the dialogue box, to get the chisquared test results, and to get the expected frequencies, as shown in the output below. Initially, only the 'Pearson ChiSquare' line needs to be investigated.
Note: The pvalue is printed as .000
This should be interpreted as p< 0.001, and not be taken as exactly 0
Results
The chisquared test statistic is 71.20 with an associated p < 0.001.
Note: .000 should not be interpreted as exactly zero, as in the computer printout.
The null hypothesis is rejected, since p < 0.001, and a conclusion is made that colour preference is associated with personality. Examining the pattern of numbers it is noted that more introverts prefer blue than expected and less preferred red. The extroverts tend to favour red more than blue.
A chart illustrates the pattern of responses well.
Bar chart to illustrate the relationship between personality type and colour preference
Note: If more than one of the expected frequencies is less than 5 (in small tables), or if more than 20% are less than 5 in large tables, cells should be pooled to reduced the number of expected frequencies that are less than 5.
Note: Yates correction and Fisher's exact tests for 2x2 contingency tables are also used.