#5
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
N <- 10000
set.seed(450)
(fires <- rmultinom(N, size = 50, prob = c(.73, .20, .07))
%>% t()
%>% as.data.frame()
%>% setNames(c('y1', 'y2', 'y3'))
) %>% head y1 y2 y3
1 33 12 5
2 37 10 3
3 36 10 4
4 40 7 3
5 36 11 3
6 35 13 2
ggplot(data = fires, aes(x = y1, y = y2)) +
geom_jitter(width = .3, height = .3, size = .2, alpha = .3) +
labs(x = expression(Y[1]~'(family homes)'),
y = expression(Y[2]~'(apartments)')) +
theme_classic()# A
(fires
%>% count(y1, y2)
%>% arrange(desc(n))
%>% head(5)
) y1 y2 n
1 38 9 311
2 37 10 309
3 36 11 289
4 36 10 279
5 37 9 278
# B
(fires
%>% select(y1, y2)
%>% cov()
) y1 y2
y1 9.624058 -7.157238
y2 -7.157238 7.855573
(fires
%>% summarize('Simulated Cov' = cov(y1, y2),
'Analytic Cov' = -50 * .73 * .20)
) Simulated Cov Analytic Cov
1 -7.157238 -7.3