/usr/share/pyshared/statsmodels/sandbox/cox.py is in python-statsmodels 0.4.2-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | '''Cox proportional hazards regression model.
some dimension problems
fixed import errors
currently produces parameter estimate but then raises exception for other results
finally, after running the script several times, I get a OSError with too many
open file handles
updates and changes :
as of 2010-05-15
AttributeError: 'CoxPH' object has no attribute 'cachedir'
Traceback (most recent call last):
File "C:\...\scikits\statsmodels\sandbox\cox.py", line 244, in <module>
res = c.newton([0.4])
AttributeError: 'CoxPH' object has no attribute 'newton'
replaced newton by call to new fit method for mle with bfgs
feels very slow
need testcase before trying to fix
'''
import shutil
import tempfile
import numpy as np
from statsmodels.base import model
import survival
class Discrete(object):
"""
A simple little class for working with discrete random vectors.
Note: assumes x is 2-d and observations are in 0 axis, variables in 1 axis
"""
def __init__(self, x, w=None):
self.x = np.squeeze(x)
if self.x.shape == ():
self.x = np.array([self.x])
## #JP added and removed again b/c still broadcast error
## if self.x.ndim == 1:
## self.x = self.x[:,None]
self.n = self.x.shape[0]
if w is None:
w = np.ones(self.n, np.float64)
else:
if w.shape[0] != self.n:
raise ValueError('incompatible shape for weights w')
if np.any(np.less(w, 0)):
raise ValueError('weights should be non-negative')
self.w = w*1.0 / w.sum()
def mean(self, f=None): #JP: this is expectation, "expect" in mine
if f is None:
fx = self.x
else:
fx = f(self.x)
return (fx * self.w).sum()
def cov(self):
mu = self.mean() #JP: call to method (confusing name)
dx = self.x - mu#np.multiply.outer(mu, self.x.shape[1])
return np.dot(dx, np.transpose(dx))
## if dx.ndim == 1:
## dx = dx[:,None]
## return np.dot(dx.T, dx)
class Observation(survival.RightCensored):
def __getitem__(self, item):
if self.namespace is not None:
return self.namespace[item]
else:
return getattr(self, item)
def __init__(self, time, delta, namespace=None):
self.namespace = namespace
survival.RightCensored.__init__(self, time, delta)
def __call__(self, formula, time=None, **extra):
return formula(namespace=self, time=time, **extra)
class CoxPH(model.LikelihoodModel):
"""Cox proportional hazards regression model."""
def __init__(self, subjects, formula, time_dependent=False):
self.subjects, self.formula = subjects, formula
self.time_dependent = time_dependent
self.initialize(self.subjects)
def initialize(self, subjects):
print 'called initialize'
self.failures = {}
for i in range(len(subjects)):
s = subjects[i]
if s.delta:
if s.time not in self.failures:
self.failures[s.time] = [i]
else:
self.failures[s.time].append(i)
self.failure_times = self.failures.keys()
self.failure_times.sort()
def cache(self):
if self.time_dependent:
self.cachedir = tempfile.mkdtemp()
self.design = {}
self.risk = {}
first = True
for t in self.failures.keys():
if self.time_dependent:
d = np.array([s(self.formula, time=t)
for s in self.subjects]).astype(float)[:,None]
dshape = d.shape
dfile = file(tempfile.mkstemp(dir=self.cachedir)[1], 'w')
d.tofile(dfile)
dfile.close()
del(d)
self.design[t] = np.memmap(dfile.name,
dtype=np.dtype(float),
shape=dshape)
elif first:
d = np.array([s(self.formula, time=t)
for s in self.subjects]).astype(np.float64)
self.design[t] = d
else:
self.design[t] = d
self.risk[t] = np.compress([s.atrisk(t) for s in self.subjects],
np.arange(self.design[t].shape[0]),axis=-1)
# this raised exception on exit,
def __del__(self):
try:
shutil.rmtree(self.cachedir, ignore_errors=True)
except AttributeError:
print "AttributeError: 'CoxPH' object has no attribute 'cachedir'"
pass
def loglike(self, b, ties='breslow'):
logL = 0
for t in self.failures.keys():
fail = self.failures[t]
d = len(fail)
risk = self.risk[t]
Zb = np.dot(self.design[t], b)
logL += Zb[fail].sum()
if ties == 'breslow':
s = np.exp(Zb[risk]).sum()
logL -= np.log(np.exp(Zb[risk]).sum()) * d
elif ties == 'efron':
s = np.exp(Zb[risk]).sum()
r = np.exp(Zb[fail]).sum()
for j in range(d):
logL -= np.log(s - j * r / d)
elif ties == 'cox':
raise NotImplementedError('Cox tie breaking method not \
implemented')
else:
raise NotImplementedError('tie breaking method not recognized')
return logL
def score(self, b, ties='breslow'):
score = 0
for t in self.failures.keys():
fail = self.failures[t]
d = len(fail)
risk = self.risk[t]
Z = self.design[t]
score += Z[fail].sum()
if ties == 'breslow':
w = np.exp(np.dot(Z, b))
rv = Discrete(Z[risk], w=w[risk])
score -= rv.mean() * d
elif ties == 'efron':
w = np.exp(np.dot(Z, b))
score += Z[fail].sum()
for j in range(d):
efron_w = w
efron_w[fail] -= i * w[fail] / float(d)
rv = Discrete(Z[risk], w=efron_w[risk])
score -= rv.mean()
elif ties == 'cox':
raise NotImplementedError('Cox tie breaking method not \
implemented')
else:
raise NotImplementedError('tie breaking method not recognized')
return np.array([score])
def information(self, b, ties='breslow'):
info = 0 #np.zeros((len(b),len(b))) #0
score = 0
for t in self.failures.keys():
fail = self.failures[t]
d = len(fail)
risk = self.risk[t]
Z = self.design[t]
if ties == 'breslow':
w = np.exp(np.dot(Z, b))
rv = Discrete(Z[risk], w=w[risk])
info += rv.cov()
elif ties == 'efron':
w = np.exp(np.dot(Z, b))
score += Z[fail].sum()
for j in range(d):
efron_w = w
efron_w[fail] -= i * w[fail] / d
rv = Discrete(Z[risk], w=efron_w[risk])
info += rv.cov()
elif ties == 'cox':
raise NotImplementedError('Cox tie breaking method not \
implemented')
else:
raise NotImplementedError('tie breaking method not recognized')
return score
if __name__ == '__main__':
import numpy.random as R
n = 100
X = np.array([0]*n + [1]*n)
b = 0.4
lin = 1 + b*X
Y = R.standard_exponential((2*n,)) / lin
delta = R.binomial(1, 0.9, size=(2*n,))
subjects = [Observation(Y[i], delta[i]) for i in range(2*n)]
for i in range(2*n):
subjects[i].X = X[i]
import statsmodels.sandbox.formula as F
x = F.Quantitative('X')
f = F.Formula(x)
c = CoxPH(subjects, f)
# c.cache()
# temp file cleanup doesn't work on windows
c = CoxPH(subjects, f, time_dependent=True)
c.cache() #this creates tempfile cache,
# no tempfile cache is created in normal use of CoxPH
#res = c.newton([0.4]) #doesn't work anymore
res=c.fit([0.4],method="bfgs")
print res.params
print dir(c)
#print c.fit(Y)
#c.information(res.params) #raises exception
'''
Note: Replacement for c.newton
>>> c.fit()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
c.fit()
File "C:\Josef\eclipsegworkspace\statsmodels-josef-experimental\scikits\statsmodels\model.py", line 132, in fit
start_params = [0]*self.exog.shape[1] # will fail for shape (K,)
AttributeError: 'CoxPH' object has no attribute 'exog'
>>> c.fit([0.4])
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
c.fit([0.4])
File "C:\Josef\eclipsegworkspace\statsmodels-josef-experimental\scikits\statsmodels\model.py", line 148, in fit
H = self.hessian(history[-1])
File "C:\Josef\eclipsegworkspace\statsmodels-josef-experimental\scikits\statsmodels\model.py", line 115, in hessian
raise NotImplementedError
NotImplementedError
>>> c.fit([0.4],method="bfgs")
Optimization terminated successfully.
Current function value: 802.354181
Iterations: 3
Function evaluations: 5
Gradient evaluations: 5
<statsmodels.model.LikelihoodModelResults object at 0x01D48B70>
>>> res=c.fit([0.4],method="bfgs")
Optimization terminated successfully.
Current function value: 802.354181
Iterations: 3
Function evaluations: 5
Gradient evaluations: 5
>>> res.params
array([ 0.34924421])
'''
|