Statistical Inference & Hypothesis Testing

broom (tidy, glance, augment), report, gtsummary::tbl_summary(), rstatix (pipe-friendly tests), ggstatsplot (plots with stats).

27.1 Confidence intervals

t.test(x)$conf.int
prop.test(45, 100)$conf.int
binom.test(45, 100)$conf.int

27.2 Common tests

t.test(x, mu = 0) Two independent means t.test(y ~ group, data = df) t.test(before, after, paired = TRUE) aov(y ~ group, df), oneway.test() Two medians (non-parametric) Mann-Whitney / Wilcoxon wilcox.test(y ~ group) ≥3 groups non-parametric kruskal.test(y ~ group) z / chi-square test prop.test(c(45, 60), c(100, 100)) chisq.test(table(a, b)) Fisher's exact fisher.test(table(a, b)) Pearson / Spearman / Kendall cor.test(x, y, method = "spearman")

shapiro.test(x), qqnorm(x); qqline(x) F / Levene / Bartlett

var.test(), car::leveneTest(), bartlett.test() Goodness of fit chi-square / KS

chisq.test(obs, p = probs), ks.test(x, "pnorm")

res <- t.test(body_mass_g ~ sex, data = penguins)
res$p.value; res$estimate; res$statistic
broom::tidy(res)                    # tidy data frame of test results

fit <- aov(body_mass_g ~ species, data = penguins)
summary(fit); TukeyHSD(fit)

27.3 Multiple testing, power, effect sizes

p.adjust(c(0.01, 0.04, 0.03), method = "BH")     # "bonferroni", "holm", "fdr"
power.t.test(delta = 0.5, sd = 1, power = 0.8)  # required n per group
pwr::pwr.anova.test(k = 3, f = 0.25, power = 0.8)
effectsize::cohens_d(body_mass_g ~ sex, data = penguins)

27.4 Permutation tests & resampling inference

obs <- diff(tapply(df$y, df$g, mean))
perm <- replicate(5000, diff(tapply(df$y, sample(df$g), mean)))
mean(abs(perm) >= abs(obs))     # p-value
# infer package: specify() |> hypothesize() |> generate() |> calculate()

27.5 Reporting

broom (tidy, glance, augment), report, gtsummary::tbl_summary(), rstatix (pipe-friendly tests), ggstatsplot (plots with stats).