{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Joint Model\nThis notebook contains the code for a simple implementation of the Leaspy Joint model on synthetic data.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following imports are required libraries for numerical computation and data manipulation.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nimport pandas as pd\nimport leaspy\nfrom leaspy.io.data import Data\n\nleaspy_root = os.path.dirname(leaspy.__file__)\ndata_path = os.path.join(leaspy_root, \"datasets/data/simulated_data_for_joint.csv\")\n\ndf = pd.read_csv(data_path, dtype={\"ID\": str}, sep=\";\")\nprint(df.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use the Joint Model in Leaspy, your dataset must include the following columns:\n\n1. **ID** : Patient identifier\n2. **TIME** : Time of measurement\n3. **EVENT_TIME** : Time of the event\n4. **EVENT_BOOL** : Event indicator:\n - `1` if the event occurred\n - `0` if censored\n - `2` if a competing event occurred\n\nFor one patient, the event time and event bool are the same for each row.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We load the Joint Model from the `leaspy.models` and transform the dataset in a leaspy-compatible form with the built-in functions.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from leaspy.models import JointModel\n\ndata = Data.from_dataframe(df, \"joint\")\nmodel = JointModel(name=\"test_model\", nb_events=1, source_dimension=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The parameter `nb_events` should match the number of distinct event types\npresent in the `EVENT_BOOL` column:\n\n- If `EVENT_BOOL` contains values {0, 1}, then `nb_events=1`.\n- If it contains values {0, 1, 2}, then `nb_events=2`.\n\nOnce the model is initialized, we can fit it to the data.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "model.fit(data, \"mcmc_saem\", seed=1312, n_iter=100, progress_bar=False)\nmodel.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Joint Model includes specific parameters such as `log_rho_mean` and `zeta_mean`.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(model.parameters)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have seend how to fit a Joint Model using Leaspy. It also provides other models as the\n[Mixture Model](./plot_04_mixture) that can be explored in the next examples.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.12" } }, "nbformat": 4, "nbformat_minor": 0 }