22/09/2024
Let’s consider a simple example:
H₀: μ = 100 (The population mean is equal to 100) H₁: μ ≠ 100 (The population mean is not equal to 100)
The formula for the z-test statistic is:
\[ z = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}} \]
Where: - \(\bar{x}\) is the sample mean - \(\mu_0\) is the hypothesized population mean - \(\sigma\) is the population standard deviation - \(n\) is the sample size
# Simulating data
set.seed(123)
sample_data <- rnorm(100, mean = 102, sd = 10)
# Calculating z-statistic
z_stat <- (mean(sample_data) - 100) /
(sd(sample_data) / sqrt(length(sample_data)))
# Calculating p-value
p_value <- 2 * (1 - pnorm(abs(z_stat)))
cat("Z-statistic:", round(z_stat, 4), "\n")
## Z-statistic: 3.1814
cat("P-value:", round(p_value, 4))
## P-value: 0.0015
In our example: - p-value (0.0469) < α (0.05) - Therefore, we reject the null hypothesis
| Decision | H0_True | H0_False |
|---|---|---|
| Fail to Reject H₀ | Correct Decision | Type II Error |
| Reject H₀ | Type I Error | Correct Decision |
The power of a statistical test is the probability of correctly rejecting a false null hypothesis.
\[ \text{Power} = 1 - \beta \]
Where β is the probability of a Type II error.
Let’s simulate an A/B test for email marketing campaigns:
# Simulating click-through rates for two email versions
set.seed(456)
version_a <- rbinom(1000, 1, 0.05)
version_b <- rbinom(1000, 1, 0.07)
# Performing chi-square test
chi_test <- chisq.test(table(c(version_a, version_b),
c(rep("A", 1000), rep("B", 1000))))
print(chi_test)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table(c(version_a, version_b), c(rep("A", 1000), rep("B", 1000)))
## X-squared = 0.21823, df = 1, p-value = 0.6404