-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexplore_UKBB_asthma_cohort.py
190 lines (100 loc) · 3.38 KB
/
explore_UKBB_asthma_cohort.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
# coding: utf-8
# In[79]:
import pandas as pd
import numpy as np
get_ipython().magic(u'matplotlib inline')
from matplotlib import rc
rc('font', **{'family':'sans-serif','sans-serif':['Helvetica']})
#rc('text', usetex=False)
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize']=(12,6)
# In[18]:
#Read in 5K sample
asthma_df=pd.read_csv("ukbb_asthma_sample.csv",sep='\t')
# In[19]:
asthma_df.columns
# In[20]:
# Select columns to filter on
cols = [col for col in asthma_df.columns if 'HES_p' not in col and 'PC' not in col]
# In[21]:
asthma_df=asthma_df[cols].drop('Unnamed: 0',axis=1)
# In[22]:
asthma_df.columns
# In[23]:
asthma_df.std()
# In[29]:
asthma_df_mean = asthma_df.mean()
# In[30]:
asthma_df.mean()
# In[28]:
asthma_df_mean.max()
# In[31]:
asthma_df_mean.min()
# In[36]:
asthma_df_mean.sort_values()
# In[46]:
cor=pd.DataFrame.corr(asthma_df)
# In[64]:
correlated_variables=cor.unstack().sort_values(ascending=False)
# In[77]:
#print(correlated_variables.index[0],correlated_variables[0])
print(correlated_variables.head(50))
# In[78]:
print(cor.unstack().head(20))
# In[76]:
#print(cor.sort_values(by='sex',ascending=False).head())
print(cor.head())
# In[314]:
#select only 'QUANTITY' fields
asthma_df_quant=asthma_df.loc[:, asthma_df.columns.str.contains('QUANT')].fillna(0.0)
#select only fields with 10 or more unique values
for col in asthma_df_quant.columns:
if len(asthma_df_quant[col].unique()) < 50 :
asthma_df_quant.drop(col,inplace=True,axis=1)
#calculate correlation matrix, take absolute value
corr_matrix=asthma_df_quant.corr().abs()
#only keep upper half of the matrix
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape)).astype(np.bool))
# filter to highly correlated 0.9-0.975 range
corr_var_df = upper.stack().where(lambda x: np.fabs(x) < 0.99).dropna().sort_values(ascending=False).reset_index()
# rename data frame varialbes
corr_var_df.columns = ['Variable1','Variable2','Correlation']
corr_var_df=corr_var_df.drop_duplicates(keep='first')
# In[256]:
corr_var_df.to_csv('uncorrelated pairs.csv')
# In[84]:
# from 0 to 3rd highest correlated variable
for i in xrange(2,4):
boxplot=asthma_df.boxplot(column=[corr_var_df['Variable1'][i],corr_var_df['Variable2'][i]])
scatterplot=asthma_df.plot.scatter(x=corr_var_df['Variable1'][i],y=corr_var_df['Variable2'][i])
plt.show(boxplot)
plt.show(scatterplot)
# In[103]:
i=2
boxplot=asthma_df.boxplot(column=[corr_var_df['Variable1'][i],corr_var_df['Variable2'][i]])
scatterplot=asthma_df.plot.scatter(x=corr_var_df['Variable1'][i],y=corr_var_df['Variable2'][i])
plt.show(boxplot)
plt.show(scatterplot)
# In[340]:
i=215
boxplot=asthma_df.boxplot(column=[corr_var_df['Variable1'][i],corr_var_df['Variable2'][i]])
scatterplot=asthma_df.plot.scatter(x=corr_var_df['Variable1'][i],y=corr_var_df['Variable2'][i])
plt.show(boxplot)
plt.show(scatterplot)
# In[312]:
boxplot=asthma_df.boxplot(column=['f_34_0_0_f_QUANT_Year_of_birth',corr_var_df['Variable2'][i]])
scatterplot=asthma_df.plot.scatter(x='f_34_0_0_f_QUANT_Year_of_birth',y='f_3063_0_f_QUANT_FEV1_maximumValue')
plt.show(boxplot)
plt.show(scatterplot)
# In[308]:
temp=[x for x in asthma_df.columns if 'Year_of_birth' in x]
# In[309]:
temp
# In[10]:
import pandas as pd
df = pd.read_csv('correlated pairs.csv')
# In[16]:
df.columns.values
df.Variable2.unique() == df.Variable1.unique()
# In[ ]:
# In[ ]: