Skip to main content

• 7. Revisiting two cats

Motivating Scenario: You are digging deeper into your summaries of associations between two categorical variables.

Learning Goals: By the end of this subchapter, you should be able to:

  1. Explain the intuition of the multiplication rule for independent events

  2. Calculate and explain the covariance as a deviation from expectations under independence. Again you should be able to do this with basic math and with R code.


Alternative formats: 🎥 Watch  ·  🎧 Listen


Loading and processing data.
library(dplyr)
library(readr)
library(ggplot2)

ril_link <- "https://raw.githubusercontent.com/ybrandvain/datasets/refs/heads/master/clarkia_rils.csv"
gc_rils <- readr::read_csv(ril_link) |>
    rename(visits = mean_visits)|>
    mutate(visited = ifelse(visits > 0,"some_visits","no_visits")) |>
    dplyr::filter(location == "GC", !is.na(prop_hybrid), ! is.na(visits),!is.na(petal_color))|>
    select(petal_color, visits, visited, prop_hybrid)

Unconditional & joint proportions

In the previous section we calculated UNCONDITIONAL (also known as marginal) proportions (n_case / n_total) of two binary categorical variables. Let’s call these proportions:

  • \(P_\text{pink}= n_\text{pink} / n_\text{plants}\)

  • \(P_\text{some visits}= n_\text{some visits} / n_\text{plants}\)

The R code below calculates both these UNCONDITIONAL proportions, and the joint probability of being both pink AND visited:

gc_rils |>
  summarise(n_pink             = sum(petal_color == "pink"),
            n_visited          = sum(visited == "some_visits"),
            n_pink_visited     = sum(petal_color == "pink" & visited == "some_visits"),
            n_total            = n(),                           #assuming no NAs
            prop_pink          = n_pink / n_total,
            prop_visited       = n_visited / n_total,
            p_pink_AND_visited = n_pink_visited / n_total) 
n_pink n_visited n_pink_AND_visited n_total prop_pink prop_visited p_pink_AND_visited
49 24 22 91 0.538 0.264 0.242

We can similarly calculate the UNCONDITIONAL proportion of white-petaled RILs and the joint probability of being both white AND visited (note, I also recalculate the proportion of visited plants):

gc_rils |>
  summarise(n_white             = sum(petal_color == "white"),
            n_visited           = sum(visited == "some_visits"),
            n_white_AND_visited = sum(petal_color == "white" & visited == "some_visits"),
            n_total             = n(),                           #assuming no NAs
            prop_white          = n_white / n_total,
            prop_visited        = n_visited / n_total,
            p_white_AND_visited = n_white_AND_visited / n_total) 
n_white n_visited n_white_AND_visited n_total prop_white prop_visited p_white_AND_visited
42 24 2 91 0.462 0.264 0.022

Conditional proportions

We can also calculate the proportions of one outcome, depending on the value of another variable. We quantified this as a CONDITIONAL PROPORTION:

  • \[P_\text{some visits | pink}= \frac{n_\text{some visits | pink}}{ n_\text{pink plants}} = \frac{22}{49}=0.449\]

  • \[P_\text{some visits | white}= \frac{n_\text{some visits | white}}{ n_\text{white plants}} = \frac{2}{42}=0.048\]

These differed substantially. Pink plants were way more likely to be visited than white plants.

gc_rils |>
  summarise(n_pink        = sum(petal_color == "pink"),
            n_white       = sum(petal_color == "white"),
            p_visit_pink  = sum(petal_color == "pink"  & visited == "some_visits") / n_pink ,
            p_visit_white = sum(petal_color == "white" & visited == "some_visits") / n_white) 
n_pink n_white p_visit_pink p_visit_white
49 42 0.449 0.048

Covariance

A common quantification of the association between two binary variables is the covariance. This quantification follows some simple mathematical logic:

  • If two categorical variables, A & B are independent, the probability of observing both A and B (aka, \(P_{AB}\)) should equal the product of their unconditional probabilities: \[P_{AB \mid \text{independence}} = P_A \times P_B\]

  • The covariance is the deviation from this expectation: \[COV_{AB} = P_{AB} - P_A \times P_B\]

So for the association between petal color and being visited, the covariance is:

\[COV_{\text{visited, pink}} =P_{\text{visited and pink}} - P_{\text{visited}} \times P_{\text{pink}}\]

We can find this from our work above:

\[ \begin{aligned} \operatorname{Cov}_{\text{visited, pink}} &= \frac{22} {91} - \frac{24} {91} \times \frac{49} {91} \\[6pt] &= 0.242 - \left( 0.264 \times 0.538 \right) \\[6pt] &= 0.242 - 0.142 \\[6pt] &= 0.0997. \end{aligned} \]

Or we can find this in R:

gc_rils |>
  summarise(p_pink_and_visited   = mean(petal_color == "pink" & visited  == "some_visits"),
            p_pink               = mean(petal_color == "pink"),
            p_visited            = mean(visited  == "some_visits"),
            cov_pink_and_visited = p_pink_and_visited - p_pink  * p_visited)
p_pink_and_visited p_pink p_visited cov_pink_and_visited
0.2418 0.5385 0.2637 0.0997

Alternatively, R can calculate the covariance with the cov() function. This only works if the values are numbers or logical (TRUE [1] / FALSE [0]). Note the slight mismatch between our calculation due to “Bessel’s correction” (see fyi box, below).

gc_rils |>
  summarise(cov_pink_visited = cov(petal_color == "pink", visited  == "some_visits"))
cov_pink_visited
0.1009

Our answers differ slightly from one another. We used a denominator of \(n\) (which calculates the population covariance), while the sample covariance has a denominator of \(n-1\) (this is known as Bessel’s correction). We will soon see that although the latter is more appropriate – their difference is negligible when n is large. We can convert the population covariance to the sample covariance by multiplying by \(\frac{n}{n-1}\). See the next section for a discussion of the difference between population parameters and sample estimates.

From covariance to correlation

Covariance gives us a numerical measure of how far our data deviate from what we’d expect under independence. In this case, the value is 0.10. But is a covariance of 0.10 big or small? A covariance on its own provides no clue of its strength.

We can convert a covariance to an effect size, known as a correlation (or r). r describes both the strength and sign of the association between variables:

  • An absolute value of r close to 1 means that x almost perfectly predicts y, while a value close to zero means x has no information about y.

  • The sign of r describes if x and y increase (\(r > 0\)), or decrease (\(r <0\)) with one another.

We can find r by dividing the covariance by the product of standard deviations:

\[r_{x,y}= \frac{cov_{x,y}}{s_x s_y}\]

We have previously seen the equation for a standard deviation: \[s_x = \sqrt{\frac{\sum{(x_i-\bar{x})^2 }}{n-1}}\]

For a binary variable, this equation is approximately \(s_x=\sqrt{p_x (1-p_x)}\) (ignoring Bessel’s correction).

So in our case:

\[ \begin{aligned} r_{\text{pink, visited}} &= \frac{ \operatorname{Cov}_{\text{pink, visited}} }{ \sqrt{ p_{\text{pink}}(1-p_{\text{pink}}) \, p_{\text{visited}}(1-p_{\text{visited}}) } } \\[6pt] &= \frac{0.1}{ \sqrt{ 0.538 \times 0.462 \times 0.264 \times 0.736 } } \\[6pt] &= \frac{0.1}{ \sqrt{ 0.048 } } \\[6pt] &= \frac{0.1}{0.219}} \\[6pt] &\approx 0.456. \end{aligned} \]

Or in R:

gc_rils |>
  summarise(var_pink = mean(petal_color == "pink") * (1 - mean(petal_color == "pink")),
            var_visited = mean(visited == "some_visits") * (1 -  mean(visited == "some_visits")),
            cov_pink_visited = cov(petal_color == "pink", visited  == "some_visits"),
            cor_pink_visited =  cov_pink_visited / (sqrt(var_pink) * sqrt(var_visited ))) 
var_pink var_visited cov_pink_visited cor_pink_visited
0.2485 0.1942 0.1009 0.4591

This is a strong correlation!

gc_rils |>
  summarise(cor_pink_visited = cor(petal_color == "pink", visited  == "some_visits"))
cor_pink_visited
0.4541

\(r\): effect size guide

As in Cohen’s D, what is a “large” or “small” correlation coefficient depends on the study, the question and the field of study, but there are rough guides (see below). So our observed correlation between petal color and being visited by a pollinat 0.45 is large.

Size Range of \(\lvert r \rvert\)
Not worth reporting < 0.005
Tiny 0.005 – 0.10
Small 0.10 – 0.20
Medium 0.2 – 0.35
Large 0.35 – 0.50
Very large 0.50 – 0.75
Huge \(> 0.75\)

Applications to population genetics:

Two population genetic summaries tend to confuse genetics students – deviations from Hardy-Weinberg Equilibrium, and Linkage Disequilibrium. Both can be naturally considered as covariances between two binary variables.


Hardy-Weinberg Equilibrium (HWE)

The Hardy-Weinberg Equilibrium principle states that under numerous assumptions – which essentially ensure that an individual’s two alleles at a locus are chosen independently – genotypes at a locus are simply two random samples of alleles from a population,

  • Genotype frequencies should be \(p^2\), \(2p(1-p)\), and \((1-p)^2\)
  • Where \(p\) is the frequency of one allele, A, at a locus.

We can think of this as a covariance between alleles inherited from mom and dad at the same locus.

  • So \(p^2\) is actually \(p_\text{A maternal} \times p_\text{A paternal}\).
  • The deviation from HWE, \(F\), is simply the difference between the observed frequency of homozygotes (\(p_\text{AA} = p_\text{A maternal,A paternal}\)) and their expected frequency, \(p^2\). So \(F = p_\text{AA}- p_A^2\).

Linkage Disequilibrium (LD)

Linkage disequilibrium quantifies the statistical association between alleles at different loci. The most standard quantification of linkage disequilibrium is \(D = Cov(A,B) = p_{AB} - p_A p_B\). Where A and B are alleles at two different loci. Although usually quantified as a covariance, other measures of LD include a correlation in allele frequencies between loci.

.