IMS, Ch. 11
Smith College
Apr 6, 2026
Recall our first day together
Recall the yawning data
library(tidyverse) library(openintro) library(infer) null_dist <- yawn |> specify(result ~ group, success = "yawn") |> hypothesize(null = "independence") |> generate(reps = 1000) |> summarize( trmt_yawners = sum(result == "yawn" & group == "trmt"), .groups = "drop" )
null_dist |> ggplot(aes(x = trmt_yawners)) + geom_histogram() + geom_vline(xintercept = 10, color = "red", type = 3)
library(janitor) yawn |> tabyl(result, group) |> adorn_totals(c("row", "col"))
result ctrl trmt Total not yawn 12 24 36 yawn 4 10 14 Total 16 34 50
two_way <- yawn |> group_by(group) |> summarize(n = n(), prop = mean(result == "yawn")) two_way
# A tibble: 2 × 3 group n prop <fct> <int> <dbl> 1 ctrl 16 0.25 2 trmt 34 0.294
dp_hat <- two_way |> pull(prop) |> diff() dp_hat
[1] 0.04411765
null_dist <- yawn |> specify(result ~ group, success = "yawn") |> hypothesize(null = "independence") |> generate(reps = 1000, type = "permute") |> calculate("diff in props", order = c("trmt", "ctrl"))
null_dist |> ggplot(aes(x = stat)) + geom_density(fill = "darkgray") + geom_vline(xintercept = 0, linetype = 3) + geom_vline(xintercept = dp_hat, color = "red", linetype = 2)
dp_hat
null_dist |> get_p_value(obs_stat = dp_hat, direction = "two-sided")
# A tibble: 1 × 1 p_value <dbl> 1 0.994
See Randomization Test handout