13.1 Goodness of Fit Test
Categorical or qualitative data can be organized by frequency distribution of a list of each category and the corresponding counts or relative frequencies.
A Chi-square goodness-of-fit is used to test whether a frequency distribution follows a given specific distribution.
The random variable follows a certain given distribution or (the probability for each category, , as given is true).
The random variable does not follow the distribution in the null hypothesis
The Chi-square test statistic is given as:
Where
is the observed value for category
is the expected value for category assuming is true. is sample size.
For large sample sizes is approximately follows the chi-square probability distribution
Note: In order to perfrom the Chi-Square goodness of fit, we assume:
- samples are randomly selected
- sample data consist of frequency counts for each category
- For each category, the expected frequency is at least 5
For chi-square goodness of fit test, use the following syntax with observed frequency stored in obs and expected frequency stored in exp:
from scipy import stats
stats.chisquare(obs,exp)
returns Power_divergenceResult(statistic, pvalue)
Example: A store manager claims that a plain M&M bag of candies contains an equal number of each of the six colors: brown, yellow, red, blue, orange, and green. The counts for each color of a randomly selected bag of plain M&M is given below. Test the store managers claim at 5% level of significance.
| color | frequency |
|---|---|
| brown | 57 |
| yellow | 64 |
| red | 54 |
| blue | 75 |
| orange | 86 |
| green | 64 |
The above table contains the observed () frequencies. We need to find the expected frequencies before we call the function. If the manager's claim is true there will be equal number of candies for each color. We need the total number of candies and divide it to 6 to get the expected frequencies.
obs = [57,64,54,75,86,64]
sum(obs) #total number of candies in the bag
Show expected output
400If the null is true, we expect an equal number in each category. That is, for all .
| color | observed | expected |
|---|---|---|
| brown | 57 | 400/6 |
| yellow | 64 | 400/6 |
| red | 54 | 400/6 |
| blue | 75 | 400/6 |
| orange | 86 | 400/6 |
| green | 64 | 400/6 |
exp = [400/6,400/6,400/6,400/6,400/6,400/6]
obs = [57,64,54,75,86,64]
from scipy import stats
obs = [57,64,54,75,86,64]
exp = [400/6,400/6,400/6,400/6,400/6,400/6]
stats.chisquare(obs,exp)
Show expected output
Power_divergenceResult(statistic=10.67, pvalue=0.05833003695944934)Interpretation: The Chi-square test statistic is equal to 10.67. At the p-value is not less than 0.05. We would fail to reject the null hypothesis.
Example: The number of sales of electric vehicles (EV) in the USA is given in the table below. Test a claim that the distribution of sales is uniform with .
| Brand | Sales |
|---|---|
| Ford | 2400000 |
| Toyota | 2200000 |
| Chevrolet | 2020000 |
| Honda | 1500000 |
import numpy as np
from scipy import stats
obs = [2400000,2200000,2020000,1500000] # observed frequency
sum = np.sum(obs) # sum of observed freq
exp = [sum/4,sum/4,sum/4,sum/4] # expected freq
stats.chisquare(obs,exp)
Show expected output
Power_divergenceResult(statistic=220098.52216748768, pvalue=0.0)Interpretation: The chi-square test statistics is 220098.52216748768 and the p-value 0.0. Reject the null hypothesis. There is enough evidence to conclude that the distribution is not uniform.
Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.