- A statistical method used to make decisions using data.
- Involves a null hypothesis (\(H_0\)) and an alternative hypothesis (\(H_1\)).
- Uses sample data to determine if there is enough evidence to reject \(H_0\).
We want to test if the average height of a population is 170 cm.
\(H_0: \mu = 170\)
\(H_1: \mu \ne 170\)
set.seed(88) sample_heights <- rnorm(30, mean = 172, sd = 5) test_result <- t.test(sample_heights, mu = 170) cat(paste( "t-statistic =", round(test_result$statistic, 3), "|", "p-value =", round(test_result$p.value, 5)))
## t-statistic = 1.46 | p-value = 0.15499
ggplot(data.frame(x = sample_heights), aes(x = x)) +
geom_histogram(color = "black", fill = "skyblue", bins = 10) +
geom_vline(xintercept = 170, color = "red", linetype = "dashed") +
labs(title = "Histogram of Sample Heights", x = "Height (cm)",
y ="Frequency") +
theme(plot.title = element_text(hjust = 0.5))
ggplot(data.frame(group = "Sample", value = sample_heights),
aes(x = group, y = value)) + geom_boxplot(fill = "lightgreen",
color = "darkgreen") + geom_hline(yintercept = 170, color = "red",
linetype = "dashed") +
labs(title = "Boxplot of Sample Heights", x = "", y = "Height (cm)") +
theme(plot.title = element_text(hjust = 0.5))
\[ t = \frac{\overline{x} - \mu_0}{s/\sqrt{n}} \]
Where:
- \(\overline{x}\) is the sample mean
- \(\mu_0\) is the hypothesized mean
- \(s\) is the sample standard deviation
- \(n\) is the sample size
This 3D surface shows how the shape of the t-distribution changes with degrees of freedom.
The p-value helps us decide if the difference we observe is likely due to chance.
For a two-sided t-test, the formula is:
\[ p = 2 \cdot P(T \ge |t|) \]
Where:
This formula gives us the total probability of seeing a result as extreme as ours, in either direction, if the null hypothesis is true.
Smaller p-value → stronger evidence against \(H_0\).
x_bar <- mean(sample_heights) s <- sd(sample_heights) n <- length(sample_heights) t_stat <- (x_bar - 170) / (s / sqrt(n)) t_stat
## [1] 1.460168
df <- n - 1 p_val <- 2 * pt(-abs(t_stat), df) p_val
## [1] 0.1549935