1. Introduction to Hypothesis Testing

  • Objective: Evaluate if a population mean equals a specified value.
  • Use p-values to quantify evidence against the null.

2. Null and Alternative Hypotheses

  • Null hypothesis: \[H_0: \mu = \mu_0\]
  • Alternative hypothesis: \[H_a: \mu \neq \mu_0\]

3. Test Statistic Formula

  • For a one-sample t-test: \[ t = \frac{\bar x - \mu_0}{s / \sqrt{n}}, \quad s = \sqrt{\frac{1}{n-1}\sum_{i=1}^n (x_i - \bar x)^2} \]

4. Definition of p-Value

  • p-value: Probability under \(H_0\) of observing \(|T|\ge|t_{obs}|\): \[ p = P\bigl(|T_{n-1}| \ge |t_{obs}|\bigr) \]

5. Sample Distribution: Code Only

ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, fill = "steelblue", color = "white") +
  labs(title = "Histogram of MPG", x = "MPG", y = "Count")

6. Sample Distribution: Plot Only

7. t-Statistic Computation: Code Only

n <- nrow(mtcars)
df <- n - 1
t_obs <- (mean(mtcars$mpg) - 20) / (sd(mtcars$mpg) / sqrt(n))
t_vals <- seq(-4, 4, length.out = 400)
curve_df <- data.frame(
  t = t_vals,
  density = dt(t_vals, df)
)

8. Theoretical t-Distribution: Plot Only

9. Interactive Overlay: Code Only

graph_mins <- floor(min(mtcars$mpg))
graph_maxs <- ceiling(max(mtcars$mpg))
breaks_seq <- seq(graph_mins, graph_maxs, by = 2)
hist_data <- hist(mtcars$mpg, breaks = breaks_seq, plot = FALSE)
df_hist <- data.frame(mid = hist_data$mids, count = hist_data$counts)
scale_factor <- max(df_hist$count) / max(curve_df$density)
scaled_density <- curve_df$density * scale_factor
fig <- plot_ly(df_hist, x = ~mid, y = ~count, type = 'bar', name = 'Sample') %>%
  add_lines(x = curve_df$t * (breaks_seq[2] - breaks_seq[1]) + mean(breaks_seq),
            y = scaled_density,
            name = 'Scaled t-Distribution', inherit = FALSE)
fig

10. Interactive Overlay: Plot Only

11. R Code: Conducting the t-Test

test <- t.test(mtcars$mpg, mu = 20)
print(test)
## 
##  One Sample t-test
## 
## data:  mtcars$mpg
## t = 0.08506, df = 31, p-value = 0.9328
## alternative hypothesis: true mean is not equal to 20
## 95 percent confidence interval:
##  17.91768 22.26357
## sample estimates:
## mean of x 
##  20.09062

12. Interpretation & Conclusion

  • p = 0.933
  • If p < 0.05, reject \(H_0\) at 5% level.
  • The data do not provide evidence that mean MPG differs from 20.

13. Next Steps

  • Check diagnostic plots for normality and homoscedasticity.
  • Explore one-sided tests or different \(\mu_0\).
  • Extend analysis to two-sample or paired designs.