{"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-07-16T16:05:50","modified_gmt":"2026-07-16T15:05:50","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":"\n<p class=\"wp-block-paragraph\">Have you ever noticed how mobile users behave differently from desktop users? It&#8217;s not just a feeling \u2014 it\u2019s a <strong>relationship between two categorical variables<\/strong>. The variable &#8220;device&#8221; (mobile or desktop) and the variable &#8220;converted&#8221; (yes or no) \u2014 when we cross them in a table, we can see if there\u2019s a systematic link.<\/p>\n\n\n<p class=\"wp-block-paragraph\"><strong>Contingency tables<\/strong> (also called two-way tables or cross-tabulations) are the basic tool for this. They allow us to evaluate the <strong>interaction between two categorical variables<\/strong> and to quantify <strong>conditional probability<\/strong> \u2014 how likely an event is &#8220;given that&#8221; another has occurred.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The question about mobile vs desktop differences, for example, translates into a conditional probability: is <strong>P(conversion | device = mobile)<\/strong> different from <strong>P(conversion | device = desktop)<\/strong>? If yes, there\u2019s a relationship. If not, the two variables are independent.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s see how this works, starting with a deliberately light-hearted example.<\/p>\n\n\n<!--more-->\n\n\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-toc-en-contingenza      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tWhat We\u2019ll Cover\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#two-way-tables-and-marginal-distributions\" class=\"uagb-toc-link__trigger\">Two-Way Tables and Marginal Distributions<\/a><li class=\"uagb-toc__list\"><a href=\"#conditional-probability\" class=\"uagb-toc-link__trigger\">Conditional Probability<\/a><li class=\"uagb-toc__list\"><a href=\"#dependence-and-independence\" class=\"uagb-toc-link__trigger\">Dependence and Independence<\/a><ul class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#lets-examine-the-independence-of-categorical-variables\" class=\"uagb-toc-link__trigger\">Let\u2019s examine the independence of categorical variables&#8230;<\/a><li class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#try-it-yourself\" class=\"uagb-toc-link__trigger\">Try it yourself<\/a><li class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#further-reading\" class=\"uagb-toc-link__trigger\">Further Reading<\/a><\/ul><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2 class=\"wp-block-heading\" id=\"two-way-tables\">Two-Way Tables and Marginal Distributions<\/h2>\n\n\n<p class=\"wp-block-paragraph\">A <strong>two-way table<\/strong> is a table with rows and columns that helps organize data from categorical variables:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Rows<\/strong> represent the possible categories for one qualitative variable, for example males and females.\n<\/li>\n\n\n<li><strong>Columns<\/strong> represent the possible categories for a second qualitative variable, for example whether someone likes pizza or not&#8230;\n<\/li>\n<\/ul>\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\">Let\u2019s look at an example.<\/p>\n\n\n<p class=\"wp-block-paragraph\"><em>Note: I couldn\u2019t 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\n\n<p class=\"wp-block-paragraph\">We build the table in R:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>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\n\n<p class=\"wp-block-paragraph\">and we get something like this:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>                 star trek   star wars   doctor who\ndegree               44          38          26\ndiploma              53          35          30\nlower education      58          22          29<\/code><\/pre>\n\n\n<div class=\"wp-block-group has-background\" style=\"background-color:#f5f7f9;margin-top:2.5rem;margin-bottom:2.5rem;padding-top:1.5rem;padding-right:1.5rem;padding-bottom:1rem;padding-left:1.5rem\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-container-core-group-is-layout-eed7543b wp-block-group-is-layout-constrained\">\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"600\" src=\"https:\/\/www.gironi.it\/blog\/wp-content\/uploads\/2026\/07\/contingenza-mosaic.png\" alt=\"Mosaic plot of the contingency table: education level \u00d7 favourite sci-fi series. Each rectangle\u2019s area is proportional to the cell frequency.\" class=\"wp-image-4352\" srcset=\"https:\/\/www.gironi.it\/blog\/wp-content\/uploads\/2026\/07\/contingenza-mosaic.png 975w, https:\/\/www.gironi.it\/blog\/wp-content\/uploads\/2026\/07\/contingenza-mosaic-300x185.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption class=\"wp-element-caption\">Mosaic plot of the contingency table: education level \u00d7 favourite sci-fi series. Each rectangle\u2019s area is proportional to the cell frequency.<\/figcaption><\/figure>\n\n<\/div><\/div>\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\">We can compute row totals in R with:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>margin.table(scifi_fans, 1)<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">and column totals with:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>margin.table(scifi_fans, 2)<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">We can also find the \"grand total\" with:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>margin.table(scifi_fans)<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Here is the table with totals:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>              star trek   star wars   doctor who   TOTAL\ndegree            44          38          26         108\ndiploma           53          35          30         118\nlower ed.         58          22          29         109\nTOTAL            155          95          85         335<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">So the marginal totals by education level are 108 for degree holders, 118 for diploma holders, 109 for lower education.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Likewise, the marginal totals by sci-fi series type are 155 for Star Trek, 95 for Star Wars, 85 for Doctor Who.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The grand total must be the same in both directions, in this case 335.<\/p>\n\n\n<p class=\"wp-block-paragraph\">We could also have displayed a complete table with totals using just a few lines of R code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>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\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\"><strong>26\/108 = 0.24 = 24% of degree holders prefer Doctor Who<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">And how many Star Wars fans hold a diploma?<\/p>\n\n\n<p class=\"wp-block-paragraph\"><strong>35\/95 = 0.37 = 37% of Star Wars fans are diploma holders<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">In R, we can directly obtain row proportions with the function:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>prop.table(scifi_fans, 1)<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">and the result will be:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>                 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<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">(as we can see, the row totals add up to 1, or 100%)<\/p>\n\n\n<p class=\"wp-block-paragraph\">or column proportions with:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>prop.table(scifi_fans, 2)<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">and the result will be:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>                 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<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">(as we can see, the column totals add up to 1, or 100%)<\/p>\n\n\n<p class=\"wp-block-paragraph\">As always, there is more than one way to get the result. We can also install the \u201cgmodels\u201d package and use the CrossTable function (we\u2019ll leave it to R\u2019s built-in help to show all the command options...):<\/p>\n\n\n<pre class=\"wp-block-code\"><code>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\n\n<p class=\"wp-block-paragraph\">So what is all this good for? The answer is: for example, to compute <strong>conditional probability<\/strong>.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2 class=\"wp-block-heading\" id=\"conditional-probability\">Conditional Probability<\/h2>\n\n\n<p class=\"wp-block-paragraph\">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\/\" target=\"_blank\" rel=\"noopener noreferrer\">probability<\/a>.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\"><strong>Key definition: <em><a href=\"https:\/\/www.gironi.it\/blog\/en\/first-steps-into-the-world-of-probability\/\" target=\"_blank\" rel=\"noopener noreferrer\">for equally likely events, the probability is the ratio of the number of favorable cases to the number of possible cases<\/a><\/em><\/strong><\/p>\n\n\\( P(A) = \\frac {\\text{number of favorable cases}}{\\text{number of possible cases}}\\\\ \\)\n\n\n<p class=\"wp-block-paragraph\">Let us also recall that:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>The probability of the intersection of two events cannot be greater than the probability of each individual event taken separately.\n<\/li>\n\n\n<li>If two possible events, A and B, are independent, then the probability that both occur is the product of their individual probabilities.\n<\/li>\n\n\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%.\n<\/li>\n<\/ul>\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\">The formula is:<\/p>\n\n\\( P(A|B) = \\frac {P(A \\text{ and } B)}{P(B)}\\\\ \\)\n\n\n<p class=\"wp-block-paragraph\">When a probability concerns <strong>one variable only<\/strong>, we speak of a <strong>marginal probability<\/strong>; when it concerns <strong>two variables simultaneously<\/strong>, we speak of a <strong>joint probability<\/strong>.<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>probability of an event<\/strong> P(A) is: \\( \\frac {\\text{marginal probability of A}}{\\text{Total}}\\\\ \\)\n<\/li>\n\n\n<li>The <strong>joint probability of two events<\/strong> is: \\( \\frac {P(A \\text{ and } B)}{\\text{Total}}\\\\ \\)\n<\/li>\n\n\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)}\\\\ \\)\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\">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\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<p class=\"wp-block-paragraph\">It\u2019s 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\n\n<p class=\"wp-block-paragraph\">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\n\n<pre class=\"wp-block-code\"><code>                    parents    parents     TOTAL\n                      use      no use\nstudent uses          125        94         219\nstudent does not use   85       141         226\nTOTAL                 210       235         445<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s apply our knowledge to answer these questions:<\/p>\n\n\n<p class=\"wp-block-paragraph\">1. <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><\/p>\n\n\n<p class=\"wp-block-paragraph\">This is a case of conditional probability.<br>We recall \\( P(A|B) = \\frac {P(A \\text{ and } B)}{P(B)}\\\\ \\), therefore<\/p>\n\n\n<p class=\"wp-block-paragraph\">P(<em>student uses given that parents used<\/em>) = 125 \/ 210 = 0.59 = 59%<\/p>\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\">Here again we face a question that asks for a conditional probability. Therefore:<\/p>\n\n\n<p class=\"wp-block-paragraph\">P(<em>parents used given that student does not use<\/em>) = 85 \/ 226 = 0.376 = 37.6%<\/p>\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\">In this case we need to find a joint probability, so:<\/p>\n\n\n<p class=\"wp-block-paragraph\">\\( \\frac {P(A \\text{ and } B)}{\\text{Total}}\\\\ \\), therefore \\( \\frac {85}{445} = 0.19\\\\ \\).<\/p>\n\n\n<p class=\"wp-block-paragraph\">The probability is approximately 19%.<\/p>\n\n\n<div class=\"wp-block-group has-background\" style=\"background-color:#f5f7f9;margin-top:2.5rem;margin-bottom:2.5rem;padding-top:1.5rem;padding-right:1.5rem;padding-bottom:1rem;padding-left:1.5rem\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-container-core-group-is-layout-eed7543b wp-block-group-is-layout-constrained\">\n\n<div class=\"wp-block-group has-background\" style=\"background-color:#f5f7f9;margin-top:2.5rem;margin-bottom:2.5rem;padding-top:1.5rem;padding-right:1.5rem;padding-bottom:1rem;padding-left:1.5rem\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-container-core-group-is-layout-eed7543b wp-block-group-is-layout-constrained\">\n\n<div class=\"wp-block-group has-background\" style=\"background-color:#f5f7f9;margin-top:2.5rem;margin-bottom:2.5rem;padding-top:1.5rem;padding-right:1.5rem;padding-bottom:1rem;padding-left:1.5rem\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-container-core-group-is-layout-eed7543b wp-block-group-is-layout-constrained\">\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"600\" src=\"https:\/\/www.gironi.it\/blog\/wp-content\/uploads\/2026\/07\/contingenza-probabilita-1.png\" alt=\"Probabilit\u00e0 marginale, condizionata e congiunta a confronto: lo stesso dato, tre domande diverse.\" class=\"wp-image-4358\" srcset=\"https:\/\/www.gironi.it\/blog\/wp-content\/uploads\/2026\/07\/contingenza-probabilita-1.png 975w, https:\/\/www.gironi.it\/blog\/wp-content\/uploads\/2026\/07\/contingenza-probabilita-1-300x185.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption class=\"wp-element-caption\">Probabilit\u00e0 marginale, condizionata e congiunta a confronto: lo stesso dato, tre domande diverse.<\/figcaption><\/figure>\n\n<\/div><\/div>\n\n<\/div><\/div>\n\n<\/div><\/div>\n\n\n<div class=\"wp-block-group has-background\" style=\"background-color:#f5f7f9;margin-top:2.5rem;margin-bottom:2.5rem;padding-top:1.5rem;padding-right:1.5rem;padding-bottom:1rem;padding-left:1.5rem\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-container-core-group-is-layout-eed7543b wp-block-group-is-layout-constrained\">\n<p><strong>Conditional probability is not causation.<\/strong><\/p>\n<p>A high P(A|B) does not mean B causes A. In the marijuana example, P(student uses | parents used) = 59% vs P(student uses) = 49%: there\u2019s a relationship, but it could be due to other factors (family environment, genetic predisposition, social context). The same applies in SEO: a lower P(conversion | mobile) than P(conversion | desktop) doesn\u2019t mean mobile is a worse channel \u2014 it may reflect different intent (browsing on the go vs planned purchase from home).<\/p>\n<p>Contingency tables show associations, not causal links. To get closer to causality, we need controlled experiments or more advanced models (the <a href=\"https:\/\/www.gironi.it\/blog\/en\/the-chi-square-test-goodness-of-fit-and-test-of-independence\/\" target=\"_blank\" rel=\"noopener noreferrer\">chi-square test<\/a> of independence, regression).<\/p>\n<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h2 class=\"wp-block-heading\" id=\"dependence-independence\">Dependence and Independence<\/h2>\n\n\n<p class=\"wp-block-paragraph\">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\n\n<p class=\"wp-block-paragraph\">More rigorously: we can state that event B is independent of event A if:<\/p>\n\n\n<p class=\"wp-block-paragraph\">P(B|A) = P(B)<\/p>\n\n\n<p class=\"wp-block-paragraph\">or<\/p>\n\n\n<p class=\"wp-block-paragraph\">P(A|B) = P(A)<\/p>\n\n\n<p class=\"wp-block-paragraph\">If this is not the case, the events are dependent on each other.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Therefore:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>P(A and B) = P(A) P(B) if and only if A and B are independent events.\n<\/li>\n\n\n<li>P(A | B) = P(A) and P(B | A) = P(B) if and only if A and B are independent events.\n<\/li>\n<\/ul>\n\n\n<h3 class=\"wp-block-heading\" id=\"test-independence\">Let\u2019s examine the independence of categorical variables...<\/h3>\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s explain this better with an example.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Let A be the event that people enjoy cycling.<br>B expresses whether they enjoy roast lamb. (Makes perfect sense, right?)<\/p>\n\n\n<p class=\"wp-block-paragraph\">We build our contingency table:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>                  Likes cycling   Doesn\u2019t like cycling   TOTAL\nLikes roast lamb       95                36                131\nNo roast lamb          15                19                 34\n---------------------------------------------------------------\nTOTAL                 110                55                165<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s remember what it means for two events to be independent. It means this:<br>P(A | B) = P(A)<\/p>\n\n\n<p class=\"wp-block-paragraph\">But in our case we see that<br>P(A) = 66.7%<br>because 110\/165 = 0.67<\/p>\n\n\n<p class=\"wp-block-paragraph\">P(A | B) = 72.5%<br>because 95\/131 = 0.725<\/p>\n\n\n<p class=\"wp-block-paragraph\">We recall that \\( P(A|B) = \\frac {P(A \\text{ and } B)}{P(B)}\\\\ \\), therefore \\( \\frac {95}{131} = 0.725\\\\ \\).<\/p>\n\n\n<p class=\"wp-block-paragraph\">From the result it is clear that \\( P(A) \\neq P(A|B) \\) \u2014 the two events are NOT independent (therefore they are dependent).<\/p>\n\n\n<p class=\"wp-block-paragraph\">After all, everyone knows that there is a clear dependence between loving cycling and loving roast lamb!<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h3 class=\"wp-block-heading\" id=\"try-it\">Try it yourself<\/h3>\n\n\n<p class=\"wp-block-paragraph\">Now let\u2019s try a more SEO-relevant example. Suppose we have 500 sessions on an ecommerce site, cross-referencing device type with session outcome:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>                  Converted  Did not convert  TOTAL\nDesktop              82           218         300\nMobile               28           172         200\nTOTAL               110           390         500<\/code><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Now try to answer:<\/p>\n\n\n<p class=\"wp-block-paragraph\">1. <strong>What is the probability that a mobile session converts?<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">2. <strong>Among sessions that converted, what proportion came from desktop?<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">3. <strong>If device and conversion were independent, how many desktop conversions would we expect?<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\"><em>The answers:<\/em><\/p>\n\n\n<p class=\"wp-block-paragraph\">1. P(conversion | mobile) = 28 \/ 200 = <strong>14%<\/strong>.<br>2. P(desktop | conversion) = 82 \/ 110 = <strong>74.5%<\/strong>.<br>3. If independent, P(conversion) \u00d7 P(desktop) \u00d7 total = (110\/500) \u00d7 (300\/500) \u00d7 500 = <strong>66<\/strong> expected desktop conversions (we observed 82 \u2014 that difference is exactly what the <a href=\"https:\/\/www.gironi.it\/blog\/en\/the-chi-square-test-goodness-of-fit-and-test-of-independence\/\" target=\"_blank\" rel=\"noopener noreferrer\">chi-square test<\/a> evaluates).<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<p class=\"wp-block-paragraph\">We\u2019ve seen how contingency tables allow us to compute marginal, conditional, and joint probabilities, and to check whether two categorical variables are independent. But there\u2019s one aspect we only touched on: how do we establish whether the difference between P(conversion | mobile) and P(conversion | desktop) is <strong>statistically significant<\/strong>? It\u2019s not enough for the two numbers to be different \u2014 the difference could be random noise.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The answer is the <strong><a href=\"https:\/\/www.gironi.it\/blog\/en\/the-chi-square-test-goodness-of-fit-and-test-of-independence\/\" target=\"_blank\" rel=\"noopener noreferrer\">chi-square test of independence<\/a><\/strong>, which compares observed frequencies with those expected under the hypothesis of independence. It\u2019s the natural complement to what we\u2019ve covered here, and one of the most widely used tools in SEO for comparing performance across segments (device, channel, page, time period). In the next article, we\u2019ll see how it works and how to apply it to your site\u2019s data.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<h3 class=\"wp-block-heading\">Further Reading<\/h3>\n\n\n<p class=\"wp-block-paragraph\">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&#038;ascsubtag=contingency-tables-and-conditional-probability\" 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>Have you ever noticed how mobile users behave differently from desktop users? It&#8217;s not just a feeling \u2014 it\u2019s a relationship between two categorical variables. The variable &#8220;device&#8221; (mobile or desktop) and the variable &#8220;converted&#8221; (yes or no) \u2014 when we cross them in a table, we can see if there\u2019s a systematic link. Contingency &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":"Paolo Gironi","author_link":"https:\/\/www.gironi.it\/blog\/author\/autore-articoli\/"},"uagb_comment_info":0,"uagb_excerpt":"Have you ever noticed how mobile users behave differently from desktop users? It&#8217;s not just a feeling \u2014 it\u2019s a relationship between two categorical variables. The variable &#8220;device&#8221; (mobile or desktop) and the variable &#8220;converted&#8221; (yes or no) \u2014 when we cross them in a table, we can see if there\u2019s a systematic link. Contingency&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":5,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/posts\/3482\/revisions"}],"predecessor-version":[{"id":4364,"href":"https:\/\/www.gironi.it\/blog\/wp-json\/wp\/v2\/posts\/3482\/revisions\/4364"}],"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}]}}