{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Quickstart with Leaspy\n\nThis example demonstrates how to quickly use Leaspy with properly formatted data.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leaspy uses its own data container. To use it correctly, you need to provide either\na CSV file or a pandas.DataFrame in *long format*.\n\nBelow is an example of synthetic longitudinal data illustrating how to use Leaspy:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from leaspy.datasets import load_dataset\n\nalzheimer_df = load_dataset(\"alzheimer\")\nprint(alzheimer_df.columns)\nalzheimer_df = alzheimer_df[[\"MMSE\", \"RAVLT\", \"FAQ\", \"FDG PET\"]]\nprint(alzheimer_df.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data correspond to repeated visits (`TIME` index) of different participants (`ID` index).\nEach visit corresponds to the measurement of 4 different outcomes : the MMSE, the RAVLT, the FAQ and the FDG PET.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{warning}\nYou **MUST** include both `ID` and `TIME`, either as indices or as columns.\nThe remaining columns should correspond to the observed variables\n(also called features or endpoints).\nEach feature should have its own column, and each visit should occupy one row.\n```\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{warning}\n- Leaspy supports *linear* and *logistic* models.\n- The features **MUST** be increasing over time.\n- For logistic models, data must be rescaled between 0 and 1.\n```\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from leaspy.io.data import Data, Dataset\n\ndata = Data.from_dataframe(alzheimer_df)\ndataset = Dataset(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The core functionality of Leaspy is to estimate the group-average trajectory\nof the variables measured in a population. To do this, you need to choose a model.\nFor example, a logistic model can be initialized and fitted as follows:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from leaspy.models import LogisticModel\n\nmodel = LogisticModel(name=\"test-model\", source_dimension=2)\nmodel.fit(\n dataset,\n \"mcmc_saem\",\n seed=42,\n n_iter=100,\n progress_bar=False,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leaspy can also estimate the *individual trajectories* of each participant.\nThis is done using a personalization algorithm, here `scipy_minimize`:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "individual_parameters = model.personalize(\n dataset, \"scipy_minimize\", seed=0, progress_bar=False\n)\nprint(individual_parameters.to_dataframe())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To go further;\n\n1. See the [User Guide](../user_guide.md) and full API documentation.\n2. Explore additional [examples](./index.rst).\n\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 }