################################################################## # DifferenceLogic.r # Statypus Academy: Two Sample Population Means # Course Resources: r.statypus.org ################################################################## # --- SECTION 1: THE INDEPENDENT CASE (Two Separate Hills) --- # 1. Prepare the data from the built-in Iris set seto <- iris$Sepal.Length[iris$Species == "setosa"] vers <- iris$Sepal.Length[iris$Species == "versicolor"] # 2. Plot the "Two Hills" # This plot shows two distinct populations with an observable gap. plot(density(seto), xlim=c(4, 8), ylim=c(0, 1.2), main="Scenario A: Independent (The Two-Hill Logic)", xlab="Sepal Length (cm)", lwd=2) lines(density(vers), col="blue", lwd=2) # 3. Marking the Centers (The peaks of the hills) abline(v=mean(seto), lty=2) abline(v=mean(vers), col="blue", lty=2) # STOP: Use this plot to complete Task 1 on your handout. # --- SECTION 2: THE PAIRED CASE (The Hill of Change) --- # 1. We simulate a "PM measurement" for the same Setosa plants. # We assume each flower grew a small amount (mean = 0.3cm). set.seed(45) pm_measure <- seto + rnorm(50, mean=0.3, sd=0.1) # 2. THE PAIRED MAPPING: Subtract FIRST to find individual change change <- pm_measure - seto # 3. Plot the "One Hill of Change" # We are plotting the story of growth, not the flowers themselves. plot(density(change), xlim=c(-0.2, 0.8), main="Scenario B: Paired (The Single-Hill Logic)", xlab="Individual Growth (cm)", lwd=2, col="darkgreen") # 4. The Null Marker (Zero Change) # If the Day was uneventful, the hill would be centered here. abline(v=0, lty=3, col="red", lwd=2) # STOP: Use this plot to complete Task 2 on your handout.