0
votes

Can someone please check the question and my code below and let me know why I am not getting the expected results?

Question: The table shows the contingency table of marital status by education. Use Chi-Square test for testing Homogenity.contingency table of marital status by education.

View the table by executing the following command python

from prettytable import PrettyTable
t = PrettyTable([‘Marital Status’,’Middle school’, ‘High School’,’Bachelor’,’Masters’,’PhD’])
t.add_row([‘Single’,18,36,21,9,6])
t.add_row([‘Married’,12,36,45,36,21])
t.add_row([‘Divorced’,6,9,9,3,3])
t.add_row([‘Widowed’,3,9,9,6,3])
print (t)
exit()

Hypothesis

Null Hypothesis: There is no difference in distribution between the types of education level in terms of marital status.

Alternate Hypothesis: There is a Difference

Coding

1.import chi2_contingency and from scipy.stats import chi2.

2.Declare a 2D array with the values mentioned in the contingency table of marital status by education.

3.Calculate and print the values of

– Chi-Square Statistic – Degree of Freedom – P value – Hint: Use chi2_contigency() function 4.Assume the alpha value to be 0.05

5.Compare the P value with alpha and decide whether or not to reject the null hypothesis.

– If Rejected print “Reject the Null Hypothesis” – Else print “Failed to reject the Null Hypothesis”

Sample output 2.33 4.5 8.9 Reject the Null Hypothesis

My Code:

from scipy.stats import chi2_contingency
from scipy.stats import chi2
table= [ [18,36,21,9,6],[12,36,45,36,21], [6,9,9,3,3],[3,9,9,6,3] ]
stat,p,dof,expected = chi2_contingency(table)
prob = 0.95
critical = chi2.ppf(prob, dof)
if abs(stat) >= critical:
print(stat, dof ,p ,'Reject the Null Hypothesis')
else:
print(stat, dof ,p ,'Failed to reject the Null Hypothesis')

Thank You, Rakesh

3

3 Answers

1
votes

use prob = 0.05 instead of 0.95

Thanks!

0
votes

from scipy.stats import chi2_contingency from scipy.stats import chi2

def chi_test(): # Notation Output 1. stat: Float 2. dof : Integer 3. p_val: Float 4. res: String

code

table=[[18,36,21,9,6],[12,36,45,36,21],[6,9,9,3,3],[3,9,9,6,3]]

stat,dof,p_val,res=chi2_contingency(table)
    
prob=0.95
critical=chi2.ppf(prob, dof)

if abs(stat) >= critical:
    print('Reject the Null Hypothesis')
else:
     print('Failed to reject the Null Hypothesis')

alpha=1.0-prob
if p_val <= alpha:
    print('Reject the Null Hypothesis')
else:
     print('Failed to reject the Null Hypothesis')
     
return stat,dof,p_val,res   

if name=='main': print(chi_test())

0
votes
from scipy.stats import chi2_contingency
from scipy.stats import chi2

def chi_test():
    '''
    Output
    1. stat: Float
    2. dof : Integer
    3. p_val: Float
    4. res: String
    '''
    #Note: Round off the Float values to 2 decimal places.
    table=[[18,36,21,9,6],[12,36,45,36,21],[6,9,9,3,3],[3,9,9,6,3]]

    stat,p_val,dof,res=chi2_contingency(table)
    
    prob=0.95
    critical=chi2.ppf(prob, dof)

    if abs(stat) >= critical:
        res = 'Reject the Null Hypothesis'
    else:
        res= 'Failed to reject the Null Hypothesis'

    alpha=1.0-prob
    if p_val <= alpha:
        res = 'Reject the Null Hypothesis'
    else:
        res = 'Failed to reject the Null Hypothesis'
    
    stat = round(stat,2)
    dof = round(dof,2)
    p_val = round(p_val,2)
    
    
    return stat,dof,p_val,res   

if __name__=='__main__':
    print(chi_test())