execs <- c(rep('M', 4), rep('N', 3), rep('D', 2))
one_group <- \() sample(execs, size = 3, replace= FALSE)
one_group()
N <- 10000
(many_groups <- replicate(N, one_group(), .as = selected)
%>% mutate(X = map_int(selected, \(s) sum(s == 'M')),
Y = map_int(selected, \(s) sum(s == 'N')))
)
# A tibble: 10,000 × 4
.trial selected X Y
<dbl> <list> <int> <int>
1 1 <chr [3]> 1 2
2 2 <chr [3]> 0 2
3 3 <chr [3]> 0 2
4 4 <chr [3]> 2 1
5 5 <chr [3]> 1 1
6 6 <chr [3]> 2 0
7 7 <chr [3]> 2 0
8 8 <chr [3]> 1 2
9 9 <chr [3]> 1 0
10 10 <chr [3]> 1 1
# ℹ 9,990 more rows
#8A
(many_groups
%>% summarize(cnt = n(), .by = c(X,Y))
%>% mutate(prob = cnt / N)
%>% select(X, Y, prob)
%>% pivot_wider(names_from = Y, values_from = prob, values_fill = 0))
# A tibble: 4 × 5
X `2` `1` `0` `3`
<int> <dbl> <dbl> <dbl> <dbl>
1 1 0.142 0.279 0.0472 0
2 0 0.0757 0.0326 0 0.0122
3 2 0 0.223 0.139 0
4 3 0 0 0.05 0
#8B
N <- 10000
many_groups %>%
summarize(cnt = n(), .by = c("X", "Y")) %>%
mutate(jointprob = cnt / N,
probX_givenY = cnt / sum(cnt), .by = "Y")
# A tibble: 9 × 5
X Y cnt jointprob probX_givenY
<int> <int> <int> <dbl> <dbl>
1 1 2 1418 0.142 0.652
2 0 2 757 0.0757 0.348
3 2 1 2228 0.223 0.417
4 1 1 2787 0.279 0.522
5 2 0 1390 0.139 0.588
6 1 0 472 0.0472 0.200
7 3 0 500 0.05 0.212
8 0 1 326 0.0326 0.0610
9 0 3 122 0.0122 1
ggplot(data = many_groups) +
geom_jitter(aes(x=X, y=Y), width=.1, height=.3, size =.2, alpha = .8) +
scale_x_continuous(breaks = 0:3) +
scale_y_continuous(breaks = c(-1, 1, 2, 3)) +
theme_classic(base_size = 20)