Joint Model¶
This notebook contains the code for a simple implementation of the Leaspy Joint model on synthetic data.
The following imports are required libraries for numerical computation and data manipulation.
import os
import pandas as pd
import leaspy
from leaspy.io.data import Data
leaspy_root = os.path.dirname(leaspy.__file__)
data_path = os.path.join(leaspy_root, "datasets/data/simulated_data_for_joint.csv")
df = pd.read_csv(data_path, dtype={"ID": str}, sep=";")
print(df.head())
ID TIME EVENT_TIME EVENT_BOOL Y0 Y1 Y2 Y3
0 116 78.461 85.5 1 0.44444 0.04 0.0 0.0
1 116 78.936 85.5 1 0.60000 0.00 0.0 0.2
2 116 79.482 85.5 1 0.39267 0.04 0.0 0.2
3 116 79.939 85.5 1 0.58511 0.00 0.0 0.0
4 116 80.491 85.5 1 0.57044 0.00 0.0 0.0
To use the Joint Model in Leaspy, your dataset must include the following columns:
ID : Patient identifier
TIME : Time of measurement
EVENT_TIME : Time of the event
EVENT_BOOL : Event indicator:
1if the event occurred0if censored2if a competing event occurred
For one patient, the event time and event bool are the same for each row.
We load the Joint Model from the leaspy.models and transform the dataset in a leaspy-compatible form with the built-in functions.
from leaspy.models import JointModel
data = Data.from_dataframe(df, "joint")
model = JointModel(name="test_model", nb_events=1, source_dimension=2)
The parameter nb_events should match the number of distinct event types
present in the EVENT_BOOL column:
If
EVENT_BOOLcontains values {0, 1}, thennb_events=1.If it contains values {0, 1, 2}, then
nb_events=2.
Once the model is initialized, we can fit it to the data.
model.fit(data, "mcmc_saem", seed=1312, n_iter=100, progress_bar=False)
model.summary()
Fit with `AlgorithmName.FIT_MCMC_SAEM` took: 1.82s
================================================================================
Model Summary
================================================================================
Model Name: test_model
Model Type: JointModel
Features (4): Y0, Y1, Y2, Y3
Sources (2): Source 0 (s0), Source 1 (s1)
Observation Models: gaussian-scalar, weibull-right-censored-with-sources
Neg. Log-Likelihood: -415.8537
Training Metadata
--------------------------------------------------------------------------------
Algorithm: AlgorithmName.FIT_MCMC_SAEM
Seed: 1312
Iterations: 100
Data Context
--------------------------------------------------------------------------------
Subjects: 17
Visits: 157
Total Observations: 628
Leaspy Version: 2.0.1
================================================================================
Population Parameters
--------------------------------------------------------------------------------
log_rho_mean [1.8040]
n_log_nu_mean [-1.9862]
betas_mean:
s0 s1
b0 -0.0163 -0.0908
b1 -0.0079 -0.0469
b2 -0.1058 -0.0088
Y0 Y1 Y2 Y3
log_g_mean 0.1157 2.8874 2.5624 1.3001
Y0 Y1 Y2 Y3
log_v0_mean -3.0789 -3.8272 -3.8023 -2.7624
Individual Parameters
--------------------------------------------------------------------------------
tau_mean [78.4523]
tau_std [5.7890]
xi_std [0.4379]
zeta_mean:
s0 0.0421
s1 0.0660
Noise Model
--------------------------------------------------------------------------------
noise_std 0.0947
================================================================================
The Joint Model includes specific parameters such as log_rho_mean and zeta_mean.
print(model.parameters)
{'betas_mean': tensor([[-0.0163, -0.0908],
[-0.0079, -0.0469],
[-0.1058, -0.0088]]), 'log_g_mean': tensor([0.1157, 2.8874, 2.5624, 1.3001]), 'log_rho_mean': tensor([1.8040]), 'log_v0_mean': tensor([-3.0789, -3.8272, -3.8023, -2.7624]), 'n_log_nu_mean': tensor([-1.9862]), 'noise_std': tensor(0.0947, dtype=torch.float64), 'tau_mean': tensor([78.4523], dtype=torch.float64), 'tau_std': tensor([5.7890], dtype=torch.float64), 'xi_std': tensor([0.4379], dtype=torch.float64), 'zeta_mean': tensor([[0.0421],
[0.0660]])}
We have seend how to fit a Joint Model using Leaspy. It also provides other models as the Mixture Model that can be explored in the next examples.