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/2.0.1/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 57.0 0.185456 0.166414 0.298144 0.224136 0.060019
1 0 58.0 0.024288 0.028722 0.249967 0.098460 0.024833
2 0 59.0 0.010112 0.122865 0.114463 0.101750 0.017764
3 0 60.0 0.097179 0.056997 0.260436 0.178545 0.123906
4 0 61.0 0.121287 0.234458 0.225399 0.085233 0.182336
5 0 62.0 0.291544 0.122587 0.253592 0.090851 0.203891
6 0 63.0 0.217859 0.077627 0.253423 0.067522 0.088520
7 0 64.0 0.053584 0.239152 0.320299 0.214324 0.151153
8 0 65.0 0.106883 0.118058 0.263060 0.364320 0.076009
9 0 66.0 0.242047 0.201949 0.355222 0.295888 0.398629
10 0 67.0 0.303816 0.246917 0.298883 0.317110 0.146587
11 0 68.0 0.213063 0.371478 0.181284 0.142468 0.129056
12 1 63.0 0.158852 0.258909 0.178057 0.216347 0.053582
13 1 64.0 0.103495 0.219636 0.207823 0.357773 0.068490
14 1 65.0 0.268823 0.313132 0.301288 0.176670 0.236788
15 1 66.0 0.118599 0.333927 0.249874 0.302159 0.049959
16 1 67.0 0.277304 0.162151 0.288054 0.374241 0.160042
17 1 68.0 0.155204 0.317638 0.169332 0.155621 0.079430
18 1 69.0 0.247819 0.335777 0.349311 0.358967 0.103141
19 1 70.0 0.097224 0.221069 0.418659 0.292242 0.226511
20 1 71.0 0.234103 0.247215 0.454773 0.469139 0.124735
21 1 72.0 0.369189 0.193758 0.371450 0.293640 0.052970
22 1 73.0 0.213236 0.167062 0.453822 0.413418 0.324109
23 1 74.0 0.283424 0.244889 0.266367 0.367031 0.109127
24 2 63.0 0.183726 0.235106 0.355931 0.137962 0.089983
25 2 64.0 0.098704 0.077715 0.212624 0.253406 0.113259
26 2 65.0 0.222894 0.118473 0.175831 0.255742 0.033376
27 2 66.0 0.152089 0.186077 0.248942 0.345893 0.185916
28 2 67.0 0.142293 0.281357 0.295486 0.238349 0.333400
29 2 68.0 0.206771 0.227103 0.181617 0.363681 0.090476
30 2 69.0 0.165016 0.234406 0.277210 0.334226 0.219883
31 2 70.0 0.250223 0.207059 0.364663 0.336639 0.125513
32 2 71.0 0.161128 0.251041 0.362324 0.336403 0.113719
33 2 72.0 0.300383 0.269335 0.296948 0.346521 0.514978
34 2 73.0 0.093835 0.196749 0.218154 0.334323 0.243519
35 2 74.0 0.145825 0.149897 0.207967 0.296083 0.104263
36 2 75.0 0.159833 0.271455 0.269154 0.444666 0.214130
37 3 86.0 0.003206 0.053748 0.105839 0.160547 0.139456
38 3 87.0 0.009640 0.146204 0.083045 0.112748 0.255919
39 3 88.0 0.096344 0.050253 0.355516 0.262181 0.038364
40 3 89.0 0.237845 0.063547 0.318808 0.147792 0.142816
41 3 90.0 0.013605 0.125170 0.282715 0.355501 0.150363
42 3 91.0 0.071441 0.123688 0.226647 0.226228 0.222504
43 3 92.0 0.059208 0.177489 0.221775 0.306830 0.127713
44 3 93.0 0.072511 0.120052 0.239241 0.232286 0.033427
45 3 94.0 0.152136 0.106535 0.237182 0.237111 0.188796
46 3 95.0 0.039191 0.226498 0.333087 0.295066 0.103658
47 3 96.0 0.199192 0.371400 0.448040 0.317752 0.072023
48 4 64.0 0.238490 0.351639 0.260694 0.236429 0.172240
49 4 65.0 0.182194 0.170570 0.355648 0.382414 0.116423
50 4 66.0 0.274478 0.220135 0.185627 0.429081 0.182107
51 4 67.0 0.306054 0.189573 0.353684 0.295036 0.182560
52 4 68.0 0.281089 0.347251 0.291645 0.466427 0.177447
53 4 69.0 0.266278 0.213318 0.375410 0.538161 0.258666
54 4 70.0 0.206015 0.337348 0.323748 0.438849 0.313692
55 4 71.0 0.307786 0.405873 0.313640 0.532069 0.227593
56 4 72.0 0.429215 0.411726 0.389910 0.392940 0.351665
57 4 73.0 0.405849 0.421890 0.401688 0.466693 0.126978
58 4 74.0 0.441857 0.549981 0.549886 0.661442 0.399392
59 4 75.0 0.517730 0.438088 0.411879 0.752910 0.413597
REM_total PUTAMEN_R PUTAMEN_L CAUDATE_R CAUDATE_L
0 0.366838 0.685818 0.540303 0.497356 0.498585
1 0.565159 0.819268 0.627096 0.667588 0.513094
2 0.327577 0.646378 0.665159 0.584963 0.541755
3 0.408485 0.890432 0.677765 0.684854 0.599452
4 0.488553 0.831865 0.790165 0.764967 0.604748
5 0.540533 0.902273 0.767318 0.693171 0.614725
6 0.501633 0.874873 0.875868 0.684169 0.801646
7 0.349205 0.940791 0.839757 0.768182 0.730322
8 0.706459 0.883103 0.901629 0.900101 0.686260
9 0.507529 0.811815 0.827501 0.777410 0.752473
10 0.749409 0.971369 0.823843 0.920883 0.826943
11 0.645573 0.899428 0.869154 0.942046 0.923057
12 0.566339 0.578089 0.701494 0.349587 0.392281
13 0.435994 0.637612 0.674599 0.319268 0.521521
14 0.384073 0.709902 0.763561 0.524642 0.491202
15 0.442591 0.721288 0.772690 0.491484 0.330310
16 0.433512 0.639157 0.741568 0.470048 0.542373
17 0.352477 0.679279 0.850182 0.484119 0.488377
18 0.452865 0.786343 0.814556 0.603779 0.514042
19 0.505312 0.562490 0.847749 0.568118 0.423006
20 0.677082 0.849129 0.710487 0.503356 0.469289
21 0.425752 0.620247 0.747965 0.427637 0.631375
22 0.617137 0.805682 0.657918 0.598088 0.680743
23 0.480039 0.793978 0.861822 0.606581 0.693250
24 0.240086 0.630465 0.766813 0.392340 0.389811
25 0.270343 0.660364 0.794951 0.320470 0.306567
26 0.207559 0.727272 0.696611 0.283616 0.369698
27 0.300273 0.561045 0.757039 0.424202 0.348341
28 0.421474 0.715014 0.773291 0.421001 0.501273
29 0.333058 0.714630 0.704178 0.485412 0.625078
30 0.175604 0.629435 0.852404 0.393655 0.592382
31 0.370504 0.735322 0.856485 0.660396 0.381262
32 0.372694 0.821901 0.848617 0.465268 0.512477
33 0.350978 0.938598 0.762959 0.646373 0.483078
34 0.342830 0.882943 0.826630 0.588156 0.680755
35 0.421844 0.736339 0.814011 0.764478 0.768954
36 0.439016 0.760630 0.885085 0.524111 0.732516
37 0.243888 0.769106 0.716589 0.521501 0.563842
38 0.369758 0.778395 0.809663 0.449531 0.533623
39 0.257833 0.829915 0.828930 0.538539 0.523296
40 0.391758 0.624129 0.942237 0.829926 0.594611
41 0.256840 0.717669 0.750501 0.768756 0.566988
42 0.270618 0.703321 0.696525 0.571357 0.556197
43 0.317169 0.751706 0.775026 0.489665 0.626398
44 0.281875 0.820316 0.878065 0.531926 0.613271
45 0.284657 0.898290 0.863320 0.640002 0.540727
46 0.287324 0.835077 0.890369 0.626928 0.741234
47 0.337604 0.798869 0.806577 0.636975 0.773898
48 0.352968 0.781993 0.727513 0.470834 0.470635
49 0.391053 0.648755 0.870313 0.568026 0.609542
50 0.403806 0.800144 0.829649 0.508681 0.591538
51 0.175450 0.649516 0.868752 0.665634 0.664036
52 0.346002 0.709247 0.824556 0.657368 0.626552
53 0.447625 0.777221 0.812235 0.757129 0.712036
54 0.442185 0.826065 0.719233 0.742205 0.582813
55 0.630176 0.867987 0.781001 0.823956 0.806363
56 0.419852 0.958842 0.886027 0.856585 0.572830
57 0.598771 0.965982 0.998972 0.910477 0.752813
58 0.524381 0.938983 0.832146 0.884061 0.878342
59 0.632800 0.853250 0.979931 0.847763 0.920002