.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_03_joint.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_03_joint.py: Joint Model ===================================================== This notebook contains the code for a simple implementation of the Leaspy Joint model on synthetic data. .. GENERATED FROM PYTHON SOURCE LINES 8-9 The following imports are required libraries for numerical computation and data manipulation. .. GENERATED FROM PYTHON SOURCE LINES 9-23 .. code-block:: Python 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()) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 24-35 To use the Joint Model in Leaspy, your dataset must include the following columns: 1. **ID** : Patient identifier 2. **TIME** : Time of measurement 3. **EVENT_TIME** : Time of the event 4. **EVENT_BOOL** : Event indicator: - `1` if the event occurred - `0` if censored - `2` if a competing event occurred For one patient, the event time and event bool are the same for each row. .. GENERATED FROM PYTHON SOURCE LINES 38-39 We load the Joint Model from the `leaspy.models` and transform the dataset in a leaspy-compatible form with the built-in functions. .. GENERATED FROM PYTHON SOURCE LINES 39-44 .. code-block:: Python from leaspy.models import JointModel data = Data.from_dataframe(df, "joint") model = JointModel(name="test_model", nb_events=1) .. GENERATED FROM PYTHON SOURCE LINES 45-52 The parameter `nb_events` should match the number of distinct event types present in the `EVENT_BOOL` column: - If `EVENT_BOOL` contains values {0, 1}, then `nb_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. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python model.fit(data, "mcmc_saem", seed=1312, n_iter=100, progress_bar=False) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/leaspy/checkouts/476/src/leaspy/models/time_reparametrized.py:288: UserWarning: You did not provide `source_dimension` hyperparameter for multivariate model, setting it to ⌊√dimension⌋ = 2. warnings.warn( ==> Setting seed to 1312 Fit with `AlgorithmName.FIT_MCMC_SAEM` took: 1s .. GENERATED FROM PYTHON SOURCE LINES 57-58 The Joint Model includes specific parameters such as `log_rho_mean` and `zeta_mean`. .. GENERATED FROM PYTHON SOURCE LINES 58-59 .. code-block:: Python print(model.parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none {'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]])} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.377 seconds) .. _sphx_glr_download_auto_examples_plot_03_joint.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_joint.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_joint.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_joint.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_