-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchi-square_test.py
54 lines (37 loc) · 2.43 KB
/
chi-square_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# import packages
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import chi2_contingency, chi2 # import chi2_contingency to compute for the chi-square statistics and the p-value for the particular hypothesis test
# import chi2 to find the critical value based on the acceptance criteria
# import data
campaign_data = pd.read_excel("grocery_database.xlsx", sheet_name = "campaign_data")
# filter data
# data only needs the mailer1 and mailer2 types
# drop rows with "control" under mailer_type
campaign_data = campaign_data.loc[campaign_data["mailer_type"] != "Control"]
# create a 2x2 matrix array
# summarise to get our observed frequencies
observed_values = pd.crosstab(campaign_data["mailer_type"], campaign_data["signup_flag"]).values
mailer1_signup_rate = 123 / (252 + 123)
mailer2_signup_rate = 127 / (209 + 127)
print(mailer1_signup_rate, mailer2_signup_rate)
# state hypotheses & set acceptance criteria
null_hypothesis = "There is no relationship between mailer type and signup rate. They are independent."
alternate_hypothesis = "There is a relationship between mailer type and signup rate. They are not independent."
acceptance_criteria = 0.05
# calculate expected frequencies & chi square statistic
chi2_statistic, p_value, dof, expected_values = chi2_contingency(observed_values, correction = False)
print(chi2_statistic, p_value)
# find the critical value for our test
critical_value = chi2.ppf(1 - acceptance_criteria, dof)
print(critical_value)
# print results (Chi Square Statistic)
if chi2_statistic >= critical_value:
print(f"As our chi-square statistic of {chi2_statistic} is higher than our critical value of {critical_value} - we reject the null hypothesis, and conclude that: {alternate_hypothesis}.")
else:
print(f"As our chi-square statistic of {chi2_statistic} is lower than our critical value of {critical_value} - we retain the null hypothesis, and conclude that: {null_hypothesis}.")
# print results (p_value)
if p_value <= acceptance_criteria:
print(f"As our p_value of {p_value} is lower than our acceptance_criteria of {acceptance_criteria} - we reject the null hypothesis, and conclude that: {alternate_hypothesis}.")
else:
print(f"As our p_value of {p_value} is higher than our acceptance_criteria of {acceptance_criteria} - we retain the null hypothesis, and conclude that: {null_hypothesis}.")