The Goodness of Fit test is a statistical procedure used to examine whether the observed frequency distribution of a categorical variable agrees with a specified theoretical distribution.
The Chi-square goodness of fit test is commonly used for this purpose.
For example:
Suppose a random sample is classified into \(k\) categories.
Let:
The test statistic is:
\[ \chi^2 = \sum_{i=1}^{k} \frac{(O_i-E_i)^2}{E_i} \]
Under the null hypothesis, the test statistic approximately follows a Chi-square distribution.
The degrees of freedom are:
\[ df = k-1 \]
This applies when the category probabilities are completely specified and no distribution parameters are estimated from the sample.
Null hypothesis (\(H_0\)):
The observed frequencies follow the specified theoretical distribution.
Alternative hypothesis (\(H_1\)):
The observed frequencies do not follow the specified theoretical distribution.
At a 5% level of significance:
Suppose a coin is tossed 100 times.
The observed frequencies are:
| Outcome | Observed frequency |
|---|---|
| Head | 56 |
| Tail | 44 |
| Total | 100 |
We want to examine whether the coin is fair.
\[ H_0: P(H)=P(T)=0.5 \]
\[ H_1: \text{The coin is not fair} \]
For a fair coin:
\[ E(H)=100 \times 0.5=50 \]
\[ E(T)=100 \times 0.5=50 \]
# Observed frequencies
observed <- c(56, 44)
# Expected frequencies
expected <- c(50, 50)
# Chi-square test statistic
chi_square <- sum((observed-expected)^2/expected)
chi_square
## [1] 1.44
# Degrees of freedom
df <- length(observed)-1
# Calculate p-value
p_value <- pchisq(chi_square,
df = df,
lower.tail = FALSE)
p_value
## [1] 0.2301393
alpha <- 0.05
if(p_value < alpha){
print("Reject the null hypothesis")
} else {
print("Do not reject the null hypothesis")
}
## [1] "Do not reject the null hypothesis"
R provides a built-in function called chisq.test().
chisq.test(observed, p = c(0.5, 0.5))
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 1.44, df = 1, p-value = 0.2301
The output provides:
If the p-value is greater than 0.05, we do not have sufficient evidence to conclude that the coin is unfair.
One of the famous examples of statistical reasoning in biology comes from Gregor Mendel’s experiments on pea plants.
According to the law of segregation, a monohybrid cross is expected to produce a phenotypic ratio of:
\[ 3:1 \]
That is:
Suppose an experiment produces 160 offspring, with the following observed frequencies.
| Phenotype | Observed frequency |
|---|---|
| Dominant | 120 |
| Recessive | 40 |
| Total | 160 |
We want to examine whether these observations agree with the theoretical 3:1 ratio.
\[ H_0: \text{The phenotypic ratio is } 3:1 \]
\[ H_1: \text{The phenotypic ratio is not } 3:1 \]
The total ratio is:
\[ 3+1=4 \]
Expected frequency of dominant phenotype:
\[ E_1=160\times\frac{3}{4}=120 \]
Expected frequency of recessive phenotype:
\[ E_2=160\times\frac{1}{4}=40 \]
# Observed frequencies
observed <- c(120, 40)
# Theoretical probabilities
probability <- c(3/4, 1/4)
# Expected frequencies
expected <- sum(observed)*probability
expected
## [1] 120 40
chi_square <- sum((observed-expected)^2/expected)
chi_square
## [1] 0
result <- chisq.test(observed, p = probability)
result
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 0, df = 1, p-value = 1
if(result$p.value < 0.05){
print("Reject H0: The observed data do not fit the 3:1 ratio")
} else {
print("Do not reject H0: The observed data are consistent with the 3:1 ratio")
}
## [1] "Do not reject H0: The observed data are consistent with the 3:1 ratio"
The p-value indicates whether the observed frequencies provide sufficient evidence against the theoretical 3:1 ratio.
A die is rolled 120 times.
The observed frequencies are given below.
observed <- c(25, 18, 22, 17, 20, 18)
names(observed) <- c("1", "2", "3", "4", "5", "6")
observed
## 1 2 3 4 5 6
## 25 18 22 17 20 18
We want to examine whether the die is unbiased.
\[ H_0: P(1)=P(2)=\cdots=P(6)=\frac{1}{6} \]
\[ H_1: \text{At least one probability differs from } \frac{1}{6} \]
# Equal theoretical probabilities
probability <- rep(1/6, 6)
# Goodness of fit test
result <- chisq.test(observed, p = probability)
result
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 2.3, df = 5, p-value = 0.8063
result$expected
## 1 2 3 4 5 6
## 20 20 20 20 20 20
if(result$p.value < 0.05){
print("Reject the null hypothesis")
} else {
print("Do not reject the null hypothesis")
}
## [1] "Do not reject the null hypothesis"
If the p-value is less than 0.05, we conclude that the observed frequencies differ significantly from the expected frequencies of an unbiased die.
Otherwise, there is insufficient evidence to conclude that the die is biased.
We can use a bar plot to compare observed and expected frequencies.
observed <- c(25, 18, 22, 17, 20, 18)
expected <- rep(20, 6)
barplot(rbind(observed, expected),
beside = TRUE,
names.arg = 1:6,
col = c("skyblue", "orange"),
main = "Observed and Expected Frequencies",
xlab = "Outcome of die",
ylab = "Frequency")
legend("topright",
legend = c("Observed", "Expected"),
fill = c("skyblue", "orange"))
The Chi-square goodness of fit test has the following important assumptions:
If expected frequencies are too small, categories may need to be combined when scientifically appropriate, or an exact or simulation-based method may be considered.
| Function | Purpose |
|---|---|
chisq.test() |
Performs Chi-square goodness of fit test |
pchisq() |
Calculates Chi-square distribution probabilities |
qchisq() |
Calculates Chi-square distribution quantiles |
sum() |
Calculates sum |
length() |
Gives the number of categories |
barplot() |
Creates a bar plot |
A coin is tossed 200 times. The following frequencies are observed:
Test whether the coin is fair at the 5% significance level.
observed <- c(112, 88)
# Write your R code below
In a genetic experiment, 200 offspring are classified into two phenotypic categories.
Test whether the observed data agree with Mendel’s theoretical 3:1 ratio.
observed <- c(150, 50)
# Write your R code below
Suppose the probability of a male birth and a female birth is assumed to be equal.
In a sample of 100 births, the following observations are recorded:
Test whether the observed frequencies agree with the assumed theoretical distribution.
observed <- c(48, 52)
# Write your R code below
A random sample of 200 observations is classified into four categories.
Observed frequencies:
observed <- c(60, 45, 55, 40)
# Suppose all four categories are equally likely.
# Write your R code below
Test whether the observed distribution follows an equal-probability distribution.
The Chi-square goodness of fit test is used to examine whether observed categorical data agree with a specified theoretical probability distribution.
The chisq.test() function in R makes this procedure
straightforward by providing the test statistic, degrees of freedom and
p-value.
The test has applications in genetics, biology, ecology, probability and many other areas of statistics.