2025-10-19

Introduction

What is Hypothesis Testing?

  • Hypothesis Testing is a statistical method used to draw conclusions about a population based on sample data.

Why is it important?

  • It helps us evaluate whether our sample data provides sufficient evidence to support or reject a claim.

What are the key steps?

  1. Form null and alternative hypothesis
  2. Collect data
  3. Perform an appropriate statistical test
  4. Decide whether to reject or fail to reject your null hypothesis

1. State the Hypotheses

Null Hypothesis (\(H_0\)):

  • The default assumption. The null hypothesis is assumed to be true unless there is evidence to prove that it’s not.

Alternative Hypothesis (\(H_1\)):

  • A claim that contradicts the null hypothesis

Example:

Testing if a new medicine lowers blood pressure:

  • \(H_0\): The drug has no effect on blood pressure (\(\mu = 120\) mmHg)

  • \(H_1\): The drug lowers blood pressure (\(\mu < 120\) mmHg)

2. Collect Sample Data

  • For a statistical test to produce valid results, it is critical that sampling and data collection are carefully designed to test your specific hypothesis.

  • If your sample data does not accurately represent the target population, then any statistical inferences or conclusions you draw about that population will be unreliable and potentially misleading.

3. Calculate Test Statistic

A test statistic is a value that is calculated from the sample data and used in hypothesis testing to decide whether or not to reject the null hypothesis.

Types of test statistics include:

  • z-test
  • t-test
  • F-test
  • \(\chi^2\)-test

Different test statistics are used depending on the type of data, sample size, and what hypothesis you’re testing.

For the purposes of this presentation, we will be using z-test.

z-test

You should use a z-test when:

  • You are comparing sample means to a population mean

  • The population standard deviation is known

Formula: \[z = \frac{\bar{x} - \mu}{\sigma / \sqrt{n}}\]

\(\bar{x}\) - sample mean
\(\mu\) - population mean
\(\sigma\) - population standard deviation
\(n\) - sample size

4. Interpret Results

p-value

  • The p-value is the probability of obtaining results as extreme as the observed data, assuming the null hypothesis is true.
  • In other words, the p-value tells you how likely it is to observe your sample data if \(H_0\) is true.

To make a conclusion, we compare p-value with \(\alpha\) (significance level).

  • If p-value < \(\alpha\), we reject \(H_0\) => The results are significant
  • If p-value \(\geq \alpha\), we fail to reject \(H_0\) => The results are not significant

Example: Iris dataset in R

This dataset consists of measurements of iris flowers belonging to 3 different species: Setosa, Versicolor, and Virginica.

data(iris)
head(iris, 2)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
tail(iris, 2)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica

Scatterplot: Iris Measurements (plotly)

# plotly for scatterplot
plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, z = ~Petal.Width,
        type = "scatter3d", mode = "markers",
        color = ~Species,
        colors = c('purple', 'mediumpurple', 'steelblue'),
        marker = list(size = 4)) %>%
 layout(title = "3D Visualization of Iris Measurements",
         scene = list(xaxis = list(title = "Sepal Length"),
                      yaxis = list(title = "Petal Length"),
                      zaxis = list(title = "Petal Width")))

Scatterplot: Iris Measurements (plotly)

Boxplot: Petal Length by Species (ggplot)

# ggplot for boxplot
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_boxplot() +
  labs(title = "Sepal Length by Species",
       x = "Species",
       y = "Sepal Length") +
  theme_minimal() +
  scale_fill_manual(values = c("purple", "mediumpurple", "steelblue"))

Boxplot: Petal Length by Species (ggplot)

Histogram: Distribution of Petal Lengths (ggplot)

# ggplot for histogram
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_boxplot() +
  labs(title = "Sepal Length by Species",
       x = "Species",
       y = "Sepal Length") +
  theme_minimal() +
  scale_fill_manual(values = c("purple", "mediumpurple", "steelblue"))

Histogram: Distribution of Petal Lengths (ggplot)

Example: Calculating Z-Statistic for Setosa

Using the formula: \(z = \frac{\bar{x} - \mu}{\sigma / \sqrt{n}}\)

# Population parameters 
pop_mean <- mean(iris$Sepal.Length)  
pop_sd <- sd(iris$Sepal.Length)     

# Setosa sample
setosa <- iris %>% filter(Species == "setosa")
sample_mean <- mean(setosa$Sepal.Length)  
n <- nrow(setosa)                         

# Calculate z-statistic
z_stat <- (sample_mean - pop_mean) / (pop_sd / sqrt(n))

cat("z =", round(z_stat, 3))
## z = -7.15

Conclusion

With \(\alpha = 0.05\) and z = -7.14, we reject \(H_0\).

Setosa sepal length is significantly different from the population mean.

References