The Chi-Square Test for Independence is a statistical test used to determine whether there is a significant association between two categorical variables. It is based on the comparison of observed frequencies in the data with the frequencies that would be expected if the variables were independent.
The Chi-Square statistic (χ²) is calculated using the following formula:
Where:
- O_i: Observed frequency for the i-th category
- E_i: Expected frequency for the i-th category
The expected frequency (E_i) for each category is calculated under the assumption of independence between the variables, using:
- Create a Contingency Table: Summarize the frequencies of the two categorical variables in a matrix format.
- Calculate Expected Frequencies: Compute the expected frequencies for each cell of the table assuming the variables are independent.
- Compute the Chi-Square Statistic: Use the formula to calculate the χ² value.
- Determine the Degrees of Freedom (df): Calculated as (number of rows - 1) * (number of columns - 1).
- Find the P-Value: Compare the χ² value with the Chi-Square distribution table to find the p-value.
- Interpret the Result: If the p-value is less than the significance level (typically 0.05), reject the null hypothesis of independence.
Consider a dataset with two variables: Gender (Male, Female) and Preference (Sports, Reading). Suppose we have the following observed frequencies in a contingency table:
| Sports | Reading | Row Total | |
|---|---|---|---|
| Male | 30 | 20 | 50 |
| Female | 20 | 30 | 50 |
| Column Total | 50 | 50 | 100 |
-
Expected Frequencies:
- E(Male, Sports) = (50 * 50) / 100 = 25
- E(Male, Reading) = (50 * 50) / 100 = 25
- E(Female, Sports) = (50 * 50) / 100 = 25
- E(Female, Reading) = (50 * 50) / 100 = 25
-
Chi-Square Statistic: $$ χ² = Σ((O_i - E_i)² / E_i) = ((30 - 25)² / 25) + ((20 - 25)² / 25) + ((20 - 25)² / 25) + ((30 - 25)² / 25) $$
-
Degrees of Freedom:
- df = (2-1)(2-1) = 1
-
P-Value: Using the Chi-Square distribution table or a calculator, find the p-value corresponding to χ² = 4 and df = 1.
The Chi-Square Test for Independence is widely used in various fields, including:
- Biology: To determine if there is an association between different genetic traits.
- Social Sciences: To analyze survey data and examine relationships between demographic variables.
- Market Research: To evaluate consumer preferences and behaviors based on categorical variables like gender, age group, etc.
- Medical Research: To investigate the relationship between risk factors and health outcomes.
- P-Value < 0.05: Reject the null hypothesis; there is a significant association between the variables.
- P-Value ≥ 0.05: Fail to reject the null hypothesis; no significant association exists between the variables.
This test allows researchers and analysts to make informed decisions based on the relationships between categorical variables in their data.
First, we generate a dataset with 200 samples, each having two categorical variables: Gender and Preference. The dataset is saved to a CSV file for further analysis.
import pandas as pd
import numpy as np
# Seed for reproducibility
np.random.seed(42)
# Generate sample data
n_samples = 200
genders = np.random.choice(['Male', 'Female'], size=n_samples)
preferences = np.random.choice(['Sports', 'Reading'], size=n_samples)
# Create a DataFrame
data = {
'Gender': genders,
'Preference': preferences
}
df = pd.DataFrame(data)
# Save DataFrame to CSV in the current environment
csv_file_path = 'large_sample_data.csv'
df.to_csv(csv_file_path, index=False)
print(f"CSV file created at: {csv_file_path}")