Below is a simple example that might help clarify what the chi-squared test is looking at. Let's say we have a two samples of objects that have two properties: color and height. The color can be black or white and height can be low or high. We want to test if color and height are independent of each other. In the first sample, half of the objects are black and half are white while 2/3 of them are high and 1/3 are low. Within the black objects, it is also true that half are high and half are low and the same is true of the white objects. The chi-squared test gives a p value of 1, showing that we cannot reject the null hypothesis that the groups are independent.Knowing the color of an object does not help us in knowing the height.
In the second sample, the overall population again has 1/2 high and 2/3 black but within the black population three quarters of the objects are high and within the white population all of them are low. It seems high/low is not independent of black/white and indeed the chi-squared test gives a low p-value.
ColorVec <- c(rep("Black", 20), rep("White", 10))
DF <- data.frame(Ht = rep(c("High", "Low"), each = 30),
Color = rep(ColorVec, 2))
table(DF$Ht, DF$Color)
#>
#> Black White
#> High 20 10
#> Low 20 10
chisq.test(DF$Ht, DF$Color)
#>
#> Pearson's Chi-squared test
#>
#> data: DF$Ht and DF$Color
#> X-squared = 0, df = 1, p-value = 1
ColorVec2 <- c(rep("Black", 40), rep("White", 20))
DF2 <- data.frame(Ht = rep(c("High", "Low"), each = 30),
Color = ColorVec2)
table(DF2$Ht, DF2$Color)
#>
#> Black White
#> High 30 0
#> Low 10 20
chisq.test(DF2$Ht, DF2$Color)
#>
#> Pearson's Chi-squared test with Yates' continuity correction
#>
#> data: DF2$Ht and DF2$Color
#> X-squared = 27.075, df = 1, p-value = 1.957e-07
Created on 2020-09-02 by the reprex package (v0.3.0)