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,
)
/home/docs/checkouts/readthedocs.org/user_builds/leaspy/envs/v2.0.0/lib/python3.11/site-packages/torch/__init__.py:1240: UserWarning: torch.set_default_tensor_type() is deprecated as of PyTorch 2.1, please use torch.set_default_dtype() and torch.set_default_device() as alternatives. (Triggered internally at /pytorch/torch/csrc/tensor/python_tensor.cpp:434.)
  _C._set_default_tensor_type(t)
Fit with `AlgorithmName.FIT_MCMC_SAEM` took: 4s

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  MDS3_off_total  SCOPA_total  MOCA_total  \
0   0  72.0    0.045091    0.130603        0.170711     0.091289    0.075911   
1   0  73.0    0.238277    0.255318        0.203134     0.080799    0.223998   
2   0  74.0    0.327457    0.177580        0.083596     0.181224    0.090690   
3   0  75.0    0.148452    0.127384        0.346111     0.157972    0.154196   
4   0  76.0    0.071881    0.154933        0.219548     0.217893    0.193991   
5   0  77.0    0.170875    0.112293        0.238554     0.342521    0.094516   
6   0  78.0    0.083887    0.278947        0.238707     0.156327    0.143034   
7   0  79.0    0.194989    0.156856        0.229097     0.338599    0.093225   
8   0  80.0    0.124945    0.203009        0.282244     0.221830    0.187615   
9   0  81.0    0.313573    0.122339        0.368248     0.299989    0.212388   
10  0  82.0    0.275467    0.088046        0.165364     0.291637    0.074643   
11  1  65.0    0.061462    0.052724        0.083583     0.095507    0.191902   
12  1  66.0    0.037952    0.178389        0.176820     0.101080    0.064909   
13  1  67.0    0.036630    0.084424        0.121411     0.103830    0.086220   
14  1  68.0    0.310476    0.082227        0.234535     0.204972    0.099594   
15  1  69.0    0.091273    0.138457        0.193761     0.281317    0.233196   
16  1  70.0    0.088255    0.106881        0.195890     0.073675    0.059517   
17  1  71.0    0.149989    0.149513        0.242512     0.208044    0.364847   
18  1  72.0    0.134920    0.212398        0.301628     0.209863    0.114095   
19  1  73.0    0.075836    0.158912        0.298892     0.129182    0.048058   
20  1  74.0    0.121704    0.130878        0.196568     0.217569    0.107848   
21  1  75.0    0.088827    0.132378        0.278388     0.264793    0.101806   
22  1  76.0    0.230419    0.112283        0.291734     0.218981    0.133827   
23  2  77.0    0.180039    0.254492        0.155162     0.224266    0.154533   
24  2  78.0    0.362797    0.231476        0.277821     0.457270    0.268536   
25  2  79.0    0.153093    0.261720        0.254092     0.298705    0.122845   
26  2  80.0    0.192492    0.264990        0.359893     0.364820    0.161076   
27  2  81.0    0.167312    0.285123        0.395723     0.541428    0.422016   
28  2  82.0    0.388683    0.353420        0.270435     0.422772    0.113674   
29  2  83.0    0.328678    0.375383        0.388405     0.418121    0.152980   
30  2  84.0    0.347513    0.384302        0.376253     0.468451    0.202806   
31  2  85.0    0.318857    0.471523        0.401501     0.458949    0.145164   
32  2  86.0    0.396696    0.460914        0.297262     0.544449    0.134305   
33  2  87.0    0.382152    0.607420        0.418309     0.601106    0.304174   
34  2  88.0    0.272557    0.594934        0.352998     0.665379    0.310562   
35  3  71.0    0.143922    0.125501        0.375007     0.043606    0.083598   
36  3  72.0    0.172016    0.126281        0.340602     0.279560    0.200339   
37  3  73.0    0.090376    0.105161        0.239316     0.239707    0.134464   
38  3  74.0    0.315411    0.038887        0.298126     0.272958    0.095591   
39  3  75.0    0.196721    0.141064        0.198763     0.257857    0.192164   
40  3  76.0    0.287577    0.109757        0.171756     0.306433    0.158913   
41  3  77.0    0.257429    0.223910        0.291375     0.162351    0.069918   
42  3  78.0    0.220348    0.200552        0.242153     0.254385    0.086056   
43  3  79.0    0.121115    0.242452        0.103447     0.285992    0.334548   
44  3  80.0    0.183366    0.239577        0.309764     0.377817    0.270087   
45  3  81.0    0.267518    0.370611        0.265650     0.400841    0.098008   
46  3  82.0    0.196865    0.198366        0.266884     0.325353    0.177566   
47  4  66.0    0.089954    0.102206        0.241578     0.304442    0.082761   
48  4  67.0    0.110769    0.100411        0.315673     0.173225    0.113096   
49  4  68.0    0.277512    0.260042        0.260290     0.293985    0.309596   
50  4  69.0    0.275131    0.266098        0.568255     0.402525    0.194160   
51  4  70.0    0.321732    0.279065        0.432002     0.442905    0.176745   
52  4  71.0    0.445201    0.402643        0.352227     0.481053    0.296754   
53  4  72.0    0.323173    0.337596        0.371451     0.500198    0.355973   
54  4  73.0    0.416748    0.434660        0.503731     0.522047    0.382446   
55  4  74.0    0.406313    0.444701        0.467290     0.635770    0.559336   
56  4  75.0    0.369568    0.501189        0.407548     0.740296    0.470449   
57  4  76.0    0.567106    0.829043        0.644239     0.840395    0.484124   
58  4  77.0    0.536249    0.567635        0.493601     0.550171    0.523645   

    REM_total  PUTAMEN_R  PUTAMEN_L  CAUDATE_R  CAUDATE_L  
0    0.326648   0.586338   0.694107   0.486038   0.462254  
1    0.434207   0.827611   0.739770   0.391800   0.442121  
2    0.352540   0.617009   0.774088   0.442674   0.522004  
3    0.528538   0.747597   0.618830   0.518245   0.484454  
4    0.386632   0.872402   0.777369   0.380526   0.436786  
5    0.394664   0.616315   0.777363   0.617085   0.585828  
6    0.631024   0.678667   0.709021   0.593062   0.562453  
7    0.510269   0.794338   0.676370   0.546787   0.581273  
8    0.503600   0.818333   0.871542   0.605773   0.596467  
9    0.575652   0.732683   0.733573   0.578311   0.630377  
10   0.457606   0.859975   0.734046   0.630789   0.708451  
11   0.211168   0.768930   0.699682   0.635471   0.620317  
12   0.185577   0.728717   0.713026   0.671148   0.527067  
13   0.286919   0.678229   0.787073   0.626300   0.586116  
14   0.394963   0.868946   0.694668   0.530821   0.629706  
15   0.269720   0.809249   0.761141   0.704395   0.681131  
16   0.302220   0.631296   0.773845   0.700289   0.571707  
17   0.320505   0.827874   0.813270   0.647327   0.698253  
18   0.279034   0.817676   0.828690   0.762435   0.548397  
19   0.345372   0.893789   0.861582   0.879562   0.750317  
20   0.453422   0.947860   0.902041   0.768460   0.896986  
21   0.475681   0.962790   0.862242   0.752949   0.702546  
22   0.358865   0.928457   0.796154   0.890086   0.837038  
23   0.429754   0.580118   0.671636   0.268889   0.506761  
24   0.390404   0.717723   0.762220   0.509127   0.389911  
25   0.399220   0.743882   0.871702   0.369679   0.465088  
26   0.450046   0.718046   0.744557   0.489580   0.437064  
27   0.579591   0.723546   0.838607   0.465474   0.553612  
28   0.430855   0.788521   0.714022   0.498124   0.643514  
29   0.412157   0.817420   0.867711   0.670128   0.537423  
30   0.728184   0.866636   0.909693   0.666931   0.688474  
31   0.705184   0.825847   0.930431   0.693197   0.821572  
32   0.609044   0.825532   0.955276   0.658320   0.762378  
33   0.691247   0.808048   0.945092   0.550747   0.964239  
34   0.672684   0.854356   0.845147   0.652172   0.865204  
35   0.486181   0.829522   0.716224   0.560258   0.394031  
36   0.420408   0.839702   0.663012   0.578177   0.544152  
37   0.359053   0.629525   0.621988   0.540873   0.528242  
38   0.344606   0.752880   0.776862   0.487030   0.570666  
39   0.410437   0.656528   0.746326   0.570133   0.661573  
40   0.374546   0.755665   0.802393   0.650199   0.610944  
41   0.608595   0.670235   0.888710   0.603651   0.750991  
42   0.408269   0.805031   0.939145   0.750404   0.587975  
43   0.665959   0.861183   0.917273   0.768473   0.763192  
44   0.521304   0.905549   0.812050   0.823772   0.676197  
45   0.452963   0.736068   0.893073   0.814810   0.785287  
46   0.633318   0.896292   0.870405   0.835049   0.939467  
47   0.174034   0.704538   0.815031   0.478320   0.560745  
48   0.134000   0.807849   0.754326   0.461581   0.642307  
49   0.161544   0.821036   0.879163   0.673620   0.602157  
50   0.190457   0.775704   0.771499   0.559432   0.659072  
51   0.363751   0.867058   0.879247   0.889137   0.760731  
52   0.199559   0.818311   0.862290   0.798646   0.853982  
53   0.419013   0.692159   0.842417   0.959424   0.926155  
54   0.439691   0.906980   0.993150   0.908726   0.903987  
55   0.351311   0.973279   0.937073   0.889652   0.974006  
56   0.412637   0.950911   0.708308   0.967897   0.956320  
57   0.653084   0.937758   1.000000   0.999993   0.999992  
58   0.642088   0.623933   1.000000   0.999980   1.000000