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