# ============================================================================== # SweetCelebration.r # A Sweet Celebration: Sampling Distributions # academy.statypus.org # ============================================================================== # Instructions: Execute each line one by one as directed by your PDF worksheet. # Inspect the objects created in your Environment pane. # --- Part 1: Macro Population Audit --- # Download the audit dataset directly from the Statypus servers. candy_data <- read.csv("https://statypus.org/files/candy_data.csv") # Look at your Environment pane. Click the spreadsheet icon next to candy_data # to see the structure of the bag_id, white_count, and blue_count columns. # Task: Use the sum() function to calculate the total candies across all 100 bags. total_white <- sum(candy_data$white_count) total_blue <- sum(candy_data$blue_count) total_white total_blue # --- Part 2: Manual Row Extraction --- # R allows us to isolate specific rows in a column using the bracket [ ] operator. # Let's extract the white and blue counts for the very first bag: candy_data$white_count[1] candy_data$blue_count[1] # We can manually calculate the proportion for Bag 1 using R as a calculator: candy_data$white_count[1] / (candy_data$white_count[1] + candy_data$blue_count[1]) # Task: Modify the brackets below to find the counts and calculate the # proportion for Bag 3, then record it on your worksheet. candy_data$white_count[3] candy_data$blue_count[3] # Type your division math here: # --- Part 3: Vectorization --- # Let's stop checking them one by one. We will calculate the total candies # in each bag for the entire column simultaneously. candy_data$total_in_bag <- candy_data$white_count + candy_data$blue_count # Note that this actually expands the data frame by adding a new column # called total_in_bag. # Now, we divide the entire white column by the total column to generate 100 p-hats. candy_data$prop_white <- candy_data$white_count / candy_data$total_in_bag # Again, this extends the data frame by adding a new column called prop_white, # which contains the sample proportion of white candies for each bag. # Visualize the resulting Sampling Distribution of p-hat. hist(candy_data$prop_white, breaks = 12, col = "lightgray", main = "Sampling Distribution of 100 Guest Experiences (p-hat)", xlab = "Sample Proportion of White Candies", ylab = "Frequency (Number of Bags)") # Add a red line marking the location of the very first bag we opened. abline(v = 31/52, col = "red", lwd = 2, lty = 2) # --- Part 4: Theoretical Tail Verification --- # Using the theoretical parameters calculated by hand on Page 5. theoretical_mean <- 0.50 theoretical_sd <- sqrt((0.50 * (1 - 0.50)) / 50) # Task: Use the pnorm function to find the probability of getting # a sample proportion (p-hat) of 0.596 or greater. # Hint: Remember to set lower.tail correctly for a "greater than" question! pnorm( ) # We can check to see how close our actual data fits this model. # We can use the sum() function along with a logical condition to count how many # bags had a p-hat of 0.596 or greater. sum( candy_data$prop_white >= 0.596 ) # The reason this works is that the line of code inside the sum() function # creates a vector of TRUE and FALSE values, and then the sum() # function treats TRUE as 1 and FALSE as 0, so it effectively counts the TRUEs.