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
|
############################################################
# #
# Virtual Laboratory of Statistics in Python #
# #
# Enumerative Data Models (18.08.2017) #
# #
# Complutense University of Madrid, Spain #
# #
# THIS SCRIPT IS PROVIDED BY THE AUTHORS "AS IS" AND #
# CAN BE USED BY ANYONE FOR THE PURPOSES OF EDUCATION #
# AND RESEARCH. #
# #
############################################################
import scipy.stats as s
import numpy as np
from statsmodels.stats.proportion import proportions_ztest
# ONE PROPORTION
x = 69
n = 96
H0 = 0.8
# alternative : string in [‘two-sided’, ‘smaller’, ‘larger’]
HA = 'smaller'
z,p = proportions_ztest(x, n, H0, HA) print() print(' z-stat. one proportion = {z} \n p-value = {p}'.format(z=z,p=p)) print()
# TWO PROPORTIONS
n1 = 1000
x1 = 702
n2 = 1000
x2 = 198
H0 = 0.0
# alternative : string in [‘two-sided’, ‘smaller’, ‘larger’]
HA='two-sided'
x12 = np.array([x1,x2]) n12 = np.array([n1, n2]) z,p = proportions_ztest(x12, n12, H0, HA) print() print(' z-stat. two proportions = {z} \n p-value = {p}'.format(z=z,p=p)) print()
# CHI-SQUARE TEST
# 2x2 - CONTINGENCE TABLE
data_observed = np.array([[8,22], [12,6]]) chi2_result,p_value,df_value,table_expected=s.chi2_contingency(data_observed) print() confidence_level = 0.95
crit_value = s.chi2.ppf(q = confidence_level, df=df_value) print() oddsratio, p_value_f = s.fisher_exact(data_observed) print ('Contingency table 2x2') print ('====================================') print ('chi2:', chi2_result) print ('p-value:', p_value) print ('df = ', df_value) print('Critical value: ',crit_value) print() print() print(' Fisher exact test:') print() print(' Oddsratio: ',oddsratio,' p-value: ',p_value_f) print() print (' Table') print ('Obs.: ') print(data_observed) print ('Exp.: ') print(table_expected) print ('====================================') print() print()
# INDEPENDENCE TEST - CONTINGENCE TABLE
data_observed = np.array([[8,22,16,12,19], [24,6,2,12,7]]) chi2_result,p_value,df_value,table_expected=s.chi2_contingency(data_observed) print() confidence_level = 0.95
crit_value = s.chi2.ppf(q = confidence_level, df=df_value) print() print ('Contingency table') print ('====================================') print ('chi2:', chi2_result) print ('p-value:', p_value) print ('df = ', df_value) print('Critical value: ',crit_value) print() print (' Table') print ('Obs.: ') print (data_observed) print ('Exp.: ') print (table_expected) print ('====================================') print() print()
|