################################################################## # visualizingt.r # Statypus Academy: The Safety Net (t vs. Normal) # Course Resources: r.statypus.org ################################################################## # --- TASK A: THE SAFETY NET AUDIT (LINES 8-53) --- # 1. SETUP THE X-AXIS AND LIMITS x <- seq(-5, 5, length = 400) y_norm <- dnorm(x) y_t_small <- dt(x, df = 2) # 2. INITIAL PLOT plot(x, y_norm, type = "n", # Start with empty plot to layer correctly main = "The Safety Net: Statistical Skepticism (n=3)", xlab = "Standard Deviations (z or t)", ylab = "Density", ylim = c(0, 0.5), xlim = c(-5, 5)) # 3. DEFINE CRITICAL VALUES z_crit <- 1.96 t_crit <- 4.30 # 4. SHADE REJECTION ZONES (Fixed Flat Bottoms) # Normal Rejection (Gray) x_z_left <- seq(-5, -z_crit, length = 100) polygon(c(-5, x_z_left, -z_crit), c(0, dnorm(x_z_left), 0), col = "gray85", border = NA) x_z_right <- seq(z_crit, 5, length = 100) polygon(c(z_crit, x_z_right, 5), c(0, dnorm(x_z_right), 0), col = "gray85", border = NA) # t (df=2) Rejection (Red) x_t_left <- seq(-5, -t_crit, length = 100) polygon(c(-5, x_t_left, -t_crit), c(0, dt(x_t_left, df=2), 0), col = rgb(0.7, 0, 0, 0.4), border = NA) x_t_right <- seq(t_crit, 5, length = 100) polygon(c(t_crit, x_t_right, 5), c(0, dt(x_t_right, df=2), 0), col = rgb(0.7, 0, 0, 0.4), border = NA) # 5. DRAW CURVES AND THRESHOLDS lines(x, y_norm, lty = 2, col = "gray40", lwd = 1) lines(x, y_t_small, col = "black", lwd = 3) abline(v = c(-z_crit, z_crit), col = "gray40", lty = 2) abline(v = c(-t_crit, t_crit), col = "darkred", lty = 3, lwd = 2) # 6. CLEAN SINGLE LEGEND (Fixed Overlap) legend("topright", legend = c("Normal (Steel Rod)", "t (df=2, Rubber Band)", "z-threshold (1.96)", "t-threshold (4.30)"), col = c("gray40", "black", "gray40", "darkred"), lty = c(2, 1, 2, 3), lwd = c(1, 3, 1, 2), cex = 0.8, # Slightly smaller text to fit inset = 0.02, # Move away from edges bty = "n") # --- TASK B: THE CONVERGENCE (LINES 58-69) --- plot(x, y_norm, type = "l", lty = 2, col = "gray", lwd = 2, main = "The Law of Certainty: Case vs. Pallet", xlab = "Standard Deviations (z or t)", ylab = "Density", ylim = c(0, 0.5), xlim = c(-5, 5)) lines(x, dt(x, df = 11), col = "firebrick", lwd = 2) lines(x, dt(x, df = 143), col = "royalblue", lwd = 2) legend("topright", legend = c("Normal", "Single Case (df=11)", "Full Pallet (df=143)"), col = c("gray", "firebrick", "royalblue"), lty = c(2, 1, 1), lwd = 2, bty = "n", cex = 0.8)