2024-10-15

Introduction to Hypothesis Testing

  1. Hypothesis testing is a fundamental concept in statistics used to make inferences about a population based on sample data.

  2. It involves making a claim (hypothesis) and then using statistical techniques to determine if there is enough evidence in the sample to reject that claim.

Key Terms and Concepts

Hypothesis
A statement about a population parameter (such as a mean, proportion or standard deviation) that can be tested using data. There are two types of hypotheses in hypothesis testing:

\[ H_0: \text{Null Hypothesis} \] This is the default or initial assumption. It usually states that there is no effect or no difference.

\[ H_1: \text{Alternative Hypothesis} \] This is what you want to prove. It typically represents the presence of an effect, difference, or relationship.

Concepts of Hypothesis Testing (Continued)

\[C: \text{Level of Confidence}\] How confident we want to be in our results. Denoted by a percentage level like 95%, 99%, etc.

\[\alpha: \text{Level of Significance}\] The threshold for rejecting H₀ (commonly 0.05). \[ \alpha = 1 - C\] If you want to be 95% confident in your results, your alpha value would be \[ \alpha = 1 - 0.95 = 0.05\]

Types of Alternative Hypothesis - One Tailed

Testing for an effect in a specific direction (e.g., H₁: μ > μ₀).

Types of Alternative Hypothesis - Two Tailed

Testing for an effect in either direction (e.g., H₁: μ ≠ μ₀).
 
The red shaded region represents the area in which the z-statistic has to fall in order to reject the null hypothesis at a 95% confidence interval.

Concepts (continued)

Z - Test Statistic \[\frac{\bar{x} - \mu}{\frac{\sigma}{\sqrt{n}}}\]
where \[\bar{x} = sample~mean\] \[\mu= population~parameter~in~hypotheses\] \[\sigma = population~standard~deviation\] \[\sqrt{n} = square~root~of~sample~size~(n)\]

Concepts (continued)

\[\text{P: Probability Value}\] The probability of obtaining a result at least as extreme as the observed result, assuming H₀ is true.
- Two-tailed test: The p-value is calculated as: \[\text{p-value} = 2 \times P(Z > |z|)\] This means we’re looking for the probability of observing a z-statistic as extreme as \(|z|\) or more in both directions.
- One-tailed test:
- If testing for a greater-than effect, the p-value is: \[\text{p-value} = P(Z > z) \] - If testing for a less-than effect, the p-value is: \[\text{p-value} = P(Z < z)\]

Steps in Hypothesis Testing

  1. State the Hypotheses.
  2. Choose a Significance Level.
  3. Collect Data and compute the test statistic.
  4. Calculate P-value or compare the test statistic with a critical value.
  5. Make a Decision: Reject or Fail to Reject H₀.

Example - Z-Test for Population Mean

Problem Statement:
We want to test whether the average height of a population is 170 cm, based on a sample.
Given:
Sample Mean \((\bar{x}) = 172\) cm
Population Standard Deviation \((\sigma) = 5\) cm
Sample Size \((n) = 30\)

Hypotheses:
Null Hypothesis (\(H_0\)): \(\mu = 170\) - The population mean height is 170 cm.
Alternative Hypothesis (\(H_1\)): \(\mu \neq 170\) - The population mean height is not 170 cm.

Significance Level:
Let’s set \(\alpha = 0.05\) (5%).

Example - Solution

Z-Test Calculation:
1. Calculate the z-statistic: \[ z = \frac{\bar{x} - \mu}{\frac{\sigma}{\sqrt{n}}} = \frac{172 - 170}{\frac{5}{\sqrt{30}}} \] 2. Find the P-value:
- For a two-tailed test, the P-value is: \[\text{P-value} = 2 \times P(Z > |z|)\]
- Compare this p-value to \(\alpha\):
If \(\text{p-value} \leq \alpha\), reject \(H_0\)
If \(\text{p-value} > \alpha\), fail to reject \(H_0\)

Example - Solution (continued)

Conclusion:
Based on the p-value, we conclude whether the average height is statistically different from 170 cm. Becuase the z-critical value lies in the rejection rejion we reject the Hull Hypothesis.

Power of a Test and its Relation to Type I and Type II Errors

Power of a Test:
- The power of a test is the probability of correctly rejecting the null hypothesis \(H_0\) when it is actually false.
- Formula: Power \(= 1 - \beta\), where \(\beta\) is the probability of a Type II error.

Type I and Type II Errors:
- Type I Error (\(\alpha\)): Rejecting \(H_0\) when it is actually true.
This is the significance level of the test, chosen before conducting the test (e.g., 0.05).
- Type II Error (\(\beta\)): Failing to reject \(H_0\) when it is actually false.
Power increases as \(\beta\) decreases, meaning fewer Type II errors.

Relationship Between Power and Errors:
- Increasing power reduces the chance of a Type II error (\(\beta\)).
- Higher significance levels (larger \(\alpha\)) increase power but also increase the likelihood of Type I errors.

Visual Representation of the Power

Visual Representation of Power and Errors

Visual Representation of Power and Errors Code

# Load ggplot2 for plotting
library(ggplot2)

# Define parameters
alpha <- 0.05       # Type I error rate
mu0 <- 0            # Mean under the null hypothesis
mu1 <- 2            # Mean under the alternative hypothesis
sigma <- 1          # Standard deviation
z_alpha <- qnorm(1 - alpha)  # Critical z-score

# Create data frames for the null and alternative distributions
x <- seq(-4, 6, length.out = 1000)
h0_dist <- data.frame(x = x, y = dnorm(x, mean = mu0, sd = sigma))
h1_dist <- data.frame(x = x, y = dnorm(x, mean = mu1, sd = sigma))

# Plot the distributions
ggplot() +
  geom_line(data = h0_dist, aes(x = x, y = y), color = "blue", size = 1) +
  geom_line(data = h1_dist, aes(x = x, y = y), color = "red", size = 1) +
  geom_vline(xintercept = z_alpha, linetype = "dashed", color = "purple") +
  geom_area(data = subset(h0_dist, x > z_alpha), aes(x = x, y = y), fill = "blue", alpha = 0.3) +
  geom_area(data = subset(h1_dist, x < z_alpha), aes(x = x, y = y), fill = "red", alpha = 0.3) +
  labs(title = "Power of a Test and Error Types",
       x = "Test Statistic",
       y = "Density") +
  annotate("text", x = z_alpha + 0.5, y = 0.05, label = "Type I Error", color = "blue") +
  annotate("text", x = z_alpha - 1, y = 0.05, label = "Type II Error (Beta)", color = "red") +
  theme_minimal()

Conclusion

  1. Hypothesis testing is a key tool in statistical inference.
  2. By understanding the steps and key concepts, we can draw meaningful conclusions from data.
  3. Always be aware of potential errors (Type I and II) and the limitations of your test.