Test your Hypothesis and Chi-Squared test
The table shows the contingency table of marital status by education. Use the Chi-Squared Test for testing Homogeneity.
Marital Status | Middle School | High School | Bachelor | Masters | PhD |
---|---|---|---|---|---|
Single | 18 | 36 | 21 | 9 | 6 |
Married | 12 | 36 | 45 | 36 | 21 |
Divorced | 6 | 9 | 9 | 3 | 3 |
Widowed | 3 | 9 | 9 | 6 | 3 |
Hypothesis
Null Hypothesis:
-There is no difference between the types of education level in terms of marital status.
Alternate Hypothesis:
-There is a difference in distribution between the types of education level in terms of marital status.
Steps to be followed:
Write a function chi_test() that does the following sequence of steps
- Declare a 2D array with the values mentioned in the contingency table of marital status by education.
- calculate the values of
- Chi-Square Statistic
- Degree of Freedom
- P value
and assign it to variables stat,dof,p_val respetively
3.Assume nthe alpha value to be 0.05
4. Compare the P value with alpha and decide whether or not to reject the null hyphothesis.
-If Rejected assign the string "Reject the Null Hypothesis" to res variable
-Else assign the string "Failed to reject the Null Hypothesis" to res variable
5. Hint: Use chi2_contingency() of scipy package
Answer:
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__':
Click on tested image:
No comments:
Post a Comment
If you have any doubts, Please let us know.