χ2 (chi-square) test

Goodness of fit

χ2 goodness of fit test

We can use the χ2 goodness of fit test to decide if a set of observations fit a particular, expected distribution.

H0: The data fits an expected distribution

HA: The data does not fit an expected distribution

\chi^2 = \sum_{i=1} \frac{(O_{i} - E_{i})^2}{E_{i}}

# Example chi-square
chi_square <- ((50 – 56.25)^2 / 56.25) +
((20 – 18.75)^2 / 18.75) +
((20 – 18.75)^2 / 18.75) +
((10 – 6.25)^2 / 6.25)
print(chi_square)
## Chi-square distribution
pchisq(3.11, df = 3, lower.tail = FALSE)
## Check to our results
obs <- c(50, 20, 20, 10)
expect_prob <- c(9 / 16, 3 / 16, 3 / 16, 1 / 16)
chisq.test(obs, p = expect_prob)
view raw chi-square.r hosted with ❤ by GitHub