-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpermSLR.py
224 lines (156 loc) · 4.4 KB
/
permSLR.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
#####
# run the response premutation
# for the SLR model in SLR2.py
# to obtain st errors on coeff
# for the null distribution
# and finally calc the p-values
#
#####
import elasticNetLinReg as enet
from glmnet import glmnet
import numpy as np
import math
import gpdPerm
import cvTools as st
def run(X,y,name,nPerms=1000):
n,m = X.shape
# check to see if we have enough observations
if math.factorial(n)<nPerms:
raise ValueError("Not enough observations \
for {} permutations".format(nPerms))
# open preexisting model file
f = open('SLR2run_'+name+'.dat','r')
# get node properties
line = f.next()
lam = line.split()[0]
line = f.next()
alpha = line.split()[0]
line = f.next()
intercept = line.split()[0]
line = f.next()
aveErr = line.split()[0]
line = f.next()
sdErr = line.split()[0]
line = f.next()
aveNullErr = line.split()[0]
line = f.next()
sdNullErr = line.split()[0]
line = f.next()
sdY = line.split()[0]
# look to see if there are indices (non zero coef)
try:
line = f.next()
indices = line.split()
line = f.next()
sdX = line.split()
line = f.next()
coefs = line.split()
line = f.next()
meanCoef = line.split()
line = f.next()
sdCoef = line.split()
line = f.next()
pSup = line.split()
line = f.next()
errOut = line.split()
line = f.next()
errIn = line.split()
except:
# if not, set to empty
indices = []
# done with that file
f.close()
# ok we are doing a bit of a cheat here
# only looking at non zero coefs from original
# model, should redo the entier selection process
if len(indices)>0:
coefIndex = np.array(map(int,indices))
# working on subset now
Xhat = X[:,coefIndex]
nObs,nRegsHat = Xhat.shape
# t-statistic
fltSd = np.array(map(float,sdCoef))
fltSd[fltSd<1E-52]=1E-52
tStat = np.array(map(float,meanCoef))/fltSd
tStatPerm = np.ones((nRegsHat,nPerms))
for i in range(nPerms):
# permute the response
# *** probably should keep track to avoid repeats, future???
yPerm = np.random.permutation(y)
# calc of tStat
tStatPerm[:,i] = getTStat(Xhat,yPerm,float(alpha),float(lam))
# no values should have 2 in the end,
# this will let us know if something goes wrong
p = np.ones(nRegsHat)*2
for i in range(nRegsHat):
p[i] = gpdPerm.est(tStat[i],tStatPerm[i,:])
# use standard permutation if this fails
if np.isnan(p[i]) or p[i] < 1E-52:
z = tStatPerm[i,:]
tmp = np.sum(z>tStat[i])+1
p[i] = float(tmp)/(float(nPerms))
# we have it all, lets print it
f = open('SLR2run_perm_'+name+'.dat','w')
f.write(lam+"\n")
f.write(alpha+"\n")
f.write(intercept+"\n")
f.write(aveErr+"\n")
f.write(sdErr+"\n")
f.write(aveNullErr+"\n")
f.write(sdNullErr+"\n")
f.write(sdY+"\n")
if len(indices)>0:
np.array(indices).tofile(f,sep="\t")
f.write("\n")
np.array(sdX).tofile(f,sep="\t")
f.write("\n")
np.array(coefs).tofile(f,sep="\t")
f.write("\n")
np.array(meanCoef).tofile(f,sep="\t")
f.write("\n")
np.array(sdCoef).tofile(f,sep="\t")
f.write("\n")
np.array(pSup).tofile(f,sep="\t")
f.write("\n")
np.array(errOut).tofile(f,sep="\t")
f.write("\n")
np.array(errIn).tofile(f,sep="\t")
f.write("\n")
p.tofile(f,sep="\t")
f.write("\n")
f.close()
def getTStat(X,y,alpha,lam,nSamp=100):
# here we are doing residual bootstrap
# to identify the std err and report
# the t-stat (mean/st err)
nObs,nRegs = X.shape
# sd is done by res boot so we need to get the res
enm = enet.fit(X,y, alpha,lambdas=[lam])
yHat = enm.predict(X)[:,0]
res = y - yHat
resCent = res-np.mean(res)
ySample = np.zeros((nObs,nSamp))
# now we need the samples
for i in range(nSamp):
resSample = st.sampleWR(resCent)
ySample[:,i] = yHat+resSample
# residual bs time
sc = np.zeros(nRegs)
sSqc = np.zeros(nRegs)
for i in range(nSamp):
# need the coef
# they change so we need to map the back to the original
tmpEnm = enet.fit(X,ySample[:,i], alpha,lambdas=[lam])
sc[tmpEnm.indices] = sc[tmpEnm.indices] + tmpEnm.coef[:,0]
sSqc[tmpEnm.indices] = sSqc[tmpEnm.indices] + tmpEnm.coef[:,0]**2
# get averages and variances
aveCoef = sc/float(nSamp)
sdCoef = np.sqrt(sSqc/float(nSamp) - aveCoef**2)
# get tstat
# due to the sparsity of lasso
# its possible for a coef to be zero
# on all samples, thus a zero st error
# we are going to remove the zeros
sdCoef[sdCoef<1E-52] = 1E-52
tStat = np.abs(aveCoef/sdCoef)
return tStat