# ============================================================================== # CarnivalConfidence.r # Games at a Carnival: Confidence and Precision # academy.statypus.org # ============================================================================== # # YOUR TASK: # 1. Open your Statypus Textbook to Section 9.2. # 2. Locate the "Estimation Template" code block. # 3. Copy that entire block of code and paste it below. # 4. Update the variables to match the scenarios in your worksheet. # --- PASTE YOUR TEXTBOOK TEMPLATE BELOW THIS LINE --- # ============================================================================== # --- PART 2: THE CONFIDENCE RIPPLE (PAGE 6) --- # ============================================================================== # Download the data file from: https://r.statypus.org/data/candy_data.csv # Save it in the same folder as this script. candy_data <- read.csv("https://statypus.org/files/candy_data.csv") # Calculate the point estimate (phat) for all 100 bags candy_data$phat <- candy_data$white / (candy_data$white + candy_data$blue) # Note that this actually expands the data frame by adding a new column # called phat. # Calculate SE and MOE for all 100 bags (using z* = 1.96 for 95% Confidence) candy_data$SE <- sqrt((candy_data$phat * (1 - candy_data$phat)) / (candy_data$white + candy_data$blue)) candy_data$MOE <- 1.96 * candy_data$SE # Here we append the columns SE and MOE # Build the 100 lower and upper bounds candy_data$lower <- candy_data$phat - candy_data$MOE candy_data$upper <- candy_data$phat + candy_data$MOE # Audit: How many intervals successfully 'caught' the true parameter 0.50? hits <- sum(candy_data$lower <= 0.50 & candy_data$upper >= 0.50) cat("Out of 100 Confidence Intervals, the true p (0.50) was captured", hits, "times.\n")