Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import numpy as np
- import random
- from math import sqrt
- import scipy.stats
- def get_data(file):
- csv_x = csv.reader(open(file))
- data = []
- for row in csv_x:
- data.append(row)
- return data
- def extrapolate(data_list, diagnosis):
- data = []
- for tuple in data_list:
- if tuple[0] == diagnosis:
- data.append(float(tuple[1]))
- return data
- def sig_test(reference, symptom, tail):
- alpha = 0.05
- ref_sample_size = len(reference)
- sample_size = len(symptom)
- se = sqrt(np.std(symptom)**2/sample_size + np.std(reference)**2/ref_sample_size)
- t = abs(np.mean(symptom) - np.mean(reference))/se
- p_value = 1 - scipy.stats.t.cdf(t, min(sample_size, ref_sample_size) - 1)
- s = sqrt(((sample_size - 1)*np.std(symptom)**2 + (ref_sample_size - 1)*np.std(reference)**2)/(sample_size + ref_sample_size - 2))
- d = abs(np.mean(symptom) - np.mean(reference))/s
- if tail == 2:
- p_value *= 2
- print " Standard error:", se, " T-score:", t, " P-value:", p_value, " Difference of means:", np.mean(symptom) - np.mean(reference)
- if p_value <= alpha/3:
- print "Significant difference"
- print "Cohen's d:", d
- print "Difference of means:", np.mean(symptom) - np.mean(reference)
- else:
- print "No significant difference"
- ###########################################################
- fever = get_data('fever.csv')
- WBC = get_data('WBC.csv')
- fever_None = extrapolate(fever, 'No')
- fever_Py2_7 = extrapolate(fever, 'Py2.7')
- fever_Py3 = extrapolate(fever, 'Py3')
- WBC_None = extrapolate(WBC, 'No')
- WBC_Py2_7 = extrapolate(WBC, 'Py2.7')
- WBC_Py3 = extrapolate(WBC, 'Py3')
- print "Fever tests:"
- print "Normal and Py2.7:"
- sig_test(fever_None, fever_Py2_7, 1)
- print "Normal and Py3:"
- sig_test(fever_None, fever_Py3, 1)
- print "Py2.7 and Py3:"
- sig_test(fever_Py2_7, fever_Py3, 2)
- print ""
- print "WBC tests:"
- print "Normal and Py2.7:"
- sig_test(WBC_None, WBC_Py2_7, 1)
- print "Normal and Py3:"
- sig_test(WBC_None, WBC_Py3, 1)
- print "Py2.7 and Py3:"
- sig_test(WBC_Py2_7, WBC_Py3, 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement