You Try Answers: Joint Distributions

POSC 8410, Week 7

Author

Will Horne

Published

October 26, 2026

library(tidyverse)
anes <- read_csv("../Slides/Data/anes.csv", show_col_types = FALSE)

You Try

1. Build the joint distribution of votechoice and aca as proportions of the whole sample. Do the cells sum to 1?

In class, I tried using prop.table which didn’t work because of the data format.

From the definition of the joint distribution, we could just build by hand, which would look like this

## filter to the variables and rows we need
d <- anes |>
  select(votechoice, aca) |>
  filter(!is.na(votechoice), votechoice != "Other", !is.na(aca))

joint <- d |> count(votechoice, aca) |> mutate(p = n / sum(n))

## reshapes the data to look like crosstabs
joint |> select(-n) |> pivot_wider(names_from = aca, values_from = p)
# A tibble: 2 × 4
  votechoice Approve Disapprove Neutral
  <chr>        <dbl>      <dbl>   <dbl>
1 Biden       0.481      0.0184  0.0717
2 Trump       0.0297     0.310   0.0890
sum(joint$p)
[1] 1

Yes, they sum to exactly 1.

Alternatively, we can let R do the work. prop.table() failed in class because it expects a table object, and I gave it a data frame. xtabs() converts a data frame into a contingency table of counts, which prop.table() can then use. (I remembered this solution at 3:46!)

## xtabs() cross-tabulates the variables on the right of the ~ and returns counts
counts <- xtabs(~ votechoice + aca, data = d)
counts
          aca
votechoice Approve Disapprove Neutral
     Biden    2739        105     408
     Trump     169       1766     507
## with no margin argument, prop.table() divides every cell by the grand total,  which gives the joint distribution
joint_tab <- prop.table(counts)
round(joint_tab, 3)
          aca
votechoice Approve Disapprove Neutral
     Biden   0.481      0.018   0.072
     Trump   0.030      0.310   0.089
sum(joint_tab)
[1] 1

The table object also makes Questions 2 and 3 one line each: margin.table(joint_tab, 2) gives the marginal of aca, and prop.table(counts, 1) gives the conditional distribution of aca given vote choice. Below I show how you could also compute by hand.

2. Get the marginal distribution of aca from it.

Sum over vote choice:

\[p_{\text{aca}}(a) = \sum_v p_{\text{vote},\,\text{aca}}(v, a)\]

joint |> group_by(aca) |> summarise(p = sum(p))
# A tibble: 3 × 2
  aca            p
  <chr>      <dbl>
1 Approve    0.511
2 Disapprove 0.329
3 Neutral    0.161

About 51% approve, 33% disapprove, and 16% are neutral.

3. Compute the conditional distribution of aca given votechoice. Which rows sum to 1?

Divide each cell by its own row total instead of the sample total:

\[p_{\text{aca} \mid \text{vote}}(a \mid v) = \frac{p_{\text{vote},\,\text{aca}}(v, a)}{p_{\text{vote}}(v)}\]

cond <- joint |>
  group_by(votechoice) |>
  mutate(p_cond = p / sum(p)) |>
  ungroup()

cond |>
  select(votechoice, aca, p_cond) |>
  pivot_wider(names_from = aca, values_from = p_cond)
# A tibble: 2 × 4
  votechoice Approve Disapprove Neutral
  <chr>        <dbl>      <dbl>   <dbl>
1 Biden       0.842      0.0323   0.125
2 Trump       0.0692     0.723    0.208
# check that each votechoice row sums to 1
cond |> group_by(votechoice) |> summarise(total = sum(p_cond))
# A tibble: 2 × 2
  votechoice total
  <chr>      <dbl>
1 Biden          1
2 Trump          1

Each vote choice row sums to 1, because vote choice is the variable we conditioned on.

4. Compute cor(ft_science, ft_fauci). Then compute the regression slope by hand using cov and var, and check it against lm.

sf <- anes |> filter(!is.na(ft_science), !is.na(ft_fauci))

cor(sf$ft_science, sf$ft_fauci)
[1] 0.5461521
# Slope = Cov(X, Y) / Var(X), with ft_science as the predictor X
cov(sf$ft_science, sf$ft_fauci) / var(sf$ft_science)
[1] 0.8202178
coef(lm(ft_fauci ~ ft_science, data = sf))[2]
ft_science 
 0.8202178 

The correlation is about 0.55, a moderate positive association. The hand-computed slope and lm agree at about 0.82: each additional point of warmth toward science goes with about 0.82 more points of warmth toward Fauci.

Why are the correlation (0.55) and the slope (0.82) different? The slope keeps the units, and correlation is unitless.

\[\beta_1 = \rho \cdot \frac{SD(Y)}{SD(X)}\]

cor(sf$ft_science, sf$ft_fauci) * sd(sf$ft_fauci) / sd(sf$ft_science)
[1] 0.8202178