Skip to content

Commit c33ca88

Browse files
authored
Added file on chisquare_test (#5890)
* Added file on chisquare_test * Update chi-squared-tests.md minor fixes * Update content/python/concepts/statsmodels/terms/chi-squared-tests/chi-squared-tests.md * Updates and errors ---------
1 parent e50335b commit c33ca88

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
Title: 'Chi-Squared tests'
3+
Description: 'Assess the relationship between the actual and expected variables against a hypothesis.'
4+
Subjects:
5+
- 'Machine Learning'
6+
- 'Data Science'
7+
Tags:
8+
- 'Statistics'
9+
- 'Properties'
10+
- 'Models'
11+
- 'Data'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **Chi-Square test** in statsmodels is used to test whether observed proportions differ from expected proportions. It is commonly used to compare proportions across multiple groups or categories. The test can be applied in two contexts: goodness-of-fit (to see if the proportions match an expected distribution) and test of independence (to assess if two categorical variables are independent).
18+
19+
## Syntax
20+
21+
```psuedo
22+
scipy.stats.chisquare(f_obs, f_exp=None, ddof=0, axis=0)
23+
```
24+
25+
- `f_obs`: The observed frequencies or values. This should be a 1D or 2D array where each value represents the observed count in a category or group.
26+
- `f_exp`: The expected frequencies or values. This is also a 1D or 2D array, where each value represents the expected count in the corresponding category or group.
27+
- `ddof`: The "Delta Degrees of Freedom" adjustment for the test. This is used to adjust for the number of parameters estimated from the data. For a goodness-of-fit test, `ddof=0` is standard, but you can adjust it for specific models or tests.
28+
- `axis`: The axis along which the test is computed. For multi-dimensional data, you can specify the axis (0 for rows and 1 for columns). If `axis` is set to `None`, the test is applied to all dimensions of the array.
29+
30+
## Example
31+
32+
In this example, a chi-square test is performed to compare observed proportions across four categories with the expected proportions to determine if they differ:
33+
34+
```py
35+
from scipy.stats import chisquare
36+
37+
# Observed counts
38+
counts = [150, 80, 100, 70]
39+
40+
# For equal expected proportions (null hypothesis)
41+
# Expected counts would be total/number of categories
42+
n_categories = len(counts)
43+
total = sum(counts)
44+
expected = [total/n_categories] * n_categories
45+
46+
# Perform chi-square test
47+
chi2_stat, p_value = chisquare(
48+
f_obs=counts, # Observed frequencies
49+
f_exp=expected, # Expected frequencies
50+
ddof=0 # Degrees of freedom adjustment
51+
)
52+
53+
# Print results
54+
print(f"Chi-square statistic: {chi2_stat}")
55+
print(f"P-value: {p_value}")
56+
57+
# Interpret results
58+
alpha = 0.05
59+
if p_value < alpha:
60+
print("Reject the null hypothesis: The proportions are significantly different.")
61+
else:
62+
print("Fail to reject the null hypothesis: The proportions are not significantly different.")
63+
```
64+
65+
The code above generates the ouput as follows:
66+
67+
```shell
68+
Chi-square statistic: 38.0
69+
P-value: 2.8264748814532456e-08
70+
Reject the null hypothesis: The proportions are significantly different.
71+
```

0 commit comments

Comments
 (0)