國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
附錄 D: 方法二使用之程式碼
people_ state_ sub.py
import pickle
import numpy as np import calendar
from scipy import stats from NHI_toolbox import * import numpy.random as rn
All_stat = list() All_record = list() W_All_record = list() mean_record = list() W_mean_record = list() P_value = list()
Super_record = list() for ttt in range(64):
print ttt, ’times **********’
print ’---’
for i in range(1998,2012):
‧
filename1 = ’/home/glophy/NHI/YOYO/flu_shot_’+str(i)+’.pkl’
filename2 = ’/home/glophy/NHI/YOYO/flu_seak_’+str(i)+’.pkl’
filename3 = ’/home/glophy/NHI/YOYO/flu_seak_’+str(i+1)+’.pkl’
fid1 = open(filename1,’r’) fid2 = open(filename2,’r’) fid3 = open(filename3,’r’) A1 = pickle.load(fid1) B1 = pickle.load(fid2) B2 = pickle.load(fid3) fid2.close()
fid2.close() fid3.close()
Shot_people = A1.keys()
Seak_people1 = set(B1.keys()) Seak_people2 = set(B2.keys()) n = len(Shot_people)
n1 = int(0.7*float(n)) Rid = rn.permutation(n)
m = len(set(Seak_people1).difference(set(Shot_people))) print ’In year ’,i
print ’There are ’, n, ’ peoples who have flu shot’
print ’There are ’, m, ’ peoples who seak without flu shot’
print ’*********************************’
All_stat.append((n,m))
# Rc for child, Rm for middle age, Ro for old man Rc = np.zeros([4,n1])
Rm = np.zeros([4,n1])
‧
for ii1 in range(n1):
#make the random sampling 70%
ID = Shot_people[Rid[ii1]]
shot_day = A1[ID][0][0]
if ID in Seak_people1:
if A1[ID][0][2][0]<6:
for seakdata in B1[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]>0):
j = period[1]/3 Rc[j:,t] += 1
elif A1[ID][0][2][0]>=60:
for seakdata in B1[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]>0):
j = period[1]/3 Ro[j:,t] += 1 else:
for seakdata in B1[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]>0):
j = period[1]/3 Rm[j:,t] += 1
if ID in Seak_people2:
‧
for seakdata in B2[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]<12):
j = period[1]/3 Rc[j:,t] += 1
elif A1[ID][0][2][0]>=60:
for seakdata in B2[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]<12):
j = period[1]/3 Ro[j:,t] += 1 else:
for seakdata in B2[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]<12):
j = period[1]/3 Rm[j:,t] += 1
‧
rc[k] = np.mean(Rc[k,:]) rm[k] = np.mean(Rm[k,:]) ro[k] = np.mean(Ro[k,:])
ra[k] = float(rc[k]+rm[k]+ro[k])/3 mean_record.append([rc,rm,ro,ra]) All_record.append([Rc,Rm,Ro])
Shot_people = set(A1.keys()) Seak_people1 = set(B1.keys())
Seak_people1 = list(Seak_people1.difference(Shot_people)) Seak_people2 = set(B2.keys())
m = len(Seak_people1)
m1 = int(float(m)*0.7)
# random sampling 70%
Rid = rn.permutation(m) WRc = np.zeros([4,m1])
WRm = np.zeros([4,m1]) WRo = np.zeros([4,m1]) shot_day = str(i)+’1001’
t=0
for ii2 in range(m1):
ID = Seak_people1[Rid[ii2]]
if B1[ID][0][1][0]<6:
for seakdata in B1[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]>0):
‧
j = period[1]/3 WRc[j:,t] += 1
elif B1[ID][0][1][0]>=60:
for seakdata in B1[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]>0):
j = period[1]/3 WRo[j:,t] += 1 else:
for seakdata in B1[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]>0):
j = period[1]/3 WRm[j:,t] += 1
if ID in Seak_people2:
if B1[ID][0][1][0]<6:
for seakdata in B2[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]<12):
j = period[1]/3 WRc[j:,t] += 1
elif B1[ID][0][1][0]>=60:
for seakdata in B2[ID]:
‧
period = Age(shot_day,seak_day) if (period[1]<12):
j = period[1]/3 WRo[j:,t] += 1 else:
for seakdata in B2[ID]:
seak_day = seakdata[0]
period = Age(shot_day,seak_day) if (period[1]<12):
j = period[1]/3 WRm[j:,t] += 1
for k in range(4):
wrc[k] = np.mean(WRc[k,:]) wrm[k] = np.mean(WRm[k,:]) wro[k] = np.mean(WRo[k,:])
wra[k] = float(wrc[k]+wrm[k]+wro[k])/3 W_mean_record.append([wrc,wrm,wro,wra]) W_All_record.append([WRc,WRm,WRo])
for k in range(4):
print ’The ’, 3*(k+1), ’ month’
print ’The average times for child is ’, rc[k], wrc[k]
‧
print ’The average times for middle people is ’, rm[k], wrm[k]
print ’The average times for old people is ’, ro[k], wro[k]
print ’...’
#stats.ttest_ind(rvs1,rvs2, equal_var = False) K = np.zeros([3,4])
for k in range(4):
pc = stats.ttest_ind(WRc[k,:],Rc[k,:],equal_var = False) pm = stats.ttest_ind(WRm[k,:],Rm[k,:],equal_var = False) po = stats.ttest_ind(WRo[k,:],Ro[k,:],equal_var = False) K[0,k] = pc[1]
K[1,k] = pm[1]
K[2,k] = po[1]
print ’The ’, 3*(k+1), ’ month’
print ’The p-value of Child is ’, pc[1]
print ’The p-value of middle people is ’, pm[1]
print ’The p-value of old people is ’, po[1]
print ’==========================================’
P_value.append(K)
Super_record.append((mean_record,W_mean_record)) output_file = ’Result_sub.pkl’
fid = open(output_file,’w’) pickle.dump(Super_record,fid) fid.close()
final_ stat.py
‧
import numpy as np
fid = open(’Result_sub.pkl’,’r’) A = pickle.load(fid)
fid.close()
RC = [list(), list(), list(), list()]
WRC = [list(), list(), list(), list()]
RM = [list(), list(), list(), list()]
WRM = [list(), list(), list(), list()]
RO = [list(), list(), list(), list()]
WRO = [list(), list(), list(), list()]
RA = [list(), list(), list(), list()]
WRA = [list(), list(), list(), list()]
for i in range(64):
for j in range(14):
#A[0][0][] : no W
#A[0][1][] : W for k in range(4):
RC[k].append(A[0][0][i*14+j][0][k])
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
print ’The mean of RC-3 is ’, np.mean(RC[0]) print ’The mean of WRC-3 is ’, np.mean(WRC[0]) print ’The p-value of RC-WRC-3 is ’,
stats.ttest_ind(RC[0],WRC[0],equal_var = False) print ’=============================’
print ’The mean of RC-6 is ’, np.mean(RC[1]) print ’The mean of WRC-6 is ’, np.mean(WRC[1]) print ’The p-value of RC-WRC-6 is ’,
stats.ttest_ind(RC[1],WRC[1],equal_var = False) print ’=============================’
print ’The mean of RC-9 is ’, np.mean(RC[2]) print ’The mean of WRC-9 is ’, np.mean(WRC[2]) print ’The p-value of RC-WRC-9 is ’,
stats.ttest_ind(RC[2],WRC[2],equal_var = False) print ’=============================’
print ’The mean of RC-12 is ’, np.mean(RC[3]) print ’The mean of WRC-12 is ’, np.mean(WRC[3]) print ’The p-value of RC-WRC-12 is ’,
stats.ttest_ind(RC[3],WRC[3],equal_var = False) print ’*****************************’
print ’The mean of RM-3 is ’, np.mean(RM[0])
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
print ’The p-value of RM-WRM-3 is ’,
stats.ttest_ind(RM[0],WRM[0],equal_var = False) print ’=============================’
print ’The mean of RM-6 is ’, np.mean(RM[1]) print ’The mean of WRM-6 is ’, np.mean(WRM[1]) print ’The p-value of RM-WRM-6 is ’,
stats.ttest_ind(RM[1],WRM[1],equal_var = False) print ’=============================’
print ’The mean of RM-9 is ’, np.mean(RM[2]) print ’The mean of WRM-9 is ’, np.mean(WRM[2]) print ’The p-value of RM-WRM-9 is ’,
stats.ttest_ind(RM[2],WRM[2],equal_var = False) print ’=============================’
print ’The mean of RM-12 is ’, np.mean(RM[3]) print ’The mean of WRM-12 is ’, np.mean(WRM[3]) print ’The p-value of RM-WRM-12 is ’,
stats.ttest_ind(RM[3],WRM[3],equal_var = False) print ’*****************************’
print ’The mean of RO-3 is ’, np.mean(RO[0]) print ’The mean of WRO-3 is ’, np.mean(WRO[0]) print ’The p-value of RO-WRO-3 is ’,
stats.ttest_ind(RO[0],WRO[0],equal_var = False) print ’=============================’
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
print ’The mean of RO-6 is ’, np.mean(RO[1]) print ’The mean of WRO-6 is ’, np.mean(WRO[1]) print ’The p-value of RO-WRO-6 is ’,
stats.ttest_ind(RO[1],WRO[1],equal_var = False) print ’=============================’
print ’The mean of RO-9 is ’, np.mean(RO[2]) print ’The mean of WRO-9 is ’, np.mean(WRO[2]) print ’The p-value of RO-WRO-9 is ’,
stats.ttest_ind(RO[2],WRO[2],equal_var = False) print ’=============================’
print ’The mean of RO-12 is ’, np.mean(RO[3]) print ’The mean of WRO-12 is ’, np.mean(WRO[3]) print ’The p-value of RO-WRO-12 is ’,
stats.ttest_ind(RO[3],WRO[3],equal_var = False) print ’*****************************’
print ’The mean of RA-3 is ’, np.mean(RA[0]) print ’The mean of WRA-3 is ’, np.mean(WRA[0]) print ’The p-value of RA-WRA-3 is ’,
stats.ttest_ind(RA[0],WRA[0],equal_var = False) print ’=============================’
print ’The mean of RA-6 is ’, np.mean(RA[1]) print ’The mean of WRA-6 is ’, np.mean(WRA[1]) print ’The p-value of RA-WRA-6 is ’,
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
print ’=============================’
print ’The mean of RA-9 is ’, np.mean(RA[2]) print ’The mean of WRA-9 is ’, np.mean(WRA[2]) print ’The p-value of RA-WRA-9 is ’,
stats.ttest_ind(RA[2],WRA[2],equal_var = False) print ’=============================’
print ’The mean of RA-12 is ’, np.mean(RA[3]) print ’The mean of WRA-12 is ’, np.mean(WRA[3]) print ’The p-value of RA-WRA-12 is ’,
stats.ttest_ind(RA[3],WRA[3],equal_var = False) print ’*****************************’