2026-09-11

What is Hypothesis Testing

  • the theory, methods, and practice of testing a hypothesis by comparing it with the null hypothesis.
  • It start with the null hypothesis, or the assumption
  • Then data is collected and then checked for consistency with that assumption.
  • If the data strongly disagrees with the assumption, then the assumption is rejected.

Null vs. Alternative Hypothesis

  • A hypothesis starts with two competing claims: \[H_0: \mu = \mu_0\] \[H_1: \mu \neq \mu_0\]
  • \(H_0\) (null hypothesis): the original assumption, assumed to be true unless proven otherwise
  • \(H_1\) (alternative hypothesis): what we expect might actually be true

The Test Statistics

To test a hypothesis, a test statistic is calculated that measures how far a sample result is from what the null hypothesis predicted.

For a population mean test, the formula would show as so: \[t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}\] where:

  • \(\bar{x}\) = sample mean

  • \(s\) = sample standard deviation

  • \(n\) = sample size

  • \(\mu_0\) = the value claimed in the null hypothesis

Running a Hypothesis Test Using R

set.seed(42)
sample_data<- rnorm(30, mean=105, sd=15)

t.test(sample_data, mu=100)
## 
##  One Sample t-test
## 
## data:  sample_data
## t = 1.7541, df = 29, p-value = 0.08998
## alternative hypothesis: true mean is not equal to 100
## 95 percent confidence interval:
##   98.99927 113.05833
## sample estimates:
## mean of x 
##  106.0288

P-value = 0.09, it is above the common threshold of 0.05, meaning there is failure to reject the null hypothesis.

Visualizing the Rejection Region

- Chart shows distribution of sample data.

P-value vs Sample Size

n_seq <- seq(5, 100, by = 5)
diff <- 5
sd_val <- 15

t_stats <- diff / (sd_val / sqrt(n_seq))
p_vals <- 2 * (1 - pt(abs(t_stats), df = n_seq - 1))

df_plot <- data.frame(n = n_seq, p_value = p_vals)

plot_ly(df_plot, x = ~n, y = ~p_value, type = "scatter", mode = "lines+markers") %>%
  layout(
    title = "P-value vs. Sample Size",
    xaxis = list(title = "Sample Size (n)"),
    yaxis = list(title = "P-value")
  )

Note: This code generates an interactive plotly plot showing how p-value decreases as sample size increases. It renders correctly when run in RStudio, but isn’t displaying properly in the knitted HTML output.

Wrapping Up

  • Hypothesis testing lets us use sample data in order to make decisions regarding our hypotheses
  • We compare the assumption (null) with what we think might be true (alternative)
  • A test statistic will measure how far out data is from what is expected under the null hypothesis
  • The p-value tells us how surprising it would be if our null hypothesis turned out to be true
  • A larger sample size makes it easier to detect smaller true differences