-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpermSLR_v2.py
205 lines (137 loc) · 3.66 KB
/
permSLR_v2.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
#####
# 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
import SLR2
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.abs(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
allTStat = getTStat(X,yPerm)
tStatPerm[:,i] = allTStat[coefIndex]
# 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))
if p[i]>1.0:p[i]=1.0
# 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,nSamp=100,alphaList=np.array([1])):
nObs,nRegs = X.shape
slr = SLR2.SLR(X,y)
slr.fit(nSamp,alphaList)
slr.estStErr(nSamp)
aveCoef = np.zeros(nRegs)
sdCoef = np.zeros(nRegs)
if slr._notEmpty:
aveCoef[slr._coefIndex] = slr._aveCoef
sdCoef[slr._coefIndex] = slr._sdCoef
sdCoef[sdCoef<1E-52] = 1E-52
tStat = np.abs(aveCoef/sdCoef)
return tStat