Lab 14: Vectors and Iteration
Simulate a sampling distribution, then map models over diamonds.
Work in a Quarto document. Do notes 14.1 and 14.2 first.
library(tidyverse)
diamonds is in ggplot2. geom_hex() needs
the hexbin package for the optional plot.
1. A sampling distribution
- Draw 1000 samples of size 30 from
rexp(n = 30, rate = 1). Store the mean of each sample. Use a for-loop and pre-allocate the vector of means. Histogram the 1000 means. - The same 1000 means with
map_dbl(). - The same 1000 means with
replicate(). - Repeat the
replicate()version forninc(5, 50, 100, 500). Histogram each set of means. Write two sentences about how the shape changes asngrows.
2. Diamonds
- Summary of every numeric column of
diamondswith purrr, then again withacross(). What is different about the two results? split()bycolor. Fitlm(price ~ depth)in each piece. Extract the p-value fordepthand the adjusted R-squared.- Optional: a hex plot of
depthvsprice, faceted bycolor, with an OLS line. Do the p-values and R-squared values match what the panels look like?