@@ -58,7 +58,7 @@ format, then train a Magma model and use it to perform predictions.
58
58
``` r
59
59
library(MagmaClustR )
60
60
# # Simulate a dataset with 11 individuals, each observed at 10 input locations
61
- set.seed(28 )
61
+ set.seed(17 )
62
62
data_magma <- simu_db(M = 11 , N = 10 , common_input = FALSE )
63
63
# # Split individuals into training and prediction sets, and define test points
64
64
magma_train <- data_magma %> % subset(ID %in% 1 : 10 )
@@ -67,20 +67,19 @@ magma_test <- data_magma %>% subset(ID == 11) %>% tail(3)
67
67
68
68
data_magma
69
69
# > # A tibble: 110 x 3
70
- # > ID Output Input
71
- # > <chr> <dbl> <dbl>
72
- # > 1 1 -56.9 0
73
- # > 2 1 -57.4 0.7
74
- # > 3 1 -53.1 1
75
- # > 4 1 -62.8 1.5
76
- # > 5 1 -60.2 1.7
77
- # > 6 1 -81.4 5.2
78
- # > 7 1 -96.2 7
79
- # > 8 1 -102. 7.1
80
- # > 9 1 -96.4 8.05
81
- # > 10 1 -86.4 9.65
70
+ # > ID Input Output
71
+ # > <chr> <dbl> <dbl>
72
+ # > 1 1 1.3 -20.7
73
+ # > 2 1 2.65 -24.2
74
+ # > 3 1 2.8 -24.4
75
+ # > 4 1 3.3 -25.6
76
+ # > 5 1 3.8 -23.9
77
+ # > 6 1 5 -11.4
78
+ # > 7 1 6.1 -4.97
79
+ # > 8 1 6.35 -4.35
80
+ # > 9 1 7.55 -6.63
81
+ # > 10 1 9.8 -8.68
82
82
# > # ... with 100 more rows
83
- # > # i Use `print(n = ...)` to see more rows
84
83
```
85
84
86
85
As displayed above, any dataset processed in MagmaClustR should provide
@@ -97,21 +96,21 @@ model <- train_magma(data = magma_train, common_hp = F)
97
96
# >
98
97
# > The 'ini_hp_i' argument has not been specified. Random values of hyper-parameters for the individal processes are used as initialisation.
99
98
# >
100
- # > EM algorithm, step 1: 8.23 seconds
99
+ # > EM algorithm, step 1: 5.18 seconds
101
100
# >
102
- # > Value of the likelihood: -434.32317 --- Convergence ratio = Inf
101
+ # > Value of the likelihood: -313.47546 --- Convergence ratio = Inf
103
102
# >
104
- # > EM algorithm, step 2: 4.91 seconds
103
+ # > EM algorithm, step 2: 2.7 seconds
105
104
# >
106
- # > Value of the likelihood: -401.89388 --- Convergence ratio = 0.08069
105
+ # > Value of the likelihood: -303.47668 --- Convergence ratio = 0.03295
107
106
# >
108
- # > EM algorithm, step 3: 4.18 seconds
107
+ # > EM algorithm, step 3: 2.51 seconds
109
108
# >
110
- # > Value of the likelihood: -400.50342 --- Convergence ratio = 0.00347
109
+ # > Value of the likelihood: -302.84575 --- Convergence ratio = 0.00208
111
110
# >
112
- # > EM algorithm, step 4: 3.53 seconds
111
+ # > EM algorithm, step 4: 2.04 seconds
113
112
# >
114
- # > Value of the likelihood: -400.21465 --- Convergence ratio = 0.00072
113
+ # > Value of the likelihood: -302.56379 --- Convergence ratio = 0.00093
115
114
# >
116
115
# > The EM algorithm successfully converged, training is completed.
117
116
# >
@@ -187,10 +186,9 @@ clustering and predictions.
187
186
### Data generation
188
187
189
188
``` r
190
-
191
189
# # Simulate a dataset containing 3 clusters of 4 individuals, each observed at 10 input locations
192
- set.seed(2 )
193
- data_magmaclust <- simu_db(M = 4 , N = 10 , K = 3 )
190
+ set.seed(4 )
191
+ data_magmaclust <- simu_db(M = 4 , N = 10 , K = 3 , common_input = FALSE )
194
192
# # Split individuals into training and prediction sets, and define test points
195
193
list_ID = unique(data_magmaclust $ ID )
196
194
magmaclust_train <- data_magmaclust %> % subset(ID %in% list_ID [1 : 11 ])
@@ -199,20 +197,19 @@ magmaclust_test <- data_magmaclust %>% subset(ID == list_ID[12]) %>% tail(5)
199
197
200
198
data_magmaclust
201
199
# > # A tibble: 120 x 3
202
- # > ID Output Input
203
- # > <chr> <dbl> <dbl>
204
- # > 1 ID1-Clust1 -47.0 0.25
205
- # > 2 ID1-Clust1 -39.5 0 .8
206
- # > 3 ID1-Clust1 -29.7 2
207
- # > 4 ID1-Clust1 -38.7 4.2
208
- # > 5 ID1-Clust1 -43.4 4.6
209
- # > 6 ID1-Clust1 -53.6 6.2
210
- # > 7 ID1-Clust1 -57.5 6.75
211
- # > 8 ID1-Clust1 -64.9 7.95
212
- # > 9 ID1-Clust1 -53.3 8.85
213
- # > 10 ID1-Clust1 -42.7 9.85
200
+ # > ID Input Output
201
+ # > <chr> <dbl> <dbl>
202
+ # > 1 ID1-Clust1 0.2 32.4
203
+ # > 2 ID1-Clust1 0.7 28 .8
204
+ # > 3 ID1-Clust1 2.05 6.62
205
+ # > 4 ID1-Clust1 2.1 5.46
206
+ # > 5 ID1-Clust1 3.15 -5.44
207
+ # > 6 ID1-Clust1 5.3 -16.5
208
+ # > 7 ID1-Clust1 6.4 -24.7
209
+ # > 8 ID1-Clust1 7.35 -31.4
210
+ # > 9 ID1-Clust1 7.5 -32.4
211
+ # > 10 ID1-Clust1 8.95 -42.7
214
212
# > # ... with 110 more rows
215
- # > # i Use `print(n = ...)` to see more rows
216
213
```
217
214
218
215
### Training and prediction with MagmaClust
@@ -227,21 +224,29 @@ model_clust <- train_magmaclust(data = magmaclust_train)
227
224
# >
228
225
# > The 'prior_mean' argument has not been specified. The hyper_prior mean function is thus set to be 0 everywhere.
229
226
# >
230
- # > VEM algorithm, step 1: 44.73 seconds
227
+ # > VEM algorithm, step 1: 20.02 seconds
228
+ # >
229
+ # > Value of the elbo: -712.16808 --- Convergence ratio = Inf
230
+ # >
231
+ # > VEM algorithm, step 2: 12.18 seconds
232
+ # >
233
+ # > Value of the elbo: -635.70636 --- Convergence ratio = 0.12028
234
+ # >
235
+ # > VEM algorithm, step 3: 14.54 seconds
231
236
# >
232
- # > Value of the elbo: -454.05837 --- Convergence ratio = Inf
237
+ # > Value of the elbo: -623.0371 --- Convergence ratio = 0.02033
233
238
# >
234
- # > VEM algorithm, step 2: 14.69 seconds
239
+ # > VEM algorithm, step 4: 10.76 seconds
235
240
# >
236
- # > Value of the elbo: -409.05605 --- Convergence ratio = 0.11002
241
+ # > Value of the elbo: -619.56266 --- Convergence ratio = 0.00561
237
242
# >
238
- # > VEM algorithm, step 3: 14.16 seconds
243
+ # > VEM algorithm, step 5: 8.23 seconds
239
244
# >
240
- # > Value of the elbo: -408.61253 --- Convergence ratio = 0.00109
245
+ # > Value of the elbo: -618.50147 --- Convergence ratio = 0.00172
241
246
# >
242
- # > VEM algorithm, step 4: 14 .46 seconds
247
+ # > VEM algorithm, step 6: 10 .46 seconds
243
248
# >
244
- # > Value of the elbo: -408.20937 --- Convergence ratio = 0.00099
249
+ # > Value of the elbo: -617.95297 --- Convergence ratio = 0.00089
245
250
# >
246
251
# > The EM algorithm successfully converged, training is completed.
247
252
# >
@@ -275,9 +280,9 @@ plot_magmaclust(pred = pred_clust,
275
280
data_train = data_train_with_clust ,
276
281
col_clust = TRUE ,
277
282
prior_mean = model_clust $ hyperpost $ mean ,
278
- y_grid = seq(0 , 60 , 0.5 ),
283
+ y_grid = seq(10 , 55 , 0.5 ),
279
284
heatmap = TRUE )
280
- # > The mixture probability of the cluster K3 is 1. Therefore, the predictive distribution is Gaussian and the associated credible interval can be displayed.
285
+ # > The mixture probability of the cluster K1 is 1. Therefore, the predictive distribution is Gaussian and the associated credible interval can be displayed.
281
286
```
282
287
283
288
<img src =" man/figures/README-display_MagmaClust-1.png " width =" 80% " style =" display : block ; margin : auto ;" />
@@ -291,31 +296,28 @@ desired in the model.
291
296
### Data generation
292
297
293
298
``` r
294
- library(MagmaClustR )
295
299
# # Dataset with 11 individuals, 10 reference input locations and a covariate
296
- set.seed(2 )
300
+ set.seed(5 )
297
301
data_dim2 <- simu_db(M = 11 , N = 10 , covariate = TRUE )
298
302
# # Split individuals into training and prediction sets, and define test points
299
303
dim2_train <- data_dim2 %> % subset(ID %in% 1 : 10 )
300
- dim2_pred <- data_dim2 %> % subset(ID == 11 ) %> % head(5 )
301
- dim2_test <- data_dim2 %> % subset(ID == 11 ) %> % tail(5 )
304
+ dim2_pred <- data_dim2 %> % subset(ID == 11 )
302
305
303
306
data_dim2
304
307
# > # A tibble: 110 x 4
305
- # > ID Output Input Covariate
306
- # > <chr> <dbl> <dbl> <dbl>
307
- # > 1 1 -47.0 0.25 -2
308
- # > 2 1 -39.5 0.8 1.94
309
- # > 3 1 -29.7 2 4.64
310
- # > 4 1 -38.7 4.2 -3.7
311
- # > 5 1 -43.4 4.6 -4.24
312
- # > 6 1 -53.6 6.2 0.68
313
- # > 7 1 -57 .5 6.75 0.55
314
- # > 8 1 -64.9 7.95 -4.38
315
- # > 9 1 -53 .3 8.85 1.74
316
- # > 10 1 -42.7 9.85 4.14
308
+ # > ID Input Covariate Output
309
+ # > <chr> <dbl> <dbl> <dbl>
310
+ # > 1 1 1.85 4.5 21.3
311
+ # > 2 1 2.85 8.5 5.21
312
+ # > 3 1 2 4 24.8
313
+ # > 4 1 3.25 7 12.3
314
+ # > 5 1 3.5 2.5 37.0
315
+ # > 6 1 5.3 5.5 20.0
316
+ # > 7 1 6 .5 5 20.8
317
+ # > 8 1 6 2 34.1
318
+ # > 9 1 7 .3 9.5 5.18
319
+ # > 10 1 9.2 7.5 7.20
317
320
# > # ... with 100 more rows
318
- # > # i Use `print(n = ...)` to see more rows
319
321
```
320
322
321
323
### Training and prediction with Magma
@@ -328,89 +330,21 @@ model_dim2 <- train_magma(data = dim2_train)
328
330
# >
329
331
# > The 'ini_hp_i' argument has not been specified. Random values of hyper-parameters for the individal processes are used as initialisation.
330
332
# >
331
- # > EM algorithm, step 1: 14.17 seconds
332
- # >
333
- # > Value of the likelihood: -547.76188 --- Convergence ratio = Inf
334
- # >
335
- # > EM algorithm, step 2: 9.5 seconds
336
- # >
337
- # > Value of the likelihood: -494.5156 --- Convergence ratio = 0.10767
338
- # >
339
- # > EM algorithm, step 3: 11.98 seconds
340
- # >
341
- # > Value of the likelihood: -466.4102 --- Convergence ratio = 0.06026
342
- # >
343
- # > EM algorithm, step 4: 9.24 seconds
344
- # >
345
- # > Value of the likelihood: -449.17074 --- Convergence ratio = 0.03838
346
- # >
347
- # > EM algorithm, step 5: 5 seconds
348
- # >
349
- # > Value of the likelihood: -435.574 --- Convergence ratio = 0.03122
350
- # >
351
- # > EM algorithm, step 6: 5.4 seconds
352
- # >
353
- # > Value of the likelihood: -426.56255 --- Convergence ratio = 0.02113
354
- # >
355
- # > EM algorithm, step 7: 5.17 seconds
356
- # >
357
- # > Value of the likelihood: -421.25636 --- Convergence ratio = 0.0126
358
- # >
359
- # > EM algorithm, step 8: 5.15 seconds
360
- # >
361
- # > Value of the likelihood: -417.61643 --- Convergence ratio = 0.00872
362
- # >
363
- # > EM algorithm, step 9: 5.11 seconds
364
- # >
365
- # > Value of the likelihood: -415.16894 --- Convergence ratio = 0.0059
366
- # >
367
- # > EM algorithm, step 10: 4.99 seconds
368
- # >
369
- # > Value of the likelihood: -413.38881 --- Convergence ratio = 0.00431
370
- # >
371
- # > EM algorithm, step 11: 4.8 seconds
372
- # >
373
- # > Value of the likelihood: -411.98102 --- Convergence ratio = 0.00342
374
- # >
375
- # > EM algorithm, step 12: 4.25 seconds
376
- # >
377
- # > Value of the likelihood: -410.94035 --- Convergence ratio = 0.00253
378
- # >
379
- # > EM algorithm, step 13: 4.25 seconds
380
- # >
381
- # > Value of the likelihood: -409.99426 --- Convergence ratio = 0.00231
382
- # >
383
- # > EM algorithm, step 14: 4.25 seconds
384
- # >
385
- # > Value of the likelihood: -409.21728 --- Convergence ratio = 0.0019
386
- # >
387
- # > EM algorithm, step 15: 4.35 seconds
388
- # >
389
- # > Value of the likelihood: -408.47281 --- Convergence ratio = 0.00182
390
- # >
391
- # > EM algorithm, step 16: 4.33 seconds
392
- # >
393
- # > Value of the likelihood: -407.67549 --- Convergence ratio = 0.00196
394
- # >
395
- # > EM algorithm, step 17: 4.42 seconds
396
- # >
397
- # > Value of the likelihood: -406.49068 --- Convergence ratio = 0.00291
398
- # >
399
- # > EM algorithm, step 18: 4.67 seconds
333
+ # > EM algorithm, step 1: 5.42 seconds
400
334
# >
401
- # > Value of the likelihood: -405.14487 --- Convergence ratio = 0.00332
335
+ # > Value of the likelihood: -247.66608 --- Convergence ratio = Inf
402
336
# >
403
- # > EM algorithm, step 19: 4.22 seconds
337
+ # > EM algorithm, step 2: 5.39 seconds
404
338
# >
405
- # > Value of the likelihood: -404.58873 --- Convergence ratio = 0.00137
339
+ # > Value of the likelihood: -217.29028 --- Convergence ratio = 0.13979
406
340
# >
407
- # > EM algorithm, step 20: 4.31 seconds
341
+ # > EM algorithm, step 3: 3.04 seconds
408
342
# >
409
- # > Value of the likelihood: -403.83303 --- Convergence ratio = 0.00187
343
+ # > Value of the likelihood: -216.11346 --- Convergence ratio = 0.00545
410
344
# >
411
- # > EM algorithm, step 21: 4.51 seconds
345
+ # > EM algorithm, step 4: 3.04 seconds
412
346
# >
413
- # > Value of the likelihood: -403.49586 --- Convergence ratio = 0.00084
347
+ # > Value of the likelihood: -216.02699 --- Convergence ratio = 4e-04
414
348
# >
415
349
# > The EM algorithm successfully converged, training is completed.
416
350
# >
@@ -425,10 +359,6 @@ pred_dim2 <- pred_magma(data = dim2_pred,
425
359
# >
426
360
# > Done!
427
361
# >
428
- # > Warning: Raster pixels are placed at uneven horizontal intervals and will be
429
- # > shifted. Consider using geom_tile() instead.
430
- # > Warning: Raster pixels are placed at uneven vertical intervals and will be
431
- # > shifted. Consider using geom_tile() instead.
432
362
```
433
363
434
364
<img src =" man/figures/README-train_and_predict_Magma_in_2-D-1.png " width =" 80% " style =" display : block ; margin : auto ;" />
0 commit comments