<jupyter_start><jupyter_text>Chi-squared test for homogeneity, with independent samplesConsider that you have two independent samples and you want to check if their multinomial distributions are the same in the population. Tests for independence are the same as tests for homogeneity, apart from the question they ask (the hypothesis).<jupyter_code>alpha = 0.05

observed_1 = c(30,40,20)
n_1 = sum(observed_1)
n_1

observed_2 = c(45,30,25)
n_2 = sum(observed_2)
n_2

nTotal = n_1 + n_2
nTotal

stopifnot(length(observed_1) == length(observed_2))

nCategories = length(observed_1)
nCategories
expected_1 = c()
expected_2 = c()
for (i in 1:nCategories) {
    expected_1_i = (observed_1[i] + observed_2[i])/nTotal * n_1
    expected_2_i = (observed_1[i] + observed_2[i])/nTotal * n_2
    expected_1 = c(expected_1, expected_1_i)
    expected_2 = c(expected_2, expected_2_i)
}

expected_1
expected_2

### Z^2
testStatistic = 0
for (i in 1:nCategories) {
    o_i = observed_1[i]
    e_i = expected_1[i]
    testStatistic = testStatistic + (o_i - e_i)^2 / e_i
}

# This could be achieved with a single loop but conceptually, it's nicer as two
for (i in 1:nCategories) {
    o_i = observed_2[i]
    e_i = expected_2[i]
    testStatistic = testStatistic + (o_i - e_i)^2 / e_i
} 

testStatistic
### DOF

df = nCategories - 1
df
### Region of rejection 

### This is always an upper tail test, so just alpha not alpha/2

criticalValue = qchisq(1-alpha, df)
criticalValue<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.040268456375838924, 'mean_word_length': 4.501845018450185, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '372642', 'license': 'MIT', 'n_tokens_mistral': 592, 'n_tokens_neox': 546, 'n_words': 158, 'path': 'statistics/chi_squared_test_homogeneity_independence_independent_samples.ipynb', 'repo_name': 'JasonThomasData/algorithms', 'size': '1490'}