{"id":3482,"date":"2026-03-01T20:47:43","date_gmt":"2026-03-01T19:47:43","guid":{"rendered":"https:\/\/www.gironi.it\/blog\/contingency-tables-and-conditional-probability\/"},"modified":"2026-03-02T09:29:55","modified_gmt":"2026-03-02T08:29:55","slug":"contingency-tables-and-conditional-probability","status":"publish","type":"post","link":"https:\/\/www.gironi.it\/blog\/en\/contingency-tables-and-conditional-probability\/","title":{"rendered":"Contingency Tables and Conditional Probability"},"content":{"rendered":"<p><strong>Contingency tables<\/strong> are used to evaluate the <strong>interaction between two categorical variables<\/strong> (qualitative). They are also called two-way tables or cross-tabulations.<\/p>\n<p>Searching for relationships between two categorical variables is a very common goal for researchers. Think, for example, of the classic question that marketers ask: who is more likely to buy certain product categories, young or old people, men or women&#8230;<\/p>\n<p><!--more--><\/p>\n<div style=\"border: 1px solid #ccc;padding: 1.2em 1.5em;margin: 1.5em 0;border-radius: 6px\">\n<h3 style=\"margin-top: 0\">What We&#8217;ll Cover<\/h3>\n<ul>\n<li><a href=\"#two-way-tables\">Two-Way Tables and Marginal Distributions<\/a><\/li>\n<li><a href=\"#conditional-probability\">Conditional Probability<\/a><\/li>\n<li><a href=\"#dependence-independence\">Dependence and Independence<\/a><\/li>\n<li><a href=\"#further-reading\">Further Reading<\/a><\/li>\n<\/ul>\n<\/div>\n<hr \/>\n<h2 id=\"two-way-tables\">Two-Way Tables and Marginal Distributions<\/h2>\n<p>A <strong>two-way table<\/strong> is a table with rows and columns that helps organize data from categorical variables:<\/p>\n<ul>\n<li><strong>Rows<\/strong> represent the possible categories for one qualitative variable, for example males and females.<\/li>\n<li><strong>Columns<\/strong> represent the possible categories for a second qualitative variable, for example whether someone likes pizza or not&#8230;<\/li>\n<\/ul>\n<p>A <strong>marginal distribution<\/strong> shows how many total responses there are for each category of the variable. The marginal distribution of a variable can be determined by looking at the &#8220;Total&#8221; column (or row).<\/p>\n<p>Let&#8217;s look at an example.<\/p>\n<p><em>Note: I couldn&#8217;t think of anything particularly clever, so I created a table (with fictitious data, of course) of rare silliness, imagining that the two categorical variables concern education level and favorite sci-fi series&#8230;<\/em><\/p>\n<p>We build the table in R:<\/p>\n<pre><code class=\"language-r\">scifi_fans <- matrix(c(44, 38, 26, 53, 35, 30, 58, 22, 29), ncol = 3, byrow = TRUE)\nrownames(scifi_fans) <- c(\"degree\", \"diploma\", \"lower education\")\ncolnames(scifi_fans) <- c(\"star trek\", \"star wars\", \"doctor who\")\nscifi_fans <- as.table(scifi_fans)\nscifi_fans<\/code><\/pre>\n<p>and we get something like this:<\/p>\n<pre>                 star trek   star wars   doctor who\ndegree               44          38          26\ndiploma              53          35          30\nlower education      58          22          29<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.gironi.it\/blog\/wp-content\/uploads\/2023\/03\/26e3bb37-2a8f-4f6c-9e5d-fddf6a1bb60f-1024x1024.jpeg\" alt=\"Fantasy image for the sci-fi dataset used to discuss contingency tables and conditional probability\" loading=\"lazy\"\/><\/p>\n<p>Remember? A <strong>marginal distribution<\/strong> shows how many total responses there are for each category of the variable (at the margins, precisely, where the Total column or row is...).<\/p>\n<p>We can compute row totals in R with:<\/p>\n<pre><code class=\"language-r\">margin.table(scifi_fans, 1)<\/code><\/pre>\n<p>and column totals with:<\/p>\n<pre><code class=\"language-r\">margin.table(scifi_fans, 2)<\/code><\/pre>\n<p>We can also find the \"grand total\" with:<\/p>\n<pre><code class=\"language-r\">margin.table(scifi_fans)<\/code><\/pre>\n<p>Here is the table with totals:<\/p>\n<pre>              star trek   star wars   doctor who   <strong>TOTAL<\/strong>\ndegree            44          38          26        <strong>108<\/strong>\ndiploma           53          35          30        <strong>118<\/strong>\nlower ed.         58          22          29        <strong>109<\/strong>\n<strong>TOTAL            155          95          85        335<\/strong><\/pre>\n<p>So the marginal totals by education level are 108 for degree holders, 118 for diploma holders, 109 for lower education.<\/p>\n<p>Likewise, the marginal totals by sci-fi series type are 155 for Star Trek, 95 for Star Wars, 85 for Doctor Who.<\/p>\n<p>The grand total must be the same in both directions, in this case 335.<\/p>\n<p>We could also have displayed a complete table with totals using just a few lines of R code:<\/p>\n<pre><code class=\"language-r\">scifi_fans <- matrix(c(44, 38, 26, 53, 35, 30, 58, 22, 29), ncol = 3, byrow = TRUE)\n\nrow_names <- c(\"degree\", \"diploma\", \"lower education\")\ncol_names <- c(\"star trek\", \"star wars\", \"doctor who\")\ndimnames(scifi_fans) <- list(row_names, col_names)\n\n# Compute column totals using apply\ncol_totals <- apply(scifi_fans, 2, sum)\n# Add row with column totals using rbind\nscifi_fans2 <- rbind(scifi_fans, col_totals)\n# Compute row totals\nrow_totals <- apply(scifi_fans2, 1, sum)\n# Add column with row totals\ncont_table <- cbind(scifi_fans2, row_totals)\n\n# Print the table\ncont_table<\/code><\/pre>\n<p>We can then ask ourselves (and answer): what percentage of degree holders has a soft spot for Doctor Who?<br \/>Elementary, Watson (oh wait, that was a different series...):<\/p>\n<p><strong>26\/108 = 0.24 = 24% of degree holders prefer Doctor Who<\/strong><\/p>\n<p>And how many Star Wars fans hold a diploma?<\/p>\n<p><strong>35\/95 = 0.37 = 37% of Star Wars fans are diploma holders<\/strong><\/p>\n<p>In R, we can directly obtain row proportions with the function:<\/p>\n<pre><code class=\"language-r\">prop.table(scifi_fans, 1)<\/code><\/pre>\n<p>and the result will be:<\/p>\n<pre>                 star trek    star wars    doctor who\ndegree           0.4074074    0.3518519    0.2407407\ndiploma          0.4491525    0.2966102    0.2542373\nlower ed.        0.5321101    0.2018349    0.2660550<\/pre>\n<p>(as we can see, the row totals add up to 1, or 100%)<\/p>\n<p>or column proportions with:<\/p>\n<pre><code class=\"language-r\">prop.table(scifi_fans, 2)<\/code><\/pre>\n<p>and the result will be:<\/p>\n<pre>                 star trek    star wars    doctor who\ndegree           0.2838710    0.4000000    0.3058824\ndiploma          0.3419355    0.3684211    0.3529412\nlower ed.        0.3741935    0.2315789    0.3411765<\/pre>\n<p>(as we can see, the column totals add up to 1, or 100%)<\/p>\n<p>As always, there is more than one way to get the result. We can also install the \"gmodels\" package and use the CrossTable function (we'll leave it to R's built-in help to show all the command options...):<\/p>\n<pre><code class=\"language-r\">install.packages(\"gmodels\")\nlibrary(gmodels)\nscifi_fans <- matrix(c(44, 38, 26, 53, 35, 30, 58, 22, 29), ncol = 3, byrow = TRUE)\nrownames(scifi_fans) <- c(\"degree\", \"diploma\", \"lower education\")\ncolnames(scifi_fans) <- c(\"star trek\", \"star wars\", \"doctor who\")\n\nCrossTable(scifi_fans, prop.r = \"false\", prop.c = \"false\", prop.t = \"false\", prop.chisq = \"false\")<\/code><\/pre>\n<p>So what is all this good for? The answer is: for example, to compute <strong>conditional probability<\/strong>.<\/p>\n<hr \/>\n<h2 id=\"conditional-probability\">Conditional Probability<\/h2>\n<p>Before we see what it is and why it is an extremely useful concept in everyday life, we need a few preliminary definitions about <a href=\"https:\/\/www.gironi.it\/blog\/en\/first-steps-into-the-world-of-probability\/\">probability<\/a>.<\/p>\n<p>An event is something that occurs with one or more possible outcomes.<br \/>An experiment is the process of measuring or making an observation.<\/p>\n<p><strong>Key definition: <em><a href=\"https:\/\/www.gironi.it\/blog\/en\/first-steps-into-the-world-of-probability\/\">the probability of an event is the ratio of the number of favorable cases to the number of possible cases<\/a><\/em><\/strong><\/p>\n\\( P(A) = \\frac {\\text{number of favorable cases}}{\\text{number of possible cases}}\\\\ \\)\n<p>Let us also recall that:<\/p>\n<ul>\n<li>The probability that two events both occur can never be greater than the probability that each event occurs separately.<\/li>\n<li>If two possible events, A and B, are independent, then the probability that both occur is the product of their individual probabilities.<\/li>\n<li>If an event can have a certain number of different and distinct possible outcomes (A, B, C, etc.), then the probability that A or B occurs equals the sum of the individual probabilities of A and B, and the sum of the probabilities of all possible outcomes (A, B, C, etc.) equals 1, i.e. 100%.<\/li>\n<\/ul>\n<p>The <strong>conditional probability<\/strong> of an event A with respect to an event B is the probability that A occurs, given that B has occurred.<\/p>\n<p>The formula is:<\/p>\n\\( P(A|B) = \\frac {P(A \\text{ and } B)}{P(B)}\\\\ \\)\n<p>If a probability is based on <strong>one variable<\/strong> it is a <strong>marginal probability<\/strong>; if on <strong>two or more variables<\/strong> it is called a <strong>joint probability<\/strong>.<\/p>\n<ul>\n<li>The <strong>probability of an event<\/strong> P(A) is: \\( \\frac {\\text{marginal probability of A}}{\\text{Total}}\\\\ \\)<\/li>\n<li>The <strong>joint probability of two events<\/strong> is: \\( \\frac {P(A \\text{ and } B)}{\\text{Total}}\\\\ \\)<\/li>\n<li>The <strong>conditional probability<\/strong> of outcome A given the occurrence of condition B is: \\( \\frac {P(A \\text{ and } B)}{P(B)}\\\\ \\)<\/li>\n<\/ul>\n<p>In other words:<\/p>\n<p>A <strong>joint probability<\/strong> is the probability that someone selected from the entire group has two particular characteristics at the same time. That is, both characteristics occur jointly. We find a joint probability by taking the value of the cell at the intersection of A and B and dividing by the grand total.<\/p>\n<p>To find a <strong>conditional probability<\/strong>, we take the value of the cell at the intersection of A and B and divide it by the marginal total of B, i.e. the variable expressing the event that has occurred.<\/p>\n<hr \/>\n<p>It's time for a second example. We take the data from:<br \/><em>Ellis GJ and Stone LH. 1979. Marijuana Use in College: An Evaluation of a Modeling Explanation. Youth and Society 10:323-334.<\/em><\/p>\n<p>The study asks whether a college student is more likely to smoke marijuana if their parents had used drugs in the past. Here is the table:<\/p>\n<pre>                    parents    parents     <strong>Total<\/strong>\n                      use      no use\nstudent uses          125        94         <strong>219<\/strong>\nstudent does not use   85       141         <strong>226<\/strong>\n<strong>Total                 210       235         445<\/strong><\/pre>\n<p>Let's apply our knowledge to answer these questions:<\/p>\n<ol>\n<li><strong><em>If the parents used soft drugs in the past, what is the probability that their child does the same in college?<\/em><\/strong><\/li>\n<\/ol>\n<p>This is a case of conditional probability.<br \/>We recall \\( P(A|B) = \\frac {P(A \\text{ and } B)}{P(B)}\\\\ \\), therefore<\/p>\n<p>P(<em>student uses given that parents used<\/em>) = 125 \/ 210 = 0.59 = 59%<\/p>\n<p>2. <strong><em>A student is selected at random and does not use marijuana. What is the probability that their parents used it?<\/em><\/strong><\/p>\n<p>Here again we face a question that asks for a conditional probability. Therefore:<\/p>\n<p>P(<em>parents used given that student does not use<\/em>) = 85 \/ 226 = 0.376 = 37.6%<\/p>\n<p>3. <strong><em>What is the probability of selecting a student who does not use marijuana and whose parents used it in the past?<\/em><\/strong><\/p>\n<p>In this case we need to find a joint probability, so:<\/p>\n<p>\\( \\frac {P(A \\text{ and } B)}{\\text{Total}}\\\\ \\), therefore \\( \\frac {85}{445} = 0.19\\\\ \\).<\/p>\n<p>The probability is approximately 19%.<\/p>\n<h2 id=\"dependence-independence\">Dependence and Independence<\/h2>\n<p>If the outcomes of A and B influence each other, we say that <strong>the two variables are in a relationship of dependence<\/strong>.<br \/>Conversely, we say the two variables are independent.<\/p>\n<p>More rigorously: we can state that event B is independent of event A if:<\/p>\n<p>P(B|A) = P(B)<\/p>\n<p>or<\/p>\n<p>P(A|B) = P(A)<\/p>\n<p>If this is not the case, the events are dependent on each other.<\/p>\n<p>Therefore:<\/p>\n<ul>\n<li>P(A and B) = P(A) P(B) if and only if A and B are independent events.<\/li>\n<li>P(A | B) = P(A) and P(B | A) = P(B) if and only if A and B are independent events.<\/li>\n<\/ul>\n<h3>Let's examine the independence of categorical variables...<\/h3>\n<p>Let's explain this better with an example.<\/p>\n<p>Let A be the event that people enjoy cycling.<br \/>B expresses whether they enjoy roast lamb. (Makes perfect sense, right?)<\/p>\n<p>We build our contingency table:<\/p>\n<pre>                  Likes cycling   Doesn't like cycling   <strong>Total<\/strong>\nLikes roast lamb       95                36               <strong>131<\/strong>\nNo roast lamb          15                19                <strong>34<\/strong>\n---------------------------------------------------------------\n<strong>Total                 110                55               165<\/strong><\/pre>\n<p>Let's remember what it means for two events to be independent. It means this:<br \/>P(A | B) = P(A)<\/p>\n<p>But in our case we see that<br \/>P(A) = 66.7%<br \/>because 110\/165 = 0.67<\/p>\n<p>P(A | B) = 72.5%<br \/>because 95\/131 = 0.725<\/p>\n<p>We recall that \\( P(A|B) = \\frac {P(A \\text{ and } B)}{P(B)}\\\\ \\), therefore \\( \\frac {95}{131} = 0.725\\\\ \\).<\/p>\n<p>From the result it is clear that \\( P(A) \\neq P(A|B) \\) -- the two events are NOT independent (therefore they are dependent).<\/p>\n<p>After all, everyone knows that there is a clear dependence between loving cycling and loving roast lamb!<\/p>\n<hr \/>\n<h3>You might also like<\/h3>\n<ul>\n<li><a href=\"https:\/\/www.gironi.it\/blog\/en\/first-steps-into-the-world-of-probability\/\">First Steps into the World of Probability<\/a><\/li>\n<li><a href=\"https:\/\/www.gironi.it\/blog\/en\/the-chi-square-test\/\">The Chi-Square Test<\/a><\/li>\n<li><a href=\"https:\/\/www.gironi.it\/blog\/en\/bayesian-statistics-how-to-learn-from-data-one-step-at-a-time\/\">Bayesian Statistics<\/a><\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"further-reading\">Further Reading<\/h3>\n<p>For a comprehensive treatment of contingency tables, conditional probability, and the full machinery of categorical data analysis, <a href=\"https:\/\/www.amazon.it\/dp\/8891910651?tag=consulenzeinf-21\" rel=\"nofollow sponsored noopener\" target=\"_blank\"><em>Statistica<\/em><\/a> by Newbold, Carlson and Thorne provides a rigorous yet accessible framework for applying these concepts in real-world settings.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contingency tables are used to evaluate the interaction between two categorical variables (qualitative). They are also called two-way tables or cross-tabulations. Searching for relationships between two categorical variables is a very common goal for researchers. Think, for example, of the classic question that marketers ask: who is more likely to buy certain product categories, young &hellip; <a href=\"https:\/\/www.gironi.it\/blog\/en\/contingency-tables-and-conditional-probability\/\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;Contingency Tables and Conditional Probability&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[161],"tags":[],"class_list":["post-3482","post","type-post","status-publish","format-standard","hentry","category-statistics"],"lang":"en","translations":{"en":3482,"it":709},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"autore-articoli","author_link":"https:\/\/www.gironi.it\/blog\/author\/autore-articoli\/"},"uagb_comment_info":0,"uagb_excerpt":"Contingency tables are used to evaluate the interaction between two categorical variables (qualitative). They are also called two-way tables or cross-tabulations. Searching for relationships between two categorical variables is a very common goal for researchers. Think, for example, of the classic question that marketers ask: who is more likely to buy certain product categories, young&hellip;","_links":{"self":[{"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/posts\/3482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/comments?post=3482"}],"version-history":[{"count":1,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/posts\/3482\/revisions"}],"predecessor-version":[{"id":3490,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/posts\/3482\/revisions\/3490"}],"wp:attachment":[{"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/media?parent=3482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/categories?post=3482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/tags?post=3482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}