Simulating Data with Leaspy#

This example demonstrates how to use Leaspy to simulate longitudinal data based on a fitted model.

The following imports bring in the required modules and load the synthetic Parkinson dataset from Leaspy. A logistic model will be fitted on this dataset and then used to simulate new longitudinal data.

from leaspy.datasets import load_dataset
from leaspy.io.data import Data

df = load_dataset("parkinson")

The clinical and imaging features of interest are selected and the DataFrame is converted into a Leaspy Data object that can be used for model fitting.

data = Data.from_dataframe(
    df[
        [
            "MDS1_total",
            "MDS2_total",
            "MDS3_off_total",
            "SCOPA_total",
            "MOCA_total",
            "REM_total",
            "PUTAMEN_R",
            "PUTAMEN_L",
            "CAUDATE_R",
            "CAUDATE_L",
        ]
    ]
)

A logistic model with a two-dimensional latent space is initialized.

from leaspy.models import LogisticModel

model = LogisticModel(name="test-model", source_dimension=2)

The model is fitted to the data using the MCMC-SAEM algorithm. A fixed seed is used for reproducibility and 100 iterations are performed.

model.fit(
    data,
    "mcmc_saem",
    n_iter=100,
    progress_bar=False,
)
Fit with `AlgorithmName.FIT_MCMC_SAEM` took: 7s

The parameters for simulating patient visits are defined. These parameters specify the number of patients, the visit spacing, and the timing variability.

visit_params = {
    "patient_number": 5,
    "visit_type": "random",  # The visit type could also be 'dataframe' with df_visits.
    # "df_visits": df_test           # Example for custom visit schedule.
    "first_visit_mean": 0.0,  # The mean of the first visit age/time.
    "first_visit_std": 0.4,  # The standard deviation of the first visit age/time.
    "time_follow_up_mean": 11,  # The mean follow-up time.
    "time_follow_up_std": 0.5,  # The standard deviation of the follow-up time.
    "distance_visit_mean": 2 / 12,  # The mean spacing between visits in years.
    "distance_visit_std": 0.75
    / 12,  # The standard deviation of the spacing between visits in years.
    "min_spacing_between_visits": 1,  # The minimum allowed spacing between visits.
}

A new longitudinal dataset is simulated from the fitted model using the specified parameters.

df_sim = model.simulate(
    algorithm="simulate",
    features=[
        "MDS1_total",
        "MDS2_total",
        "MDS3_off_total",
        "SCOPA_total",
        "MOCA_total",
        "REM_total",
        "PUTAMEN_R",
        "PUTAMEN_L",
        "CAUDATE_R",
        "CAUDATE_L",
    ],
    visit_parameters=visit_params,
)
Simulate with `simulate` took: 0s

The simulated data is converted back to a pandas DataFrame for inspection.

The simulated longitudinal dataset is displayed below.

print(df_sim)
   ID  TIME  MDS1_total  MDS2_total  ...  PUTAMEN_R  PUTAMEN_L  CAUDATE_R  CAUDATE_L
0   0  59.0    0.189939    0.112294  ...   0.610849   0.806895   0.347635   0.404080
1   0  60.0    0.194089    0.066156  ...   0.501993   0.776347   0.540903   0.561378
2   0  61.0    0.030512    0.175505  ...   0.717203   0.693641   0.370752   0.572413
3   0  62.0    0.119541    0.156830  ...   0.738911   0.726752   0.502189   0.493689
4   0  63.0    0.086805    0.224038  ...   0.786160   0.829731   0.566986   0.551572
5   0  64.0    0.141513    0.291274  ...   0.782231   0.841130   0.720109   0.607043
6   0  65.0    0.272452    0.286645  ...   0.895019   0.820283   0.824561   0.606303
7   0  66.0    0.245043    0.258475  ...   0.807644   0.911036   0.743751   0.780685
8   0  67.0    0.277930    0.157324  ...   0.809695   0.913771   0.662261   0.870641
9   0  68.0    0.308450    0.328545  ...   0.804564   0.830837   0.670492   0.796148
10  0  69.0    0.324895    0.325250  ...   0.850327   0.858201   0.868501   0.927926
11  0  70.0    0.319666    0.443039  ...   0.911180   0.931071   0.861369   0.830026
12  1  65.0    0.121414    0.222224  ...   0.828942   0.691873   0.564565   0.688706
13  1  66.0    0.040760    0.108624  ...   0.814398   0.876737   0.519739   0.474029
14  1  67.0    0.129288    0.455297  ...   0.854192   0.906250   0.675482   0.615470
15  1  68.0    0.044313    0.160242  ...   0.849999   0.893450   0.820203   0.594268
16  1  69.0    0.045160    0.169726  ...   0.770018   0.696688   0.718772   0.722142
17  1  70.0    0.047836    0.194882  ...   0.907968   0.855295   0.814611   0.688449
18  1  71.0    0.065118    0.138083  ...   0.763811   0.626895   0.874124   0.846976
19  1  72.0    0.195403    0.198115  ...   0.936061   0.969157   0.850622   0.782209
20  1  73.0    0.055530    0.149858  ...   0.837723   0.962256   0.879176   0.752671
21  1  74.0    0.260755    0.291046  ...   0.860677   0.831510   0.696963   0.867249
22  1  75.0    0.264688    0.161277  ...   0.913787   0.941464   0.720730   0.838277
23  1  76.0    0.098442    0.186268  ...   0.900725   0.875494   0.869940   0.942322
24  2  68.0    0.182791    0.086153  ...   0.577744   0.629605   0.467519   0.532292
25  2  69.0    0.243044    0.070629  ...   0.887326   0.751850   0.550713   0.550929
26  2  70.0    0.118436    0.119586  ...   0.808526   0.891738   0.509327   0.726000
27  2  71.0    0.261068    0.221197  ...   0.663529   0.844126   0.825212   0.682973
28  2  72.0    0.256566    0.512039  ...   0.899953   0.938835   0.772484   0.662235
29  2  73.0    0.428748    0.240621  ...   0.726195   0.897710   0.860066   0.859125
30  2  74.0    0.326572    0.509671  ...   0.863287   0.873742   0.849937   0.863990
31  2  75.0    0.525620    0.434300  ...   0.991943   0.922922   0.860801   0.867301
32  2  76.0    0.430416    0.471770  ...   0.969796   0.961168   0.964843   0.957182
33  2  77.0    0.622178    0.624307  ...   0.998086   0.993630   0.992073   0.766310
34  2  78.0    0.581200    0.632386  ...   0.992706   0.962883   0.747308   0.999341
35  3  56.0    0.134610    0.124035  ...   0.717391   0.731399   0.534670   0.342360
36  3  57.0    0.078028    0.236096  ...   0.780150   0.736094   0.476870   0.350677
37  3  58.0    0.080540    0.235337  ...   0.743304   0.721075   0.494028   0.464229
38  3  59.0    0.246473    0.286679  ...   0.608832   0.675320   0.360504   0.405648
39  3  60.0    0.157468    0.184091  ...   0.798021   0.695018   0.626350   0.620815
40  3  61.0    0.447728    0.423680  ...   0.791102   0.751383   0.525980   0.488774
41  3  62.0    0.292670    0.243179  ...   0.811854   0.711317   0.614433   0.697378
42  3  63.0    0.280289    0.192707  ...   0.930976   0.875044   0.637860   0.566104
43  3  64.0    0.290556    0.348490  ...   0.774849   0.850594   0.586130   0.646002
44  3  65.0    0.233718    0.353004  ...   0.896448   0.782456   0.776951   0.701641
45  3  66.0    0.383449    0.168052  ...   0.822230   0.946817   0.715118   0.760198
46  3  67.0    0.493583    0.394476  ...   0.902239   0.952384   0.833269   0.810425
47  3  68.0    0.376743    0.231466  ...   0.652938   0.855918   0.755145   0.764546
48  4  71.0    0.101106    0.168326  ...   0.814374   0.781793   0.525062   0.340506
49  4  72.0    0.246301    0.112069  ...   0.519374   0.665495   0.510266   0.452574
50  4  73.0    0.134864    0.161323  ...   0.894641   0.645769   0.476589   0.489455
51  4  74.0    0.123802    0.290941  ...   0.720495   0.768238   0.399612   0.400241
52  4  75.0    0.134085    0.119196  ...   0.827463   0.852753   0.563742   0.568400
53  4  76.0    0.376549    0.225019  ...   0.776040   0.589515   0.560445   0.393720
54  4  77.0    0.390951    0.323386  ...   0.862339   0.707368   0.675938   0.377041
55  4  78.0    0.339961    0.216669  ...   0.827882   0.891702   0.611568   0.641374
56  4  79.0    0.224103    0.276478  ...   0.760263   0.879511   0.680437   0.531347
57  4  80.0    0.294964    0.381846  ...   0.863098   0.739219   0.706242   0.659499
58  4  81.0    0.128703    0.198115  ...   0.812475   0.771272   0.799491   0.599024
59  4  82.0    0.325457    0.293852  ...   0.822453   0.874950   0.778589   0.672610

[60 rows x 12 columns]

Total running time of the script: (0 minutes 7.327 seconds)

Gallery generated by Sphinx-Gallery