################################################################## # TwoSampleHero.r # Statypus Academy: The Signal and the Noise # Course Resources: r.statypus.org ################################################################## # --- STAGE 1: INDEPENDENT SIGNALS (Setosa vs. Versicolor) --- # We return to our two distinct species. # Last block, we saw two separate hills. Now we ask: # Is the gap between them a "True Signal" or just environmental noise? seto <- iris$Sepal.Length[iris$Species == "setosa"] vers <- iris$Sepal.Length[iris$Species == "versicolor"] # The Universal Lens (Independent t-test) # We assume the groups are unrelated populations. independent_study <- t.test(seto, vers) # VIEW THE FIELD REPORT print(independent_study) # SEARCH THE OUTPUT: # 1. Find the "mean in group setosa" and "mean in group versicolor". # 2. Find the p-value. If this is < 0.05, the signal is clear. # --- STAGE 2: PAIRED SIGNALS (Growth over Time) --- # Now we look at the SAME individuals measured twice. # Because the "Morning" and "Evening" measurements are linked, # we can cancel out the "noise" unique to each individual plant. set.seed(45) morning <- iris$Sepal.Length[iris$Species == "setosa"] evening <- morning + rnorm(50, mean=0.3, sd=0.1) # We used the set.seed function to ensure that the random growth we added is the # same each time you run this code. This way, everyone will see the same results # when they run the paired t-test. However, if you want to see how the results # change with different random growth, you can remove or change the set.seed line. # The Universal Lens (Paired t-test) # We must tell the lens to focus on the relationship (paired = TRUE). paired_study <- t.test(evening, morning, paired = TRUE) # VIEW THE FIELD REPORT print(paired_study) # SEARCH THE OUTPUT: # 1. Find the "mean of the differences". This is the average growth. # 2. Find the 95 percent confidence interval. # This is our "Detection Range." If 0 is outside this range, # the growth signal is confirmed.