March 14, 2024

Introduction to Hypothesis Testing

Introduction to Hypothesis Testing Hypothesis testing is a statistical method used to make inferences about population parameters based on sample data. It helps us determine whether observed differences or associations in the data are statistically significant or due to random variation.

Null and Alternative Hypotheses

Null and Alternative Hypotheses Null Hypothesis (H0): The default assumption that there is no effect or no difference in the population parameters. Alternative Hypothesis (Ha): The statement that contradicts the null hypothesis and suggests there is a significant effect or difference.

Example Hypothesis Test

Example Hypothesis Test Let’s conduct a hypothesis test to compare the mean petal length of the “setosa” species to a hypothetical value.

Null Hypothesis (H0): The mean petal length of “setosa” species is equal to 1.5. Alternative Hypothesis (Ha): The mean petal length of “setosa” species is not equal to 1.5.

# Perform hypothesis test
test <- t.test(iris$Petal.Length[iris$Species == "setosa"], mu = 1.5)
test
    One Sample t-test

data:  iris$Petal.Length[iris$Species == "setosa"]
t = -1.5472, df = 49, p-value = 0.1282
alternative hypothesis: true mean is not equal to 1.5
95 percent confidence interval:
 1.412645 1.511355
sample estimates:
mean of x 
    1.462 

Test Statistic and P-value

Test Statistic and P-value Test Statistic: The measure calculated from sample data that helps us assess the evidence against the null hypothesis. P-value: The probability of observing a test statistic as extreme as or more extreme than the one calculated, assuming the null hypothesis is true.

Decision Rule

Decision Rule Compare the p-value to the significance level (alpha) to make a decision: If p-value < alpha: Reject the null hypothesis. If p-value ≥ alpha: Fail to reject the null hypothesis.

Example Visualization

# Create a histogram of petal lengths for "setosa" species
ggplot(data = iris[iris$Species == "setosa", ], aes(x = Petal.Length)) +
  geom_histogram(binwidth = 0.2, fill = "lightblue", color = "black") +
  labs(x = "Petal Length", y = "Frequency", title = "Distribution of Petal Lengths (setosa)")

Scatter plot of Sepal length vs. Petal length

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
  geom_point() +
  labs(x = "Sepal Length", y = "Petal Length", title = "Sepal Length vs. Petal Length")

3D Scatter plot of Sepal Length, Sepal Width, and Petal Width

plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, z = ~Petal.Width, color = ~Species,
        type = "scatter3d", mode = "markers") %>%
  layout(scene = list(xaxis = list(title = "Sepal Length"),
                      yaxis = list(title = "Sepal Width"),
                      zaxis = list(title = "Petal Width")),
         title = "3D Scatter Plot of Sepal Length, Sepal Width, and Petal Width")

Hypothesis Testing

\[ \text{Null Hypothesis (H}_0\text{): } \mu_1 = \mu_2 \quad \text{vs.} \quad \text{Alternative Hypothesis (H}_a\text{): } \mu_1 \neq \mu_2 \]

\[ t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}} \quad \text{vs.} \quad t > c \quad \text{or} \quad t < -c \]

Null Hypothesis (H0) and Alternative Hypothesis (Ha): - The null hypothesis, denoted as \(H_0\), represents the default assumption that there is no significant difference or effect. - The alternative hypothesis, denoted as \(H_a\), proposes a specific difference, effect, or relationship between variables.

  • Test Statistic (t) and Critical Value (c):
    • The test statistic \(t\) quantifies the difference between sample means relative to the variability within the samples.
    • It is compared to a critical value \(c\) determined from a statistical distribution, such as the t-distribution, to assess significance.

Hypothesis Testing Continued

\[ \text{P-value} = P(|t| > |t_{\text{observed}}|) \]

P-value Calculation: - The p-value measures the strength of evidence against the null hypothesis. - It represents the probability of observing a test statistic as extreme as the one obtained, assuming the null hypothesis is true. - A small p-value (< 0.05) suggests strong evidence against the null hypothesis, leading to its rejection.

  • Interpretation:
    • If the p-value is less than the chosen significance level (often denoted as \(\alpha\)), typically 0.05, the null hypothesis is rejected.
    • This implies that there is sufficient evidence to support the alternative hypothesis.
    • If the p-value is greater than \(\alpha\), the null hypothesis is not rejected, indicating insufficient evidence to support the alternative hypothesis.