Chi-square Tests

Chi-square Tests

Chi-square Tests in R

Published on September 08, 2025By EduResHub Team

Introduction

This report demonstrates two applications of the Chi-square test in R using hypothetical agricultural entomology data:

  1. Chi-square Test of Independence – to check association between two categorical variables.
  2. Chi-square Goodness of Fit – to check whether observed counts match expected proportions.

1. Chi-square Test of Independence

We want to test if the level of pest infestation (High/Low) depends on the crop type (Tea, Citrus, Mango).

# Required libraries
library(ggplot2)
library(reshape2)

Data

pest_data <- matrix(c(25, 15,
                      30, 20,
                      10, 30),
                    nrow = 3,
                    byrow = TRUE)

colnames(pest_data) <- c("High", "Low")
rownames(pest_data) <- c("Tea", "Citrus", "Mango")
pest_data
##        High Low
## Tea      25  15
## Citrus   30  20
## Mango    10  30

Chi-square Test

chisq_test <- chisq.test(pest_data)
chisq_test
## 
##  Pearson's Chi-squared test
## 
## data:  pest_data
## X-squared = 14.5, df = 2, p-value = 0.0007102

Interpretation

  • Null hypothesis (H₀): Crop type and pest infestation are independent.
  • Alternative hypothesis (H₁): Crop type and pest infestation are associated.
  • If p-value < 0.05, reject H₀ → pest infestation depends on crop type.

From the test results, the p-value is very small (<0.05), so we reject H₀.
➡️ Pest infestation is significantly associated with crop type.

Visualization

# Mosaic plot
mosaicplot(pest_data, main="Pest Infestation by Crop Type",
           color=TRUE, xlab="Crop Type", ylab="Pest Level")

# Bar plot
df <- melt(pest_data)
colnames(df) <- c("Crop", "Pest_Level", "Count")

ggplot(df, aes(x=Crop, y=Count, fill=Pest_Level)) +
  geom_bar(stat="identity", position="dodge") +
  labs(title="Pest Infestation by Crop Type",
       x="Crop Type", y="Number of Plots") +
  theme_minimal()


2. Chi-square Goodness of Fit

We want to test if ladybird beetles are uniformly distributed across 4 blocks in a tea garden.

Data

observed <- c(18, 22, 25, 15)
expected <- c(20, 20, 20, 20)

df_gof <- data.frame(
  Block = c("A","B","C","D"),
  Observed = observed,
  Expected = expected
)
df_gof
##   Block Observed Expected
## 1     A       18       20
## 2     B       22       20
## 3     C       25       20
## 4     D       15       20

Chi-square Test

chisq_gof <- chisq.test(observed, p = rep(1/4, 4))
chisq_gof
## 
##  Chi-squared test for given probabilities
## 
## data:  observed
## X-squared = 2.9, df = 3, p-value = 0.4073

Interpretation

  • Null hypothesis (H₀): Ladybird beetles are uniformly distributed across blocks.
  • Alternative hypothesis (H₁): Distribution deviates from uniform.
  • If p-value < 0.05, reject H₀ → not uniform distribution.

From the test results, the p-value is > 0.05, so we fail to reject H₀.
➡️ Beetles are fairly evenly spread across the blocks.

Visualization

df_long <- melt(df_gof, id.vars="Block")

ggplot(df_long, aes(x=Block, y=value, fill=variable)) +
  geom_bar(stat="identity", position="dodge") +
  labs(title="Goodness of Fit: Ladybird Beetle Distribution",
       x="Block", y="Count") +
  theme_minimal()


Conclusion

  • The Chi-square Test of Independence showed a significant association between crop type and pest infestation.
  • The Chi-square Goodness of Fit Test showed that ladybird beetles are uniformly distributed across blocks.

Both examples illustrate how Chi-square tests can be applied in agricultural entomology research.