Note
Go to the end to download the full example code.
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: 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 ... PUTAMEN_R PUTAMEN_L CAUDATE_R CAUDATE_L
0 0 59.0 0.189837 0.108346 ... 0.613526 0.773531 0.529728 0.343298
1 0 60.0 0.193939 0.062798 ... 0.504392 0.703654 0.450950 0.333464
2 0 61.0 0.030214 0.171499 ... 0.719570 0.732159 0.357568 0.451332
3 0 62.0 0.118672 0.152737 ... 0.741038 0.903827 0.491854 0.541575
4 0 63.0 0.085761 0.217857 ... 0.788019 0.862324 0.623736 0.523061
5 0 64.0 0.253548 0.155311 ... 0.647653 0.839016 0.700722 0.570511
6 0 65.0 0.270736 0.280015 ... 0.896325 0.802742 0.658602 0.734675
7 0 66.0 0.242705 0.252954 ... 0.808777 0.911349 0.629066 0.555425
8 0 67.0 0.275077 0.150783 ... 0.810372 0.752424 0.723899 0.673273
9 0 68.0 0.305548 0.321285 ... 0.805010 0.954272 0.424553 0.750659
10 0 69.0 0.321606 0.315903 ... 0.850727 0.888859 0.674979 0.931392
11 0 70.0 0.315664 0.435030 ... 0.911497 0.956316 0.656909 0.836421
12 1 65.0 0.120434 0.218497 ... 0.834261 0.856809 0.460821 0.490007
13 1 66.0 0.039748 0.104961 ... 0.818827 0.875134 0.658549 0.661982
14 1 67.0 0.127471 0.456389 ... 0.857610 0.818448 0.671842 0.655280
15 1 68.0 0.042492 0.155445 ... 0.853172 0.819222 0.633848 0.712116
16 1 69.0 0.043148 0.164907 ... 0.772310 0.786603 0.844956 0.714190
17 1 70.0 0.045784 0.188817 ... 0.910308 0.871313 0.765299 0.670916
18 1 71.0 0.062928 0.132145 ... 0.765939 0.854777 0.694377 0.735795
19 1 72.0 0.235369 0.297400 ... 0.467658 0.969473 0.844676 0.667160
20 1 73.0 0.052771 0.142803 ... 0.839226 0.901286 0.878508 0.786819
21 1 74.0 0.257031 0.284466 ... 0.862392 0.830229 0.675273 0.720498
22 1 75.0 0.259862 0.154542 ... 0.914829 0.931323 0.779060 0.776357
23 1 76.0 0.094235 0.178269 ... 0.901585 0.808526 0.860276 0.852406
24 2 68.0 0.183429 0.084859 ... 0.579350 0.591450 0.456670 0.440429
25 2 69.0 0.243442 0.069445 ... 0.888823 0.780879 0.501207 0.472845
26 2 70.0 0.118660 0.117997 ... 0.810249 0.836815 0.644630 0.726914
27 2 71.0 0.261247 0.219914 ... 0.664394 0.811736 0.768374 0.687428
28 2 72.0 0.256545 0.510885 ... 0.901668 0.850865 0.770073 0.697353
29 2 73.0 0.428611 0.239297 ... 0.726941 0.915015 0.828014 0.601117
30 2 74.0 0.326149 0.508574 ... 0.864233 0.986136 0.843917 0.884380
31 2 75.0 0.524946 0.433232 ... 0.912391 0.921290 0.855718 0.887298
32 2 76.0 0.390189 0.520200 ... 0.967769 0.960393 0.992024 0.902047
33 2 77.0 0.621280 0.623902 ... 0.836225 0.882545 0.999996 0.955342
34 2 78.0 0.578326 0.700083 ... 0.999797 0.962294 0.728558 0.998008
35 3 56.0 0.136721 0.126349 ... 0.627685 0.743683 0.276466 0.399129
36 3 57.0 0.079929 0.238376 ... 0.623884 0.665842 0.393432 0.331060
37 3 58.0 0.081911 0.237763 ... 0.816771 0.666884 0.379940 0.467313
38 3 59.0 0.248201 0.288697 ... 0.729010 0.781295 0.489165 0.575319
39 3 60.0 0.158782 0.185560 ... 0.730521 0.755332 0.621250 0.499584
40 3 61.0 0.448373 0.424977 ... 0.790184 0.869212 0.468225 0.500912
41 3 62.0 0.293297 0.244524 ... 0.791067 0.766308 0.602846 0.645500
42 3 63.0 0.280620 0.193567 ... 0.789296 0.861202 0.613285 0.492166
43 3 64.0 0.290456 0.349138 ... 0.839019 0.893522 0.585771 0.630963
44 3 65.0 0.233064 0.353432 ... 0.847231 0.911536 0.613549 0.667317
45 3 66.0 0.382608 0.168025 ... 0.857093 0.800406 0.878686 0.675758
46 3 67.0 0.492569 0.394168 ... 0.880666 0.869473 0.758846 0.712946
47 3 68.0 0.374840 0.230610 ... 0.892294 0.940573 0.902146 0.704757
48 4 71.0 0.102655 0.169692 ... 0.779729 0.679185 0.331513 0.389448
49 4 72.0 0.206649 0.255846 ... 0.516684 0.661401 0.503707 0.395906
50 4 73.0 0.135903 0.162536 ... 0.525145 0.593628 0.515619 0.482039
51 4 74.0 0.195396 0.323488 ... 0.717338 0.763528 0.670987 0.603190
52 4 75.0 0.158369 0.222417 ... 0.824327 0.848787 0.569383 0.419589
53 4 76.0 0.376903 0.225334 ... 0.902840 0.759858 0.644312 0.400713
54 4 77.0 0.270916 0.169663 ... 0.857615 0.702716 0.542852 0.620256
55 4 78.0 0.246025 0.303298 ... 0.824597 0.888865 0.594372 0.596746
56 4 79.0 0.248526 0.217401 ... 0.756308 0.876039 0.538452 0.629526
57 4 80.0 0.360452 0.233065 ... 0.857708 0.735322 0.566087 0.590673
58 4 81.0 0.298929 0.419842 ... 0.809018 0.767108 0.672858 0.616682
59 4 82.0 0.323508 0.291980 ... 0.930135 0.766822 0.560370 0.791820
[60 rows x 12 columns]
Total running time of the script: (0 minutes 4.255 seconds)