1 1. Introduction

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:

2 2. Chi-square Goodness of Fit Test

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.

2.1 2.1 Hypotheses

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.

2.2 2.2 Decision rule

At a 5% level of significance:

  • If p-value < 0.05, reject \(H_0\).
  • If p-value >= 0.05, do not reject \(H_0\).

3 3. Example 1: Testing whether a coin is fair

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.

3.0.1 Step 1: Formulate hypotheses

\[ H_0: P(H)=P(T)=0.5 \]

\[ H_1: \text{The coin is not fair} \]

3.0.2 Step 2: Calculate expected frequencies

For a fair coin:

\[ E(H)=100 \times 0.5=50 \]

\[ E(T)=100 \times 0.5=50 \]

3.0.3 Step 3: Calculate the test statistic manually

# 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

3.0.4 Step 4: Calculate p-value

# 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

3.0.5 Step 5: Decision

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"

3.0.6 Step 6: Performing the test using chisq.test()

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

3.0.7 Understanding the R output

The output provides:

  1. Chi-square test statistic.
  2. Degrees of freedom.
  3. p-value.

If the p-value is greater than 0.05, we do not have sufficient evidence to conclude that the coin is unfair.

4 4. Example 2: Mendel’s Law of Segregation

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.

4.1 Step 1: Hypotheses

\[ H_0: \text{The phenotypic ratio is } 3:1 \]

\[ H_1: \text{The phenotypic ratio is not } 3:1 \]

4.2 Step 2: Calculate expected frequencies

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 \]

4.3 Step 3: R implementation

# Observed frequencies

observed <- c(120, 40)

# Theoretical probabilities

probability <- c(3/4, 1/4)

# Expected frequencies

expected <- sum(observed)*probability

expected
## [1] 120  40

4.4 Step 4: Calculate Chi-square statistic

chi_square <- sum((observed-expected)^2/expected)

chi_square
## [1] 0

4.5 Step 5: Perform the goodness of fit test

result <- chisq.test(observed, p = probability)

result
## 
##  Chi-squared test for given probabilities
## 
## data:  observed
## X-squared = 0, df = 1, p-value = 1

4.6 Step 6: Interpretation

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.

5 5. Example 3: Testing whether a die is unbiased

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.

5.1 Hypotheses

\[ H_0: P(1)=P(2)=\cdots=P(6)=\frac{1}{6} \]

\[ H_1: \text{At least one probability differs from } \frac{1}{6} \]

5.2 R implementation

# 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

5.3 Expected frequencies

result$expected
##  1  2  3  4  5  6 
## 20 20 20 20 20 20

5.4 Decision

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"

5.5 Interpretation

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.

6 6. Visual representation of observed and expected frequencies

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"))

7 7. Important assumptions

The Chi-square goodness of fit test has the following important assumptions:

  1. Observations should be randomly selected.
  2. Observations should be independent.
  3. Categories should be mutually exclusive.
  4. Every observation should belong to exactly one category.
  5. Expected frequencies should generally be at least 5 for the usual Chi-square approximation to be reliable.

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.

8 8. Important R functions

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

9 9. Practice problems

9.1 Problem 1: Fair coin

A coin is tossed 200 times. The following frequencies are observed:

  • Head: 112
  • Tail: 88

Test whether the coin is fair at the 5% significance level.

observed <- c(112, 88)

# Write your R code below

9.2 Problem 2: Genetic ratio

In a genetic experiment, 200 offspring are classified into two phenotypic categories.

  • Dominant phenotype: 150
  • Recessive phenotype: 50

Test whether the observed data agree with Mendel’s theoretical 3:1 ratio.

observed <- c(150, 50)

# Write your R code below

9.3 Problem 3: Birth distribution

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:

  • Male: 48
  • Female: 52

Test whether the observed frequencies agree with the assumed theoretical distribution.

observed <- c(48, 52)

# Write your R code below

9.4 Problem 4: Four-category distribution

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.

10 10. Conclusion

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.