-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_pem_survival_model_timevarying.py
73 lines (67 loc) · 2.25 KB
/
test_pem_survival_model_timevarying.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
import matplotlib as mpl
mpl.use('Agg')
import survivalstan
from stancache import stancache
import numpy as np
from nose.tools import ok_
from functools import partial
num_iter = 500
from .test_datasets import load_test_dataset_long, sim_test_dataset_long
model_code = survivalstan.models.pem_survival_model_timevarying
make_inits = None
def test_pem_model(**kwargs):
''' Test survival model on test dataset
'''
dlong = load_test_dataset_long()
testfit = survivalstan.fit_stan_survival_model(
model_cohort = 'test model',
model_code = model_code,
df = dlong,
sample_col = 'index',
timepoint_end_col = 'end_time',
event_col = 'end_failure',
formula = '~ age + sex',
iter = num_iter,
chains = 2,
seed = 9001,
make_inits = make_inits,
FIT_FUN = stancache.cached_stan_fit,
**kwargs
)
ok_('fit' in testfit)
ok_('coefs' in testfit)
ok_('loo' in testfit)
survivalstan.utils.plot_coefs([testfit])
survivalstan.utils.plot_coefs([testfit], trans=np.exp)
survivalstan.utils.plot_coefs([testfit], trans=np.exp, element='grp_coefs')
survivalstan.utils.plot_coefs([testfit], element='baseline')
survivalstan.utils.plot_pp_survival([testfit])
return(testfit)
def test_pem_null_model(force=True, **kwargs):
''' Test NULL survival model on flchain dataset
'''
dlong = load_test_dataset_long()
testfit = survivalstan.fit_stan_survival_model(
model_cohort = 'test model',
model_code = model_code,
df = dlong,
sample_col = 'index',
timepoint_end_col = 'end_time',
event_col = 'end_failure',
formula = '~ 1',
iter = num_iter,
chains = 2,
seed = 9001,
make_inits = make_inits,
FIT_FUN = stancache.cached_stan_fit,
**kwargs
)
ok_('fit' in testfit)
ok_('coefs' in testfit)
ok_('loo' in testfit)
survivalstan.utils.plot_coefs([testfit])
survivalstan.utils.plot_coefs([testfit], trans=np.exp)
survivalstan.utils.plot_coefs([testfit], trans=np.exp, element='grp_coefs')
survivalstan.utils.plot_coefs([testfit], element='baseline')
survivalstan.utils.plot_pp_survival([testfit])
return(testfit)