Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Contingency Tables from Raw Data

Recommended resources

Welcome back! In our previous lesson, we established the groundwork for categorical data analysis by learning how to classify and summarize single categorical variables.

This lesson takes the next logical step: exploring the relationship between two categorical variables. Our primary tool for this is the contingency table. You will learn how to build one from raw data and, more importantly, how to interpret the story it tells about the data. Mastering this is essential, as contingency tables are the input for many of the statistical tests we will cover in the upcoming modules, such as the chi-square test.

1. What is a Contingency Table?

A contingency table, also known as a cross-tabulation or a two-way table, is a matrix that displays the frequency distribution of two or more categorical variables. It provides a simple and powerful way to organize data and examine the potential relationship between the variables.

To get a formal definition, let's start with a short reading.

Contingency Table: Definition, Examples & Interpreting

The following article by Jim Frost provides a clear definition and purpose of a contingency table.

Please read the first section, 'What is a Contingency Table?'. This will introduce you to the core concept and its alternative names.


2. Constructing a Contingency Table

How do we get from a raw dataset to a contingency table? The process involves systematically counting the occurrences of each unique combination of categories from the two variables.

Imagine you have survey data where each row is a person's response. To construct a table, you would:

  1. List the categories of one variable as the rows.
  2. List the categories of the second variable as the columns.
  3. Go through your raw data row by row, and for each observation, place a tally mark in the cell that corresponds to its categories.
  4. Finally, sum the tally marks in each cell to get the frequency counts.

The image below provides an excellent visual summary of this process.

Constructing a Contingency Table from Raw Survey Data
This diagram illustrates how individual responses from a raw dataset are tallied to create the frequency counts within a contingency table.

The resulting table has a specific structure. The numbers inside the main grid are called cell frequencies or joint frequencies. The sums at the end of each row and the bottom of each column are called marginal totals (or marginal frequencies). The total number of observations is the grand total, found in the bottom-right corner.

This next reading provides a clear example of a complete table and its components.

Contingency Table: Definition, Examples & Interpreting

Let's return to the article from Statistics By Jim to see a labeled example of a contingency table.

Read the section 'Example Contingency Table'. Pay attention to how the cell values, row totals, column totals, and grand total are presented and what each represents.


3. Interpreting a Contingency Table

Once constructed, the table allows us to analyze the data from several perspectives by looking at different types of distributions or probabilities.

  • Joint Distribution: The frequencies or probabilities of each specific combination of categories (the cells).
  • Marginal Distribution: The frequency or probability distribution of a single variable, found in the margins (totals).
  • Conditional Distribution: The distribution of one variable, given a specific level of the other variable. This is where we start looking for relationships.

The following video is excellent at explaining these concepts by converting a table of counts into a table of probabilities.

Basic probability: Joint, marginal and conditional probability | Independence

This video from zedstatistics clearly demonstrates how to derive and interpret joint and marginal probabilities from a basic contingency table.

Watch the segment from the beginning until 04:00. Focus on how the presenter transforms the raw counts into a joint probability distribution and then calculates the marginal probability distributions.

Building on the video, the next reading explains how to find these distributions within the table's structure.

Contingency Table: Definition, Examples & Interpreting

This section of the Statistics by Jim article provides a non-probabilistic take on the same concepts, focusing on how to read conditional and marginal distributions directly from the counts.

Read the section 'Marginal and Conditional Distributions in Contingency Tables'. This will solidify your understanding of how to 'slice' the table to answer different questions.


4. The Importance of Percentages for Comparison

Raw counts can be misleading when the groups you are comparing have different sizes. For instance, if you find that 50 men and 25 women prefer a certain product, you can't conclude that men prefer it more if your sample contained 200 men and only 50 women.

To make a fair comparison, we use percentages (or proportions). There are two main types:

  • Row Percentages: Calculated by dividing each cell's count by its row total. The percentages in each row will sum to 100%.
  • Column Percentages: Calculated by dividing each cell's count by its column total. The percentages in each column will sum to 100%.
Contingency Table with Relative Frequencies
An example of a contingency table that includes both row and column percentages. Notice how row percentages sum to 100% across rows, and column percentages sum to 100% down columns.

Which percentage should you use?

A common convention is to treat one variable as the independent variable (the potential cause) and the other as the dependent variable (the potential effect).

Rule of Thumb: Calculate percentages in the direction of the independent variable.

If the independent variable forms the columns, calculate column percentages. If it forms the rows, calculate row percentages. This allows you to easily compare how the distribution of the dependent variable changes across the categories of the independent variable.

Let's watch a video that puts this principle into practice.

2.7 Example contingency table | Basic Statistics | Correlation and Regression | UvA

The following video from Research Methods and Statistics walks through a complete example, from setting up the table to interpreting column percentages to find a relationship.

Watch the entire video (about 3 minutes). Pay close attention to how the presenter sets up the table with the independent variable in the columns and then uses column percentages to draw a conclusion.

To reinforce this crucial point, let's read one final section from our main article.

Contingency Table: Definition, Examples & Interpreting

This reading demonstrates the calculation and interpretation of both row and column percentages, solidifying the idea of choosing the correct one for your research question.

Read the sections 'Finding Relationships in a Contingency Table' and 'Row and Column Percentages in Contingency Tables'. Focus on the logic of why percentages are necessary and how to interpret them.

A Note on Practical Application

As a data science student, you'll be creating these tables using software. In R, for example, the table() function creates a basic frequency table from two categorical variables. You can then use prop.table() to convert these counts into proportions. The prop.table(mytable, margin=1) command would give you row proportions, while prop.table(mytable, margin=2) would give you column proportions. This direct mapping from concept to code is what makes your theoretical understanding functional.


Conclusion

In this lesson, we moved from single variables to examining the relationship between two categorical variables. The contingency table is our foundational tool for this task.

Key Takeaways:

  • A contingency table (or cross-tabulation) organizes the counts of two categorical variables, with one variable's categories as rows and the other's as columns.
  • Construction is a process of counting the occurrences for each cell defined by the intersection of a row and column category.
  • Interpretation involves analyzing three types of distributions:
    • Joint: The individual cell counts or probabilities.
    • Marginal: The row and column totals, representing each variable's distribution independently.
    • Conditional: The distribution within a single row or column.
  • For meaningful comparisons, especially with unequal group sizes, always use row or column percentages. The choice depends on which variable you consider independent.

Preview of the Next Lesson:

The validity of the statistical tests we perform on these tables can depend on how the data was collected. In the next lesson, we will explore different sampling schemes used to construct contingency tables. Understanding this will be crucial for selecting the correct analytical methods later in the course.