Introduction

In this presentation, we explore hypothesis testing - a fundamental statistical method for making decisions about population parameters based on sample data.

Key Questions Hypothesis Testing Can Answer:

  • Is the average house price greater than $400,000?
  • Does a new teaching method improve test scores?
  • Is there a difference in average wages between two groups?

The Process: We use sample data to either reject or fail to reject claims about the population with a specific level of confidence (e.g., 95%).

The Two Hypotheses

In hypothesis testing, we always have two competing hypotheses:

Null Hypothesis (\(H_0\)): The existing claim or status quo

\(H_0: \mu = \mu_0\)

Alternative Hypothesis (\(H_a\)): What we want to test

\(H_a: \mu \neq \mu_0 \quad \text{(two-tailed)}\)

\(H_a: \mu > \mu_0 \quad \text{or} \quad H_a: \mu < \mu_0 \quad \text{(one-tailed)}\)

Critical Rule: We never “accept” \(H_0\). We either reject \(H_0\) or fail to reject \(H_0\).

The Test Statistic

The t-statistic measures how many standard errors the sample mean is from the hypothesized population mean:

\[t = \frac{\bar{x} - \mu_0}{SE(\bar{x})} = \frac{\bar{x} - \mu_0}{s/\sqrt{n}}\]

Where:

  • \(\bar{x}\) = sample mean
  • \(\mu_0\) = hypothesized population mean
  • \(s\) = sample standard deviation
  • \(n\) = sample size
  • \(SE(\bar{x})\) = standard error of the mean

Decision Rules

Using Critical Values:

For a two-tailed test at significance level \(\alpha\):

\[\text{Reject } H_0 \text{ if } |t| > t_{\alpha/2}\]

For a one-tailed test:

\[\text{Reject } H_0 \text{ if } t > t_{\alpha} \text{ (or } t < -t_{\alpha})\]

Using P-values:

\[\text{Reject } H_0 \text{ if p-value} < \alpha\]

where \(\alpha\) is typically 0.05 (95% confidence) or 0.01 (99% confidence)

Visualizing Two-Tailed Test

  • ggplot() creates a plot of the t-distribution with 30 degrees of freedom.
  • geom_area() highlights the rejection regions in both tails (shaded yellow).
  • geom_vline() marks the critical values at \(\pm 1.96\) for \(\alpha = 0.05\).
  • ggplotly() converts the static ggplot into an interactive Plotly visualization.

Key Concept: If the test statistic falls in the shaded regions, we reject \(H_0\).

x <- seq(-4, 4, length.out = 1000)
y <- dt(x, df = 30)
df <- data.frame(x = x, y = y)

p <- ggplot(df, aes(x = x, y = y)) +
  geom_line(color = "#8C1D40", linewidth = 1.3) +
  geom_area(data = subset(df, x < -1.96), fill = "#FFC627", alpha = 0.6) +
  geom_area(data = subset(df, x > 1.96), fill = "#FFC627", alpha = 0.6) +
  geom_vline(xintercept = c(-1.96, 1.96), linetype = "dashed", 
             color = "red", linewidth = 1) +
  labs(title = "Two-Tailed Test: Rejection Regions (α = 0.05)",
       x = "t-statistic", y = "Density") +
  theme_classic()

ggplotly(p) %>% layout(margin = list(l = 50, r = 50, b = 50, t = 80))

Example: Testing Average Temperature

Research Question: Is the average daily maximum temperature in May equal to 100°F?

Model: \(H_0: \mu = 100\) vs. \(H_a: \mu \neq 100\); \(\alpha = 0.05\)

  • xbar, s, and n are the sample mean, standard deviation, and sample size.
  • t_stat calculates the t-statistic using the formula.
  • t_crit finds the critical value from the t-distribution.
  • p_value calculates the probability of observing such extreme data if \(H_0\) is true.
# Given data
n <- 30; xbar <- 92; s <- 10; mu_0 <- 100; alpha <- 0.05

# Calculate t-statistic
se <- s / sqrt(n)
t_stat <- (xbar - mu_0) / se

# Critical value (two-tailed)
t_crit <- qt(1 - alpha/2, df = n - 1)

# P-value
p_value <- 2 * pt(t_stat, df = n - 1)

cat("Sample mean:", xbar, "°F\n")
Sample mean: 92 °F
cat("t-statistic:", round(t_stat, 3), "\n")
t-statistic: -4.382 
cat("Critical value: ±", round(t_crit, 3), "\n")
Critical value: ± 2.045 
cat("P-value:", round(p_value, 5), "\n")
P-value: 0.00014 
cat("Decision:", ifelse(abs(t_stat) > t_crit, "Reject H₀", "Fail to reject H₀"))
Decision: Reject H₀

Interactive 3D: Critical Values Surface

  • alpha_seq and n_seq create sequences for significance levels and sample sizes.
  • T_crit is a matrix storing critical t-values for each combination of \(\alpha\) and \(n\).
  • plot_ly() with type = "surface" creates a 3D surface plot.
  • colorscale defines the color gradient (maroon to gold to blue).
  • scene with camera adjusts the 3D viewing angle for better perspective.

Tip: Rotate the plot to see how critical values decrease as sample size increases!

Statistical Power Analysis

  • Power (\(1 - \beta\)) is the probability of correctly rejecting a false null hypothesis.
  • effect_sizes represent different standardized differences (Cohen’s d).
  • Loop calculates power for each combination of sample size and effect size.
  • geom_hline() shows the conventional power threshold of 0.80.
  • Larger effect sizes require smaller sample sizes to achieve adequate power.

Real Example: House Prices

Question: Did house prices differ between 2018 and 2019 in Maricopa County?

Model: \(H_0: \mu_{2018} - \mu_{2019} = 0\) vs. \(H_a: \mu_{2018} - \mu_{2019} \neq 0\)

  • rnorm() generates simulated realistic housing price data.
  • Two-sample t-test compares means from two independent groups.
  • Welch-Satterthwaite formula calculates adjusted degrees of freedom.
  • p_value determines if the difference is statistically significant.
# Simulate realistic housing data
set.seed(456)
n1 <- 250; n2 <- 280

prices_2018 <- rnorm(n1, mean = 325000, sd = 75000)
prices_2019 <- rnorm(n2, mean = 340000, sd = 78000)

# Two-sample t-test calculations
xbar1 <- mean(prices_2018); xbar2 <- mean(prices_2019)
s1 <- sd(prices_2018); s2 <- sd(prices_2019)

se_diff <- sqrt(s1^2/n1 + s2^2/n2)
t_stat <- (xbar2 - xbar1) / se_diff

df <- (s1^2/n1 + s2^2/n2)^2 / ((s1^2/n1)^2/(n1-1) + (s2^2/n2)^2/(n2-1))
p_value <- 2 * pt(-abs(t_stat), df = df)

cat("2018 mean: $", format(round(xbar1), big.mark = ","), "\n", sep = "")
2018 mean: $327,403
cat("2019 mean: $", format(round(xbar2), big.mark = ","), "\n", sep = "")
2019 mean: $350,450
cat("Difference: $", format(round(xbar2 - xbar1), big.mark = ","), "\n", sep = "")
Difference: $23,048
cat("t-statistic:", round(t_stat, 3), "\n")
t-statistic: 3.531 
cat("p-value:", round(p_value, 4), "\n")
p-value: 5e-04 
cat("Conclusion:", ifelse(p_value < 0.05, 
    "Reject H₀: Significant difference", 
    "Fail to reject H₀: No significant difference"))
Conclusion: Reject H₀: Significant difference

Visualizing Two-Sample Comparison

  • housing_data combines both years’ data into a single data frame.
  • geom_boxplot() displays distributions with quartiles and outliers.
  • stat_summary() adds red diamonds showing mean values.
  • scale_fill_manual() uses ASU colors (maroon and gold).
  • Hover over the plot to see detailed statistics for each group!