User Guide Tutorial 05: Time-to-event Analysis¶
This tutorial shows how to use TemporAI time_to_event plugins.
All time_to_event plugins¶
Time-to-event analysis, in the context of TemporAI, refers to models that estimate risk over time for each sample. This may also be referred to as survival analysis.
To see all the relevant plugins:
[ ]:
from tempor import plugin_loader
plugin_loader.list()["time_to_event"]
['ts_coxph', 'ts_xgb', 'dynamic_deephit']
Now also load data source(s) we will use:
[ ]:
PBCDataSource = plugin_loader.get_class("time_to_event.pbc", plugin_type="datasource")
Using a time-to-event analysis plugin.¶
[ ]:
from tempor import plugin_loader
dataset = PBCDataSource(random_state=42).load()
print(dataset)
model = plugin_loader.get("time_to_event.dynamic_deephit", n_iter=50)
print(model)
TimeToEventAnalysisDataset(
time_series=TimeSeriesSamples([312, *, 14]),
static=StaticSamples([312, 1]),
predictive=TimeToEventAnalysisTaskData(targets=EventSamples([312, 1]))
)
DynamicDeepHitTimeToEventAnalysis(
name='dynamic_deephit',
category='time_to_event',
plugin_type='method',
params={
'n_iter': 50,
'batch_size': 100,
'lr': 0.001,
'n_layers_hidden': 1,
'n_units_hidden': 40,
'split': 100,
'rnn_mode': 'GRU',
'alpha': 0.34,
'beta': 0.27,
'sigma': 0.21,
'dropout': 0.06,
'device': 'cpu',
'val_size': 0.1,
'patience': 20,
'output_mode': 'MLP',
'random_state': 0
}
)
[ ]:
# Targets:
dataset.predictive.targets
EventSamples with data:
| status | |
|---|---|
| sample_idx | |
| 1 | (0.569488555470374, True) |
| 2 | (14.1523381885883, False) |
| 3 | (0.7365020260650499, True) |
| 4 | (0.27653050049282957, True) |
| 5 | (4.12057824991786, False) |
| ... | ... |
| 308 | (4.98850071186069, False) |
| 309 | (4.55317051801555, False) |
| 310 | (4.4025846019056, False) |
| 311 | (4.12879202716022, False) |
| 312 | (3.98915781404008, False) |
312 rows × 1 columns
[ ]:
# Train.
model.fit(dataset)
DynamicDeepHitTimeToEventAnalysis(
name='dynamic_deephit',
category='time_to_event',
plugin_type='method',
params={
'n_iter': 50,
'batch_size': 100,
'lr': 0.001,
'n_layers_hidden': 1,
'n_units_hidden': 40,
'split': 100,
'rnn_mode': 'GRU',
'alpha': 0.34,
'beta': 0.27,
'sigma': 0.21,
'dropout': 0.06,
'device': 'cpu',
'val_size': 0.1,
'patience': 20,
'output_mode': 'MLP',
'random_state': 0
}
)
[ ]:
# Predict:
model.predict(dataset, horizons=[0.25, 0.50, 0.75])
TimeSeriesSamples with data:
| risk_score | ||
|---|---|---|
| sample_idx | time_idx | |
| 1 | 0.25 | 0.422638 |
| 0.50 | 0.626617 | |
| 0.75 | 0.717204 | |
| 2 | 0.25 | 0.221283 |
| 0.50 | 0.288457 | |
| ... | ... | ... |
| 311 | 0.50 | 0.039307 |
| 0.75 | 0.048353 | |
| 312 | 0.25 | 0.248848 |
| 0.50 | 0.390068 | |
| 0.75 | 0.444567 |
936 rows × 1 columns
Note:¶
The current Dynamic DeepHit implementation has the following limitations: - Only one output feature is supported (no competing risks). - Risk prediction for time points beyond the last event time in the dataset may throw errors.