LdM
LdM

Reputation: 704

Chi-squared for determining people voting in each category

My dataset contains the following columns:

Voted? Political Category
1            Right
0            Left
1            Center
1            Right
1            Right
1            Right

I would need to see which category is mostly associated with people who voted. To do this, I would need to calculate the chi-squared. What I would like is to group by Voted? and Political Category in order to have something like this:

(1, Right) : 1500 people
(0, Right) : 202 people
(1, Left): 826 people
(0, Left): 652 people
(1, Center): 431 people
(0, Center): 542 people

In R, I would do:

yes = c(1500, 826, 431)
no  = c(212, 652, 542)
TBL = rbind(yes, no);  TBL

    [,1] [,2] [,3]
yes 1500  826  431
no   212  652  542

and apply

chisq.test(TBL, cor=F)

with:

X-squared = 630.08, df = 2, p-value < 2.2e-16

Even better if I use prop.test, as it would give the proportions of people voting in each political category.

   prop 1    prop 2    prop 3 
0.8761682 0.5588633 0.4429599 

I would like to get the same, or similar, results in Python.

Upvotes: 0

Views: 269

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114921

Your data is in the form of a contingency table. SciPy has the function scipy.stats.chi2_contingency for applying the chi-squared test to a contingency table.

For example,

In [48]: import numpy as np

In [49]: from scipy.stats import chi2_contingency

In [50]: tbl = np.array([[1500, 826, 431], [212, 652, 542]])

In [51]: stat, p, df, expected = chi2_contingency(tbl)

In [52]: stat
Out[52]: 630.0807418107023

In [53]: p
Out[53]: 1.5125346728116583e-137

In [54]: df
Out[54]: 2

In [55]: expected
Out[55]: 
array([[1133.79389863,  978.82440548,  644.38169589],
       [ 578.20610137,  499.17559452,  328.61830411]])

Upvotes: 1

Related Questions