Chapter 6.4 Practice Set

Question 2

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   4.0.0     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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
library(purrrfect)

Attaching package: 'purrrfect'

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

    replicate, tabulate
N <- 10000
(simstudy <- data.frame(X = runif(N, 0, 1), Y = runif(N, 0, 1))
  %>% mutate(U = X + Y,
             V = X - Y)
) %>% head
          X          Y         U            V
1 0.4475470 0.44114010 0.8886871  0.006406946
2 0.1759261 0.07203938 0.2479655  0.103886715
3 0.7409586 0.82612951 1.5670881 -0.085170914
4 0.5480133 0.98397634 1.5319896 -0.435963056
5 0.9566844 0.01061321 0.9672976  0.946071220
6 0.3891559 0.04230736 0.4314633  0.346848550
base <- ggplot(data = simstudy) + theme_classic()

# Scatterplot
(scatterplot <- base + geom_point(aes(x = U, y = V), shape = '.', alpha = 0.6))