-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_conclude.py
459 lines (402 loc) · 17.8 KB
/
stats_conclude.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#imports
from scipy import stats
from itertools import combinations
#------------------------------------------------------------- STATZ-------------------------------------------------------------
# STATS CONCLUSIONS FUNCTIONS
def chi2_test(table):
"""Goal: ((compare two categorical variables))
Compare categorical variables testing for correlation.
---
Sets alpha to 0.05, runs a chi^2 test, and evaluates the pvalue
against alpha, printing a conclusion statement.
---
This function takes in a pd.crosstab table to analyze.
"""
α = 0.05
chi2, p, degf, expected = stats.chi2_contingency(table)
print('Observed')
print(table.values).head()
print('\nExpected')
print(expected.astype(int)).head()
print('\n----')
print(f'chi^2 = {chi2:.10f}')
print(f'p-value = {p:.10f} < {α}')
print('----')
if p < α:
print ('We reject the null hypothesis.')
else:
print ("We fail to reject the null hypothesis.")
def conclude_1samp_tt(group1, group_mean):
"""Goal: ((compare one categorical and one continuous variable))
Compare observed mean to theoretical mean. (Non-parametric = Wilcoxon)
Bubble in the bubble...
---
This function is a one-sample, two-tailed, t-test reliant on parametric data with
these assumptions: The dependent variable must be continuous (interval/ratio).
The observations are independent of one another.
The dependent variable should be approximately normally distributed.
The dependent variable should not contain any outliers.
---
Sets alpha = 0.05, runs a one-sample, two-tailed test, evaluates the
pvalue and tstat against alpha, printing a conclusion statement.
"""
α = 0.05
tstat, p = stats.ttest_1samp(group1, group_mean)
print(f"Assumptions are met: One-Sample, Two-Tailed T-Test successful...")
print(f't-stat: {tstat} > 0?')
print(f'p-value: {p:.10f} < {α}?')
print('\n----')
if ((p < α) & (tstat > 0)):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
def conclude_1samp_gt(group1, group_mean):
"""Goal: ((compare one categorical and one continuous variable))
Compare observed mean to theoretical mean. (Non-parametric = Wilcoxon)
Bubble in the bubble...
---
This function is a one-sample, one-tailed, t-test reliant on parametric data with
these assumptions: The dependent variable must be continuous (interval/ratio).
The observations are independent of one another.
The dependent variable should be approximately normally distributed.
The dependent variable should not contain any outliers.
---
Sets alpha = 0.05, runs a one-sample, one-tailed test (greater than), evaluates the
pvalue and tstat against alpha, and prints a conclusion statement.
"""
α = 0.05
tstat, p = stats.ttest_1samp(group1, group_mean)
print(f"Assumptions are met: One-Sample, Right-Tailed T-Test successful...")
print(f't-stat: {tstat} > 0?')
print(f'p-value: {p/2} < {α}?')
print('\n----')
if ((p / 2) < α) and (tstat > 0):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
def conclude_1samp_lt(group1, group_mean):
"""Goal: ((compare one categorical and one continuous variable))
Compare observed mean to theoretical mean. (Non-parametric = Wilcoxon)
Bubble in the bubble...
---
This function is a one-sample, one-tailed, t-test reliant on parametric data with
these assumptions: The dependent variable must be continuous (interval/ratio).
The observations are independent of one another.
The dependent variable should be approximately normally distributed.
The dependent variable should not contain any outliers.
---
Sets alpha = 0.05, runs a one-sample, one-tailed test (less than), evaluates the
pvalue and tstat against alpha, and prints a conclusion statement.
"""
α = 0.05
tstat, p = stats.ttest_1samp(group1, group_mean)
print(f"Assumptions are met: One-Sample, Left-Tailed T-Test successful...")
print(f't-stat: {tstat} < 0?')
print(f'p-value: {p/2} < {α}?')
print('\n----')
if ((p / 2) < α) and (tstat < 0):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
def compare_categorical_continuous(categorical_var, continuous_var, data):
"""
Goal: ((compare one categorical and one continuous variable)) Compare two observed means (independent samples). (Non-parametric = Mann-Whitney's)
--- This function is a two-sample, two-tailed, t-test reliant on parametric data, independence, and equal variances.
--- Sets alpha to 0.05, performs Levene Test, runs a two-sample, two-tailed test, evaluates the pvalue against alpha, and prints a conclusion statement using the outcome of the Levene test.
Parameters:
categorical_var (str): Name of the categorical variable
continuous_var (str): Name of the continuous variable
data (pandas DataFrame): DataFrame containing the data
Prints: p-value statement assessing the significance of the test.
"""
alpha = 0.05
# Perform Levene Test for equal variances
stat, p = stats.levene(data[continuous_var][data[categorical_var] == 0], data[continuous_var][data[categorical_var] == 1])
print('Levene Test Successful')
# Run two-sample, two-tailed t-test
t_stat, p_val = stats.ttest_ind(data[continuous_var][data[categorical_var] == 0], data[continuous_var][data[categorical_var] == 1], equal_var=True)
# Evaluate p-value against alpha
if p < alpha:
print(f"Levene test p-value = {p:.10f}. Variances are significantly different. Using Welch's t-test.")
t_stat, p_val = stats.ttest_ind(data[continuous_var][data[categorical_var] == 0], data[continuous_var][data[categorical_var] == 1], equal_var=False)
if p_val < alpha:
print(f"p-value = {p_val:.10f}. There is a significant difference between the means of {categorical_var} and {continuous_var}.")
else:
print(f"p-value = {p_val:.10f}. There is no significant difference between the means of {categorical_var} and {continuous_var}.")
'''def conclude_2samp_gt(sample1, sample2): BROKEN
"""Goal: ((compare one categorical and one continuous variable))
Compare two observed means (independent samples). (Non-parametric = Mann-Whitney's)
---
This function is a two-sample, right-tailed, t-test reliant on
parametric data, independence, and equal variances.
---
Sets alpha to 0.05, performs Levene Test, runs a two-sample,
right-tailed test, evaluates the pvalue against alpha, and prints
a conclusion statementusing the outcome of the Levene test.
"""
α = 0.05
# Check Variance
tstat, p = stats.levene(sample1, sample2)
print(f"Running Levene Test...")
if p > α:
print(f'p-value: {p:.10f} > {α}?')
print(f"Variance is True")
# Perform test for True variance
tstat, p = stats.ttest_ind(sample1, sample2, equal_var=True)
print(f"Assumptions are met: Two-Sample, Right-Tailed, 'equal_Var=True', T-Test successful...")
print(f't-stat: {tstat} > 0?')
print(f'p-value: {p/2} < {α}?')
print('----')
if (((p/2) < α) and (tstat > 0)):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
else:
print(f'p-value: {p:.10f} < {α}?')
print(f"Variance is False")
# Perform test for False variance
tstat, p = stats.ttest_ind(sample1, sample2, equal_var=False)
print(f"Assumptions are met: Two-Sample, Right-Tailed, 'equal_Var=False',T-Test successful...")
print(f't-stat: {tstat}')
print(f'p-value: {p:.10f} < {α}?')
print('----')
if (((p/2) < α) and (tstat > 0)):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')'''
'''def conclude_2samp_lt(sample1, sample2): BROKEN
"""Goal: ((compare one categorical and one continuous variable))
Compare two observed means (independent samples). (Non-parametric = Mann-Whitney's)
---
This function is a two-sample, left-tailed, t-test reliant on
parametric data, independence, and equal variances.
---
Sets alpha to 0.05, runs a Levene test, runs a two-sample,
left-tailed (less than) test, evaluates the pvalue against alpha, and
prints a conclusion statement using the outcome of the Levene test.
"""
α = 0.05
# Check Variance
tstat, p = stats.levene(sample1, sample2)
print(f"Running Levene Test...")
if p > α:
print(f'p-value: {p:.10f} > {α}?')
print(f"Variance is True")
# Perform test for True variance
tstat, p = stats.ttest_ind(sample1, sample2, equal_var=True)
print(f"Assumptions are met: Two-Sample, Left-Tailed, 'equal_Var=True', T-Test successful...")
print(f't-stat: {tstat} < 0?')
print(f'p-value: {p/2} < {α}?')
print('\n----')
if (((p/2) < α) and (tstat < 0)):
print("we can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
else:
print(f'p-value: {p:.10f} < {α}?')
print(f"Variance is False")
# Perform test for False variance
tstat, p = stats.ttest_ind(sample1, sample2, equal_var=False)
print(f"Assumptions are met: Two-Sample, Right-Tailed, 'equal_Var=False', T-Test successful...")
print(f't-stat: {tstat}')
print(f'p-value: {p:.10f} < {α}?')
print('----')
if (((p/2) < α) and (tstat < 0)):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')'''
def conclude_anova(theoretical_mean, group1, group2):
"""Goal: ((compare one continuous variable and more than one categorical))
Compare several observed means (independent samples). (Non-parametric = Kruskal-Wallis)
---
This function is reliant on parametric data, independence,
and equal variances.
---
Sets alpha to 0.05, runs a Levene test, evaluates the pvalue and tstat
against alpha, and prints a conclusion statement based on the outcome of the
Levene test.
"""
α = 0.05
# Check Variance
tstat, pval = stats.levene(theoretical_mean, group1, group2)
print(f"Running Levene Test...")
if pval > α:
print(f'p-value: {pval:.10f} > {α}?')
print(f"Variance is True, Proceed with ANOVA test...")
# Perform test for true variance
tstat, p = stats.f_oneway(theoretical_mean, group1, group2)
print(f"Assumptions are met: ANOVA successful...")
print(f't-stat: {tstat}')
print(f'p-value: {p:.10f} < {α}?')
print('----')
if p < α:
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
else:
print(f'p-value: {pval:.10f} < {α}?')
print(f"Variance is False, Proceed with Kruskal-Willis test...")
# Run alternative test for false variance
tstat, p = stats.kruskal(theoretical_mean, group1, group2)
print(f"Assumptions are not met: Kruskal successful...")
print(f't-stat: {tstat}')
print(f'p-value: {p:.10f} < {α}?')
print('----')
if p < α:
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
def conclude_pearsonr(floats1, floats2):
"""Goal: ((compare two continuous variables))
Compare two continuous variables for linear correlation. (Non-parametric = Spearman's R)
---
This function is a correlation test reliant on parametric data.
---
Sets alpha to 0.05, runs a Pearson's Correlation test on parametric data,
evaluates the pvalue against alpha, and prints a conclusion statement.
---
This function takes in two seperate floats and is used when the relationship is
linear, both variables are quantitative, normally distributed, and have no outliers.
"""
α = 0.05
r, p = stats.pearsonr(floats1, floats2)
print(f"Parametric data: Pearson's R test successful...")
print(f'r (correlation value): {r}')
print(f'p-value: {p:.10f} < {α}?')
print('----')
if p < α:
print("We can reject the null hypothesis.")
else:
print("We fail to reject the null hypothesis.")
def conclude_spearmanr(floats1, floats2):
"""Goal: ((compare two continuous variables))
Compare two continuous variables for linear correlation. (Parametric = Pearson's R)
---
Sets alpha to 0.05, runs a Spearman's Correlation test on non-parametric
data, evaluates the pvalue against alpha, and prints a conclusion statement.
---
Takes in two seperate floats and is used when the relationship is rank-ordered,
both variables are quantitative, NOT normally distributed, and presents
as potentially monotonic.
"""
α = 0.05
r, p = stats.spearmanr(floats1, floats2)
print(f"Non-Parametric data: Spearman's R test successful...")
print(f'r (correlation value): {r}')
print(f'p-value: {p:.10f} < {α}?')
print('----')
if p < α:
print("We can reject the null hypothesis.")
else:
print("We fail to reject the null hypothesis.")
def conclude_wilcoxon_tt(group1, group_mean):
"""Goal: ((compare one categorical and one continuous variable))
Compare observed mean to theoretical mean. (Parametric = One-Sample, Two-Tailed, T-Test)
---
This function is an alternative two-tailed test reliant on
NON-parametric data.
---
Sets alpha = 0.05, runs a Wilcoxon Two-Tailed test, evaluates the
pvalue and tstat against alpha, and prints a conclusion statement.
"""
α = 0.05
tstat, p = stats.wilcoxon(group1, group_mean)
print(f"Non-parametric data: Wilcoxon Two-Tailed Test successful...")
print(f't-stat: {tstat} > 0?')
print(f'p-value: {p:.10f} < {α}?')
print('\n----')
if ((p < α) & (tstat > 0)):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
def conclude_wilcoxon_lt(group1, group_mean):
"""Goal: ((compare one categorical and one continuous variable))
Compare observed mean to theoretical mean. (Parametric = One-Sample, Left-Tailed, T-Test)
---
This function is an alternative left-tailed test reliant on
NON-parametric data.
---
Sets alpha = 0.05, runs a Wilcoxon Left-Tailed test, evaluates the
pvalue and tstat against alpha, and prints a conclusion statement.
"""
α = 0.05
tstat, p = stats.wilcoxon(group1, group_mean)
print(f"Non-parametric data: Wilcoxon Left-Tailed Test successful...")
print(f't-stat: {tstat} > 0?')
print(f'p-value: {p:.10f} < {α}?')
print('\n----')
if ((p / 2) < α) and (tstat < 0):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
def conclude_wilcoxon_gt(group1, group_mean):
"""Goal: ((compare one categorical and one continuous variable))
Compare observed mean to theoretical mean. (Parametric = One-Sample, Right-Tailed T-Test)
---
This function is an alternative right-tailed test reliant on
NON-parametric data.
---
Sets alpha = 0.05, runs a Wilcoxon Right-Tailed test, evaluates the
pvalue and tstat against alpha, and prints a conclusion statement.
"""
α = 0.05
tstat, p = stats.wilcoxon(group1, group_mean)
print(f"Non-Parametric data: Wilcoxon Right-Tailed Test successful...")
print(f't-stat: {tstat} > 0?')
print(f'p-value: {p/2} < {α}?')
print('\n----')
if ((p / 2) < α) and (tstat > 0):
print("We can reject the null hypothesis.")
else:
print('We fail to reject the null hypothesis.')
def conclude_mannwhitneyu_tt(subpop1, subpop2):
"""Goal: ((compare one categorical and one continuous variable))
---
Sets alpha to 0.05, runs a Mann-Whitney U test on non-parametric, ordinal
data, evaluates the pvalue against alpha, and prints a conclusion statement.
---
This function takes in two sub-populations and is usually used to compare
sample means: use when the data is ordinal (non-numeric) and t-test assumptions
are not met.
"""
α = 0.05
t, p = stats.mannwhitneyu(subpop1, subpop2)
print(f"Non-Parametric/Ordinal data: Mann-Whitney test successful...")
print(f"t-stat: {t}")
print(f'p-value: {p:.10f} < {α}?')
if p < α:
print("We reject the null hypothesis.")
else:
print("We fail to reject the null hypothesis.")
def iterate_columns(df):
"""This function runs a pearson's r correlation test through a df.
---
Sets alpha to .05. This function is a correlation test reliant on parametric
data, evaluates the pvalue against alpha, and prints a conclusion statement.
"""
α = 0.05
col_cat = []
col_num = []
# establish numericals
for col in df.columns:
if col in df.select_dtypes(include=['number']):
col_num.append(col)
else:
col_cat.append(col)
# iterate through numerical
for i in range(2, len(col_num) + 1):
for combo in combinations(col_num, i):
corr, pval = stats.pearsonr(df[combo[0]], df[combo[1]])
if pval < α:
print(f'{combo}')
print(f'corr: {corr:.2f} > 0?')
print(f'p-value: {pval:.10f} < {α}?')
print('----')
print("We can reject the null hypothesis.\n")
continue
else:
print(f'{combo}')
print(f'corr: {corr:.2f} > 0?')
print(f'p-value: {pval:.10f} < {α}?')
print('----')
print("*****We fail to reject the null hypothesis.*****")