Imagine you’re in an elementary school classroom and your teacher gives you a coin. The teacher says, “This coin is perfectly fair, meaning it has an equal chance of landing on heads or tails.”
Null Hypothesis (H₀):
The null hypothesis is like the default belief or starting assumption. It’s what you believe unless you have strong evidence to change your mind.
In our story, the null hypothesis would be: H₀: “The coin is fair, so it will land on heads about 50% of the time.”
This means you assume everything is normal or the same as expected (50% heads, 50% tails) until proven otherwise.
Alternate Hypothesis (H₁):
The alternate hypothesis is like the challenger or new idea. It’s what you’re trying to prove if you think the null hypothesis is wrong.
In our story, the alternate hypothesis would be: H₁: “The coin is not fair; it lands on heads more or less than 50% of the time.”
This means you’re suggesting that something different is happening—maybe the coin is unfair or biased.
Example:
Let’s flip the coin 10 times and count how many times it lands on heads:
If you get 5 heads and 5 tails, this supports your null hypothesis—the coin seems fair. But what if you get 9 heads and 1 tail? You might start thinking, “Hmm, maybe the coin isn’t fair after all.” This could support your alternate hypothesis.
In short:
Null hypothesis (H₀): Everything is as expected (the coin is fair).
Alternate hypothesis (H₁): Something is different (the coin is not fair).
We start by assuming the null hypothesis is true until we have enough evidence to support the alternate hypothesis.
Using R
Let’s use a simple example in R to test a hypothesis about the fairness of a coin using the binom.test function, which tests if the number of successes (heads) in a set of flips matches the expected probability.
Scenario: You flip a coin 100 times and get 60 heads. You want to test if the coin is fair, meaning the probability of heads is 0.5.
Here’s how you can do this in R:
Explanation of the code. -
n_heads: The number of times the coin landed on heads (in this case, 60) n_flips: The total number of flips (100) p = 0.5: This is the probability under the null hypothesis (the coin is fair, so it should land on heads 50% of the time) binom.test(): This function tests whether the observed number of heads (60) differs significantly from the expected number based on a 50% chance.
# Number of coin flips
n_flips <- 100
# Number of heads (successes)
n_heads <- 60
# Performing a binomial test
# The null hypothesis (H₀) is that the probability of heads is 0.5 (fair coin)
result <- binom.test(n_heads, n_flips, p = 0.5)
# Printing the result
print(result)
##
## Exact binomial test
##
## data: n_heads and n_flips
## number of successes = 60, number of trials = 100, p-value = 0.05689
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
## 0.4972092 0.6967052
## sample estimates:
## probability of success
## 0.6
Interpretation for the last test in R
p-value: 0.05766 (if you use a significance level of 0.05, this p-value is just above it, meaning you fail to reject the null hypothesis). In other words, you don’t have strong enough evidence to say the coin is unfair.
The coin might have landed on heads 60 times, but that result could still happen by chance if the coin is fair.
Conclusion with a Plot -
Based on this test, we fail to reject the null hypothesis, meaning we don’t have enough evidence to say the coin is unfair. So, for now, we assume the coin is fair.
# Load the ggplot2 library
library(ggplot2)
# Number of coin flips
n_flips <- 100
# Probability of heads for a fair coin
p_heads <- 0.5
# Observed number of heads
observed_heads <- 60
# Generate the binomial distribution for possible outcomes (0 to 100 heads)
binom_data <- data.frame(
heads = 0:n_flips,
probability = dbinom(0:n_flips, size = n_flips, prob = p_heads)
)
# Create the plot
ggplot(binom_data, aes(x = heads, y = probability)) +
geom_bar(stat = "identity", fill = "lightblue", color = "blue") +
geom_vline(xintercept = observed_heads, color = "red", linetype = "dashed", size = 1.5) +
labs(
title = "Binomial Distribution of 100 Coin Flips (p = 0.5)",
x = "Number of Heads",
y = "Probability"
) +
annotate("text", x = observed_heads + 2, y = 0.03, label = paste("Observed Heads =", observed_heads), color = "red") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Let’s continue with the coin example and show how the alternate hypothesis works in different scenarios.
Scenario: Testing if a coin is unfair
You believe a coin might be unfair and lands on heads more often than tails. You want to test this with an experiment.
You suspect that a coin is biased, and you want to test if it lands on heads more often than expected. A fair coin would land on heads 50% of the time, but you believe it lands on heads more than 50% of the time.
Null Hypothesis (H₀): The coin is fair, meaning the probability of getting heads is 0.5, p = 0.5.
Alternate Hypothesis (H₁): The coin is biased, and it lands on heads more than 50% of the time, p>0.5.
You flip the coin 100 times and get 60 heads. Now, you want to test the alternate hypothesis that the coin is biased towards heads (i.e., it lands on heads more than 50% of the time).
# Number of coin flips
n_flips <- 100
# Number of heads (successes)
n_heads <- 60
# Perform a binomial test for the alternate hypothesis (coin lands on heads more than 50% of the time)
result <- binom.test(n_heads, n_flips, p = 0.5, alternative = "greater")
# Print the result
print(result)
##
## Exact binomial test
##
## data: n_heads and n_flips
## number of successes = 60, number of trials = 100, p-value = 0.02844
## alternative hypothesis: true probability of success is greater than 0.5
## 95 percent confidence interval:
## 0.5129758 1.0000000
## sample estimates:
## probability of success
## 0.6
Output Explanation
The output will give you a p-value which helps you decide if the evidence supports your alternate hypothesis.
If p-value < 0.05: This means the result is unusual enough under the null hypothesis that you can reject the null hypothesis and accept the alternate hypothesis. In other words, the coin is probably unfair and lands on heads more than 50% of the time.
If p-value ≥ 0.05: This means the evidence isn’t strong enough, so you fail to reject the null hypothesis. The coin might still be fair.
Simplified Conclusion:
Null Hypothesis (H₀): The coin is fair (50% heads).
Alternate Hypothesis (H₁): The coin lands on heads more often than 50% (it’s biased toward heads)
Based on the p-value, you either reject or fail to reject the null hypothesis.