March 17, 2024

Introduction to Hypothesis Testing

  • Null Hypothesis (H0): Represents the default assumption that there is no significant difference or relationship.
  • Alternative Hypothesis (H1 or Ha): States what we are trying to find evidence for, usually that there is a significant difference or relationship.

H0: μ = μ0 vs. H1: μ ≠ μ0

P-Value

  • P-Value: Probability of obtaining the observed data or more extreme results if the null hypothesis is true.
  • Interpretation: Small p-values (typically < 0.05) indicate strong evidence against the null hypothesis.

P-Value = P(Observed data | H0 is true)

Type I and Type II Errors

  • Type I Error (False Positive): Rejecting the null hypothesis when it is actually true.
  • Type II Error (False Negative): Failing to reject the null hypothesis when it is actually false.

\[ \begin{array}{|c|c|c|} \hline & \text{Null Hypothesis is True} & \text{Null Hypothesis is False} \\ \hline \text{Reject H0} & \text{Type I Error} & \text{Correct Decision} \\ \hline \text{Fail to Reject H0} & \text{Correct Decision} & \text{Type II Error} \\ \hline \end{array} \]

Example Data: mtcars dataset

# Load the mtcars dataset
data(mtcars)

# Show the first few rows of the dataset
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Research Question

model: \(\text{Volume} = \beta_0+\beta_1\cdot\text{Girth} + \varepsilon; \hspace{1 cm} \varepsilon \sim N(0; \sigma^2)\)
  fitted: \(\text{Volume} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Girth}\)          \(\hat{\beta}_0 = b_0 - \text{estimate of}\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of}\beta_1\)

# Research Question
cat("Research Question: Do cars with higher horsepower (hp) have significantly lower miles per gallon (mpg) compared to cars with lower horsepower?\n\n")
## Research Question: Do cars with higher horsepower (hp) have significantly lower miles per gallon (mpg) compared to cars with lower horsepower?
cat("H0: There is no significant difference in mpg between cars with higher and lower horsepower.\n")
## H0: There is no significant difference in mpg between cars with higher and lower horsepower.
cat("H1: Cars with higher horsepower have significantly lower mpg than cars with lower horsepower.")
## H1: Cars with higher horsepower have significantly lower mpg than cars with lower horsepower.

Perform Two Sample T-Test

model: \(\text{Volume} = \beta_0+\beta_1\cdot\text{Girth} + \varepsilon; \hspace{1 cm} \varepsilon \sim N(0; \sigma^2)\)
  fitted: \(\text{Volume} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Girth}\)          \(\hat{\beta}_0 = b_0 - \text{estimate of}\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of}\beta_1\)

# Separate the data into two groups based on horsepower
high_hp <- mtcars$mpg[mtcars$hp > median(mtcars$hp)]
low_hp <- mtcars$mpg[mtcars$hp <= median(mtcars$hp)]

# Perform two-sample t-test
t_test_result <- t.test(high_hp, low_hp)

t_test_result
## 
##  Welch Two Sample t-test
## 
## data:  high_hp and low_hp
## t = -6.2717, df = 25.345, p-value = 1.374e-06
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -11.710213  -5.923512
## sample estimates:
## mean of x mean of y 
##  15.40667  24.22353

T-Test Results

model: \(\text{Volume} = \beta_0+\beta_1\cdot\text{Girth} + \varepsilon; \hspace{1 cm} \varepsilon \sim N(0; \sigma^2)\)
  fitted: \(\text{Volume} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Girth}\)          \(\hat{\beta}_0 = b_0 - \text{estimate of}\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of}\beta_1\)

# Display T-Test Results
cat("Test Statistic:", t_test_result$statistic, "\n")
## Test Statistic: -6.271668
cat("P-Value:", t_test_result$p.value, "\n")
## P-Value: 1.373725e-06
cat("Conclusion:", ifelse(t_test_result$p.value < 0.05, "Reject the null hypothesis.", "Fail to reject the null hypothesis."))
## Conclusion: Reject the null hypothesis.

Histogram of mpg

model: \(\text{Volume} = \beta_0+\beta_1\cdot\text{Girth} + \varepsilon; \hspace{1 cm} \varepsilon \sim N(0; \sigma^2)\)
  fitted: \(\text{Volume} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Girth}\)          \(\hat{\beta}_0 = b_0 - \text{estimate of}\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of}\beta_1\)

# Generate histogram of mpg for high and low horsepower cars
library(ggplot2)

ggplot(mtcars, aes(x = mpg, fill = factor(ifelse(hp > median(hp), "High HP", "Low HP")))) +
  geom_histogram(binwidth = 3, alpha = 0.7, position = "identity") +
  labs(x = "Miles per Gallon (mpg)", y = "Frequency", title = "Histogram of mpg for High and Low HP Cars")

Boxplot of mpg

model: \(\text{Volume} = \beta_0+\beta_1\cdot\text{Girth} + \varepsilon; \hspace{1 cm} \varepsilon \sim N(0; \sigma^2)\)
  fitted: \(\text{Volume} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Girth}\)          \(\hat{\beta}_0 = b_0 - \text{estimate of}\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of}\beta_1\)

# Generate boxplot of mpg for high and low horsepower cars
ggplot(mtcars, aes(x = factor(ifelse(hp > median(hp), "High HP", "Low HP")), y = mpg, fill = factor(ifelse(hp > median(hp), "High HP", "Low HP")))) +
  geom_boxplot() +
  labs(x = "Horsepower Category", y = "Miles per Gallon (mpg)", title = "Boxplot of mpg for High and Low HP Cars")

Scatterplot of mpg vs. hp

model: \(\text{Volume} = \beta_0+\beta_1\cdot\text{Girth} + \varepsilon; \hspace{1 cm} \varepsilon \sim N(0; \sigma^2)\)
  fitted: \(\text{Volume} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Girth}\)          \(\hat{\beta}_0 = b_0 - \text{estimate of}\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of}\beta_1\)

# Generate scatter plot of mpg vs. hp
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(color = "blue") +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  labs(x = "Horsepower (hp)", y = "Miles per Gallon (mpg)", title = "Scatter Plot of mpg vs. hp")
## `geom_smooth()` using formula = 'y ~ x'

Latex Slide

## Hypothesis Testing Equations:
## Null Hypothesis (H_0): μ_1 = μ_2
## Alternative Hypothesis (H_1): μ_1 ≠ μ_2
## t = \frac{(\bar{x}_1 - \bar{x}_2)}{\sqrt{(s_1^2/n_1) + (s_2^2/n_2)}}
## Degrees of Freedom (df) = n_1 + n_2 - 2
## P-Value (p) = P(|T| > |t|)