5.7-8

#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
#6

library(tidyverse)
library(purrrfect)

Attaching package: 'purrrfect'
The following objects are masked from 'package:base':

    replicate, tabulate
library(ggh4x)

set.seed(450)
(bvnorm_sim <- parameters(~sigx, ~sigy, ~rho,
                          c(0.5, 1),
                          c(0.5, 1),
                          c(-0.8, -0.4, 0, 0.4, 0.8))
  %>% add_trials(10000)
  %>% mutate(z1 = rnorm(n()),
             z2 = rnorm(n()),
             x_std = z1,
             y_std = rho * z1 + sqrt(1 - rho^2) * z2,
             x = sigx * x_std + 5,
             y = sigy * y_std + 5)
) %>% head
# A tibble: 6 × 10
   sigx  sigy   rho .trial      z1     z2   x_std   y_std     x     y
  <dbl> <dbl> <dbl>  <dbl>   <dbl>  <dbl>   <dbl>   <dbl> <dbl> <dbl>
1   0.5   0.5  -0.8      1  0.976  -0.891  0.976  -1.32    5.49  4.34
2   0.5   0.5  -0.8      2 -0.168  -0.756 -0.168  -0.319   4.92  4.84
3   0.5   0.5  -0.8      3  0.0633  0.107  0.0633  0.0137  5.03  5.01
4   0.5   0.5  -0.8      4 -1.13    0.323 -1.13    1.09    4.44  5.55
5   0.5   0.5  -0.8      5  0.187  -1.92   0.187  -1.30    5.09  4.35
6   0.5   0.5  -0.8      6  0.463   0.406  0.463  -0.127   5.23  4.94
ggplot(data = bvnorm_sim) +
  geom_point(aes(x = x, y = y), alpha = 0.5, size = .5) +
  annotate('point', x = 5, y = 5, col = 'red') +
  facet_nested(rho ~ sigx + sigy, labeller = label_both) +
  theme_bw()

#7
library(tidyverse)

N <- 10000
set.seed(450)
(simstudy <- data.frame(x = rexp(N, rate = 1))
  %>% mutate(y = map_dbl(x, \(x) x + rexp(1, rate = 1)))
) %>% head
          x         y
1 0.6710017 0.7901941
2 0.3745570 1.0536373
3 0.9633285 1.2076532
4 0.3951216 2.5672500
5 1.4275320 1.7260653
6 0.1066136 0.1674230
# Scatterplot with simulated and analytic E(Y|X) = 1 + X
ggplot(data = simstudy, aes(x = x, y = y)) +
  geom_point(size = .2, alpha = .5) +
  geom_smooth(method = 'loess', se = FALSE,
              aes(color = 'Simulated E(Y|X)'),
              linewidth = .7) +
  geom_function(fun = \(x) 1 + x,
                aes(color = 'Analytic E(Y|X)'),
                linewidth = .7) +
  labs(color = '') +
  theme_classic()
`geom_smooth()` using formula = 'y ~ x'

# e), g), h), j)
(simstudy
  %>% summarize('P(X<=1, Y<=2)' = mean(x <= 1 & y <= 2),
                'P(X<=1, Y<=0.5)' = mean(x <= 1 & y <= 0.5),
                'P(Y<X^2)' = mean(y < x^2),
                'E(X)' = mean(x),
                'E(Y)' = mean(y),
                'E(Y-X)' = mean(y - x),
                'Var(Y-X)' = var(y - x)
  )
)
  P(X<=1, Y<=2) P(X<=1, Y<=0.5) P(Y<X^2)      E(X)   E(Y)   E(Y-X) Var(Y-X)
1        0.4917          0.0892   0.2268 0.9968614 1.9985 1.001638 1.000921
#f
(simstudy
  %>% filter(x <= 1)
  %>% summarize('P(Y<=2 | X<=1)' = mean(y <= 2)
  )
)
  P(Y<=2 | X<=1)
1      0.7767773
#i
simstudy %>%
  cov
          x         y
x 0.9695599 0.9585303
y 0.9585303 1.9484215
simstudy %>%
  cor
          x         y
x 1.0000000 0.6973922
y 0.6973922 1.0000000
#k
lm(y ~ x, data = simstudy)

Call:
lm(formula = y ~ x, data = simstudy)

Coefficients:
(Intercept)            x  
     1.0130       0.9886