Lets say I have a population consisting of items A
, B
, and C
. I dont know the distribution of items in the population. I sample from this population 15 times (as an example, closer to 400 in my actual data) and count how many times I get each item, giving me something like this:
df = pd.DataFrame({'type': ['A', 'B', 'C'], 'count': [7,4,4]})
type count
0 A 7
1 B 4
2 C 4
I repeat this to get a second sample:
df2 = pd.DataFrame({'type': ['A', 'B', 'C'], 'count': [4,10,1]})
type count
0 A 4
1 B 10
2 C 1
I want to test whether these 2 samples come from the same population, or whether their frequency distributions are the same. There are posts on the theory behind this (e.g. Test for difference between 2 empirical discrete distributions), but I'm interested in implementation in Python
If I wanted to do a chi square test, would it be as simple as the following?
import scipy.stats
scipy.stats.chisquare(df['count'], df2['count'])
The docs for the chisquare
function says the second argument should be expected frequencies of each category. In my mind, this means the distribution in the population, which I dont know. Is it OK to use another set of observed frequencies if I wanted to compare 2 samples?