-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcha-mcmc.html
More file actions
2232 lines (2191 loc) · 291 KB
/
cha-mcmc.html
File metadata and controls
2232 lines (2191 loc) · 291 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 7 MCMC | NimbleUserManual.knit</title>
<meta name="description" content="This is the NIMBLE User Manual." />
<meta name="generator" content="bookdown 0.37 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 7 MCMC | NimbleUserManual.knit" />
<meta property="og:type" content="book" />
<meta property="og:image" content="/nimble-icon.png" />
<meta property="og:description" content="This is the NIMBLE User Manual." />
<meta name="github-repo" content="nimble-dev/nimble" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 7 MCMC | NimbleUserManual.knit" />
<meta name="twitter:description" content="This is the NIMBLE User Manual." />
<meta name="twitter:image" content="/nimble-icon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="cha-building-models.html"/>
<link rel="next" href="cha-algos-provided.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<style type="text/css">
/* Used with Pandoc 2.11+ new --citeproc when CSL is used */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<img src="./nimble-icon.png"
width=100>
<li><a href="./cha-welcome-nimble.html">NIMBLE User Manual, Version 1.4.2</a></li>
<li><a href="https://github.com/nimble-dev/nimble">NIMBLE Development Team</a></li>
<li><a href="https://R-nimble.org">https://R-nimble.org</a></li>
<li class="divider"></li>
<li class="part"><span><b>I Introduction</b></span></li>
<li class="chapter" data-level="1" data-path="cha-welcome-nimble.html"><a href="cha-welcome-nimble.html"><i class="fa fa-check"></i><b>1</b> Welcome to NIMBLE</a>
<ul>
<li class="chapter" data-level="1.1" data-path="cha-welcome-nimble.html"><a href="cha-welcome-nimble.html#sec:what-is-nimble"><i class="fa fa-check"></i><b>1.1</b> What does NIMBLE do?</a></li>
<li class="chapter" data-level="1.2" data-path="cha-welcome-nimble.html"><a href="cha-welcome-nimble.html#how-to-use-this-manual"><i class="fa fa-check"></i><b>1.2</b> How to use this manual</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html"><i class="fa fa-check"></i><b>2</b> Lightning introduction</a>
<ul>
<li class="chapter" data-level="2.1" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:brief-example"><i class="fa fa-check"></i><b>2.1</b> A brief example</a></li>
<li class="chapter" data-level="2.2" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:creating-model"><i class="fa fa-check"></i><b>2.2</b> Creating a model</a></li>
<li class="chapter" data-level="2.3" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:compiling-model"><i class="fa fa-check"></i><b>2.3</b> Compiling the model</a></li>
<li class="chapter" data-level="2.4" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:intro-runMCMC"><i class="fa fa-check"></i><b>2.4</b> One-line invocation of MCMC</a></li>
<li class="chapter" data-level="2.5" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:creating-mcmc"><i class="fa fa-check"></i><b>2.5</b> Creating, compiling and running a basic MCMC configuration</a></li>
<li class="chapter" data-level="2.6" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:customizing-mcmc"><i class="fa fa-check"></i><b>2.6</b> Customizing the MCMC</a></li>
<li class="chapter" data-level="2.7" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:running-mcem"><i class="fa fa-check"></i><b>2.7</b> Running MCEM</a></li>
<li class="chapter" data-level="2.8" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:creating-your-own"><i class="fa fa-check"></i><b>2.8</b> Creating your own functions</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html"><i class="fa fa-check"></i><b>3</b> More introduction</a>
<ul>
<li class="chapter" data-level="3.1" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html#nimble-adopts-and-extends-the-bugs-language-for-specifying-models"><i class="fa fa-check"></i><b>3.1</b> NIMBLE adopts and extends the BUGS language for specifying models</a></li>
<li class="chapter" data-level="3.2" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html#sec:nimble-lang-writ"><i class="fa fa-check"></i><b>3.2</b> nimbleFunctions for writing algorithms</a></li>
<li class="chapter" data-level="3.3" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html#sec:nimble-algor-libr"><i class="fa fa-check"></i><b>3.3</b> The NIMBLE algorithm library</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html"><i class="fa fa-check"></i><b>4</b> Installing NIMBLE</a>
<ul>
<li class="chapter" data-level="4.1" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#sec:requ-run-nimble"><i class="fa fa-check"></i><b>4.1</b> Requirements to run NIMBLE</a></li>
<li class="chapter" data-level="4.2" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#sec:compiler"><i class="fa fa-check"></i><b>4.2</b> Installing a C++ compiler for NIMBLE to use</a>
<ul>
<li class="chapter" data-level="4.2.1" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#macos"><i class="fa fa-check"></i><b>4.2.1</b> MacOS</a></li>
<li class="chapter" data-level="4.2.2" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#linux"><i class="fa fa-check"></i><b>4.2.2</b> Linux</a></li>
<li class="chapter" data-level="4.2.3" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#windows"><i class="fa fa-check"></i><b>4.2.3</b> Windows</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#installing-the-nimble-package"><i class="fa fa-check"></i><b>4.3</b> Installing the NIMBLE package</a></li>
<li class="chapter" data-level="4.4" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#troubleshooting-installation-problems"><i class="fa fa-check"></i><b>4.4</b> Troubleshooting installation problems</a></li>
<li class="chapter" data-level="4.5" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#customizing-your-installation"><i class="fa fa-check"></i><b>4.5</b> Customizing your installation</a>
<ul>
<li class="chapter" data-level="4.5.1" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#using-your-own-copy-of-eigen"><i class="fa fa-check"></i><b>4.5.1</b> Using your own copy of Eigen</a></li>
<li class="chapter" data-level="4.5.2" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#using-libnimble"><i class="fa fa-check"></i><b>4.5.2</b> Using libnimble</a></li>
<li class="chapter" data-level="4.5.3" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#sec:blas"><i class="fa fa-check"></i><b>4.5.3</b> BLAS and LAPACK</a></li>
<li class="chapter" data-level="4.5.4" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#customizing-compilation-of-the-nimble-generated-c"><i class="fa fa-check"></i><b>4.5.4</b> Customizing compilation of the NIMBLE-generated C++</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>II Models in NIMBLE</b></span></li>
<li class="chapter" data-level="5" data-path="cha-writing-models.html"><a href="cha-writing-models.html"><i class="fa fa-check"></i><b>5</b> Writing models in NIMBLE’s dialect of BUGS</a>
<ul>
<li class="chapter" data-level="5.1" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:supp-feat-bugs"><i class="fa fa-check"></i><b>5.1</b> Comparison to BUGS dialects supported by WinBUGS, OpenBUGS and JAGS</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="cha-writing-models.html"><a href="cha-writing-models.html#supported-features-of-bugs-and-jags"><i class="fa fa-check"></i><b>5.1.1</b> Supported features of BUGS and JAGS</a></li>
<li class="chapter" data-level="5.1.2" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:extensions-bugs"><i class="fa fa-check"></i><b>5.1.2</b> NIMBLE’s Extensions to BUGS and JAGS</a></li>
<li class="chapter" data-level="5.1.3" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:not-yet-supported"><i class="fa fa-check"></i><b>5.1.3</b> Not-supported features of BUGS and JAGS</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="cha-writing-models.html"><a href="cha-writing-models.html#writing-models"><i class="fa fa-check"></i><b>5.2</b> Writing models</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="cha-writing-models.html"><a href="cha-writing-models.html#declaring-stochastic-and-deterministic-nodes"><i class="fa fa-check"></i><b>5.2.1</b> Declaring stochastic and deterministic nodes</a></li>
<li class="chapter" data-level="5.2.2" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:more-kinds-bugs"><i class="fa fa-check"></i><b>5.2.2</b> More kinds of BUGS declarations</a></li>
<li class="chapter" data-level="5.2.3" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:vectorized-versus-scalar-declarations"><i class="fa fa-check"></i><b>5.2.3</b> Vectorized versus scalar declarations</a></li>
<li class="chapter" data-level="5.2.4" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:dists-and-functions"><i class="fa fa-check"></i><b>5.2.4</b> Available distributions</a></li>
<li class="chapter" data-level="5.2.5" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:BUGS-lang-fxns"><i class="fa fa-check"></i><b>5.2.5</b> Available BUGS language functions</a></li>
<li class="chapter" data-level="5.2.6" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:BUGS-link"><i class="fa fa-check"></i><b>5.2.6</b> Available link functions</a></li>
<li class="chapter" data-level="5.2.7" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:trunc"><i class="fa fa-check"></i><b>5.2.7</b> Truncation, censoring, and constraints</a></li>
<li class="chapter" data-level="5.2.8" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:macros"><i class="fa fa-check"></i><b>5.2.8</b> Model macros</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="cha-building-models.html"><a href="cha-building-models.html"><i class="fa fa-check"></i><b>6</b> Building and using models</a>
<ul>
<li class="chapter" data-level="6.1" data-path="cha-building-models.html"><a href="cha-building-models.html#creating-model-objects"><i class="fa fa-check"></i><b>6.1</b> Creating model objects</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="cha-building-models.html"><a href="cha-building-models.html#using-nimblemodel-to-create-a-model"><i class="fa fa-check"></i><b>6.1.1</b> Using <em>nimbleModel</em> to create a model</a></li>
<li class="chapter" data-level="6.1.2" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:readBUGSmodel"><i class="fa fa-check"></i><b>6.1.2</b> Creating a model from standard BUGS and JAGS input files</a></li>
<li class="chapter" data-level="6.1.3" data-path="cha-building-models.html"><a href="cha-building-models.html#sub:multiple-instances"><i class="fa fa-check"></i><b>6.1.3</b> Making multiple instances from the same model definition</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:nodes-and-variables"><i class="fa fa-check"></i><b>6.2</b> NIMBLE models are objects you can query and manipulate</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:what-are-nodes-and-variables"><i class="fa fa-check"></i><b>6.2.1</b> What are variables and nodes?</a></li>
<li class="chapter" data-level="6.2.2" data-path="cha-building-models.html"><a href="cha-building-models.html#determining-the-nodes-and-variables-in-a-model"><i class="fa fa-check"></i><b>6.2.2</b> Determining the nodes and variables in a model</a></li>
<li class="chapter" data-level="6.2.3" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:accessing-nodes"><i class="fa fa-check"></i><b>6.2.3</b> Accessing nodes</a></li>
<li class="chapter" data-level="6.2.4" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:how-nodes-are"><i class="fa fa-check"></i><b>6.2.4</b> How nodes are named</a></li>
<li class="chapter" data-level="6.2.5" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:why-use-node"><i class="fa fa-check"></i><b>6.2.5</b> Why use node names?</a></li>
<li class="chapter" data-level="6.2.6" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:cdisdata"><i class="fa fa-check"></i><b>6.2.6</b> Checking if a node holds data</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="cha-building-models.html"><a href="cha-building-models.html#using-models-in-parallel"><i class="fa fa-check"></i><b>6.3</b> Using models in parallel</a></li>
</ul></li>
<li class="part"><span><b>III Algorithms in NIMBLE</b></span></li>
<li class="chapter" data-level="7" data-path="cha-mcmc.html"><a href="cha-mcmc.html"><i class="fa fa-check"></i><b>7</b> MCMC</a>
<ul>
<li class="chapter" data-level="7.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:nimbleMCMC"><i class="fa fa-check"></i><b>7.1</b> One-line invocation of MCMC: <em>nimbleMCMC</em></a></li>
<li class="chapter" data-level="7.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:mcmc-configuration"><i class="fa fa-check"></i><b>7.2</b> The MCMC configuration</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:default-mcmc-conf"><i class="fa fa-check"></i><b>7.2.1</b> Default MCMC configuration</a></li>
<li class="chapter" data-level="7.2.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:customizing-mcmc-conf"><i class="fa fa-check"></i><b>7.2.2</b> Customizing the MCMC configuration</a></li>
<li class="chapter" data-level="7.2.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:derived-quantities"><i class="fa fa-check"></i><b>7.2.3</b> Setting up derived quantities for additional quantities of interest</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:build-compile-mcmc"><i class="fa fa-check"></i><b>7.3</b> Building and compiling the MCMC</a></li>
<li class="chapter" data-level="7.4" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:initMCMC"><i class="fa fa-check"></i><b>7.4</b> Initializing MCMC</a></li>
<li class="chapter" data-level="7.5" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:runMCMC"><i class="fa fa-check"></i><b>7.5</b> User-friendly execution of MCMC algorithms: <em>runMCMC</em></a></li>
<li class="chapter" data-level="7.6" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:executing-the-mcmc-algorithm"><i class="fa fa-check"></i><b>7.6</b> Running the MCMC</a>
<ul>
<li class="chapter" data-level="7.6.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:mcmc-rerun"><i class="fa fa-check"></i><b>7.6.1</b> Rerunning versus restarting an MCMC</a></li>
<li class="chapter" data-level="7.6.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:sampler-time"><i class="fa fa-check"></i><b>7.6.2</b> Measuring sampler computation times: <em>getTimes</em></a></li>
<li class="chapter" data-level="7.6.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#assessing-the-adaption-process-of-rw-and-rw_block-samplers"><i class="fa fa-check"></i><b>7.6.3</b> Assessing the adaption process of <em>RW</em> and <em>RW_block</em> samplers</a></li>
</ul></li>
<li class="chapter" data-level="7.7" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:extracting-samples"><i class="fa fa-check"></i><b>7.7</b> Extracting MCMC samples</a></li>
<li class="chapter" data-level="7.8" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:WAIC"><i class="fa fa-check"></i><b>7.8</b> Calculating WAIC</a></li>
<li class="chapter" data-level="7.9" data-path="cha-mcmc.html"><a href="cha-mcmc.html#k-fold-cross-validation"><i class="fa fa-check"></i><b>7.9</b> k-fold cross-validation</a></li>
<li class="chapter" data-level="7.10" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:rjmcmc"><i class="fa fa-check"></i><b>7.10</b> Variable selection using Reversible Jump MCMC</a>
<ul>
<li class="chapter" data-level="7.10.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:rjmcmc-indicator"><i class="fa fa-check"></i><b>7.10.1</b> Using indicator variables</a></li>
<li class="chapter" data-level="7.10.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:rjmcmc-no-indicator"><i class="fa fa-check"></i><b>7.10.2</b> Without indicator variables</a></li>
</ul></li>
<li class="chapter" data-level="7.11" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:samplers-provided"><i class="fa fa-check"></i><b>7.11</b> Samplers provided with NIMBLE</a>
<ul>
<li class="chapter" data-level="7.11.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#conjugate-gibbs-samplers"><i class="fa fa-check"></i><b>7.11.1</b> Conjugate (‘Gibbs’) samplers</a></li>
<li class="chapter" data-level="7.11.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#subsec:HMC"><i class="fa fa-check"></i><b>7.11.2</b> Hamiltonian Monte Carlo (HMC)</a></li>
<li class="chapter" data-level="7.11.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#particle-filter-samplers"><i class="fa fa-check"></i><b>7.11.3</b> Particle filter samplers</a></li>
<li class="chapter" data-level="7.11.4" data-path="cha-mcmc.html"><a href="cha-mcmc.html#customized-log-likelihood-evaluations-rw_llfunction-sampler"><i class="fa fa-check"></i><b>7.11.4</b> Customized log-likelihood evaluations: <em>RW_llFunction sampler</em></a></li>
</ul></li>
<li class="chapter" data-level="7.12" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:mcmc-example-litters"><i class="fa fa-check"></i><b>7.12</b> Detailed MCMC example: <em>litters</em></a></li>
<li class="chapter" data-level="7.13" data-path="cha-mcmc.html"><a href="cha-mcmc.html#mcmc-suite-compare-mcmcs"><i class="fa fa-check"></i><b>7.13</b> Comparing different MCMCs with <em>compareMCMCs</em></a></li>
<li class="chapter" data-level="7.14" data-path="cha-mcmc.html"><a href="cha-mcmc.html#running-mcmc-chains-in-parallel"><i class="fa fa-check"></i><b>7.14</b> Running MCMC chains in parallel</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html"><i class="fa fa-check"></i><b>8</b> Particle Filters, PMCMC, and MCEM</a>
<ul>
<li class="chapter" data-level="8.1" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#particle-filters-sequential-monte-carlo-and-iterated-filtering"><i class="fa fa-check"></i><b>8.1</b> Particle filters / sequential Monte Carlo and iterated filtering</a>
<ul>
<li class="chapter" data-level="8.1.1" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#filtering-algorithms"><i class="fa fa-check"></i><b>8.1.1</b> Filtering algorithms</a></li>
<li class="chapter" data-level="8.1.2" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#sec:particle-mcmc"><i class="fa fa-check"></i><b>8.1.2</b> Particle MCMC (PMCMC)</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#monte-carlo-expectation-maximization-mcem"><i class="fa fa-check"></i><b>8.2</b> Monte Carlo Expectation Maximization (MCEM)</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#sec:estimate-mcem-cov"><i class="fa fa-check"></i><b>8.2.1</b> Estimating the asymptotic covariance From MCEM</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="9" data-path="cha-laplace.html"><a href="cha-laplace.html"><i class="fa fa-check"></i><b>9</b> Laplace, AGHQ, and nested approximations</a>
<ul>
<li class="chapter" data-level="9.1" data-path="cha-laplace.html"><a href="cha-laplace.html#sec:AD-laplace"><i class="fa fa-check"></i><b>9.1</b> Laplace approximation and adaptive Gauss-Hermite quadrature (AGHQ)</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="cha-laplace.html"><a href="cha-laplace.html#glmm-example"><i class="fa fa-check"></i><b>9.1.1</b> GLMM example</a></li>
<li class="chapter" data-level="9.1.2" data-path="cha-laplace.html"><a href="cha-laplace.html#using-laplace-approximation"><i class="fa fa-check"></i><b>9.1.2</b> Using Laplace approximation</a></li>
<li class="chapter" data-level="9.1.3" data-path="cha-laplace.html"><a href="cha-laplace.html#using-the-laplace-approximation-methods-directly"><i class="fa fa-check"></i><b>9.1.3</b> Using the Laplace approximation methods directly</a></li>
<li class="chapter" data-level="9.1.4" data-path="cha-laplace.html"><a href="cha-laplace.html#changing-the-optimization-methods"><i class="fa fa-check"></i><b>9.1.4</b> Changing the optimization methods</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="cha-laplace.html"><a href="cha-laplace.html#sec:nested-approx"><i class="fa fa-check"></i><b>9.2</b> Nested approximation (INLA-like) methods</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="cha-laplace.html"><a href="cha-laplace.html#overview-of-the-methodology"><i class="fa fa-check"></i><b>9.2.1</b> Overview of the methodology</a></li>
<li class="chapter" data-level="9.2.2" data-path="cha-laplace.html"><a href="cha-laplace.html#using-nimbles-nested-approximation"><i class="fa fa-check"></i><b>9.2.2</b> Using NIMBLE’s nested approximation</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="cha-spatial.html"><a href="cha-spatial.html"><i class="fa fa-check"></i><b>10</b> Spatial models</a>
<ul>
<li class="chapter" data-level="10.1" data-path="cha-spatial.html"><a href="cha-spatial.html#intrinsic-gaussian-car-model-dcar_normal"><i class="fa fa-check"></i><b>10.1</b> Intrinsic Gaussian CAR model: <em>dcar_normal</em></a>
<ul>
<li class="chapter" data-level="10.1.1" data-path="cha-spatial.html"><a href="cha-spatial.html#specification-and-density"><i class="fa fa-check"></i><b>10.1.1</b> Specification and density</a></li>
<li class="chapter" data-level="10.1.2" data-path="cha-spatial.html"><a href="cha-spatial.html#example"><i class="fa fa-check"></i><b>10.1.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="10.2" data-path="cha-spatial.html"><a href="cha-spatial.html#proper-gaussian-car-model-dcar_proper"><i class="fa fa-check"></i><b>10.2</b> Proper Gaussian CAR model: <em>dcar_proper</em></a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="cha-spatial.html"><a href="cha-spatial.html#specification-and-density-1"><i class="fa fa-check"></i><b>10.2.1</b> Specification and density</a></li>
<li class="chapter" data-level="10.2.2" data-path="cha-spatial.html"><a href="cha-spatial.html#example-1"><i class="fa fa-check"></i><b>10.2.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="cha-spatial.html"><a href="cha-spatial.html#sec:spatial-mcmc-sampling-car"><i class="fa fa-check"></i><b>10.3</b> MCMC Sampling of CAR models</a>
<ul>
<li class="chapter" data-level="10.3.1" data-path="cha-spatial.html"><a href="cha-spatial.html#initial-values"><i class="fa fa-check"></i><b>10.3.1</b> Initial values</a></li>
<li class="chapter" data-level="10.3.2" data-path="cha-spatial.html"><a href="cha-spatial.html#zero-neighbor-regions"><i class="fa fa-check"></i><b>10.3.2</b> Zero-neighbor regions</a></li>
<li class="chapter" data-level="10.3.3" data-path="cha-spatial.html"><a href="cha-spatial.html#zero-mean-constraint"><i class="fa fa-check"></i><b>10.3.3</b> Zero-mean constraint</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="cha-bnp.html"><a href="cha-bnp.html"><i class="fa fa-check"></i><b>11</b> Bayesian nonparametric models</a>
<ul>
<li class="chapter" data-level="11.1" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:bnpmixtures"><i class="fa fa-check"></i><b>11.1</b> Bayesian nonparametric mixture models</a></li>
<li class="chapter" data-level="11.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:crp"><i class="fa fa-check"></i><b>11.2</b> Chinese Restaurant Process model</a>
<ul>
<li class="chapter" data-level="11.2.1" data-path="cha-bnp.html"><a href="cha-bnp.html#specification-and-density-2"><i class="fa fa-check"></i><b>11.2.1</b> Specification and density</a></li>
<li class="chapter" data-level="11.2.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:excrp"><i class="fa fa-check"></i><b>11.2.2</b> Example</a></li>
<li class="chapter" data-level="11.2.3" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:extensionscrp"><i class="fa fa-check"></i><b>11.2.3</b> Extensions</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:sb"><i class="fa fa-check"></i><b>11.3</b> Stick-breaking model</a>
<ul>
<li class="chapter" data-level="11.3.1" data-path="cha-bnp.html"><a href="cha-bnp.html#specification-and-function"><i class="fa fa-check"></i><b>11.3.1</b> Specification and function</a></li>
<li class="chapter" data-level="11.3.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:exsb"><i class="fa fa-check"></i><b>11.3.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="cha-bnp.html"><a href="cha-bnp.html#mcmc-sampling-of-bnp-models"><i class="fa fa-check"></i><b>11.4</b> MCMC sampling of BNP models</a>
<ul>
<li class="chapter" data-level="11.4.1" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:mcmcdcrp"><i class="fa fa-check"></i><b>11.4.1</b> Sampling CRP models</a></li>
<li class="chapter" data-level="11.4.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:mcmcsb"><i class="fa fa-check"></i><b>11.4.2</b> Sampling stick-breaking models</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>IV Programming with NIMBLE</b></span></li>
<li class="chapter" data-level="" data-path="overview.html"><a href="overview.html"><i class="fa fa-check"></i>Overview</a></li>
<li class="chapter" data-level="12" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html"><i class="fa fa-check"></i><b>12</b> Writing simple nimbleFunctions</a>
<ul>
<li class="chapter" data-level="12.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:RC-intro"><i class="fa fa-check"></i><b>12.1</b> Introduction to simple nimbleFunctions</a></li>
<li class="chapter" data-level="12.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:r-fiunctions-implemented"><i class="fa fa-check"></i><b>12.2</b> R functions (or variants) implemented in NIMBLE</a>
<ul>
<li class="chapter" data-level="12.2.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#finding-help-for-nimbles-versions-of-r-functions"><i class="fa fa-check"></i><b>12.2.1</b> Finding help for NIMBLE’s versions of R functions</a></li>
<li class="chapter" data-level="12.2.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#basic-operations"><i class="fa fa-check"></i><b>12.2.2</b> Basic operations</a></li>
<li class="chapter" data-level="12.2.3" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:basic-math-linear"><i class="fa fa-check"></i><b>12.2.3</b> Math and linear algebra</a></li>
<li class="chapter" data-level="12.2.4" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:nimble-dist-funs"><i class="fa fa-check"></i><b>12.2.4</b> Distribution functions</a></li>
<li class="chapter" data-level="12.2.5" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:basic-flow-control"><i class="fa fa-check"></i><b>12.2.5</b> Flow control: <em>if-then-else</em>, <em>for</em>, <em>while</em>, and <em>stop</em></a></li>
<li class="chapter" data-level="12.2.6" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:print"><i class="fa fa-check"></i><b>12.2.6</b> <em>print</em> and <em>cat</em></a></li>
<li class="chapter" data-level="12.2.7" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:check-user-interr"><i class="fa fa-check"></i><b>12.2.7</b> Checking for user interrupts: <em>checkInterrupt</em></a></li>
<li class="chapter" data-level="12.2.8" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#optimization-optim-and-nimoptim"><i class="fa fa-check"></i><b>12.2.8</b> Optimization: <em>optim</em> and <em>nimOptim</em></a></li>
<li class="chapter" data-level="12.2.9" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#integration-integrate-and-nimintegrate"><i class="fa fa-check"></i><b>12.2.9</b> Integration: <em>integrate</em> and <em>nimIntegrate</em></a></li>
<li class="chapter" data-level="12.2.10" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:altern-keyw-some"><i class="fa fa-check"></i><b>12.2.10</b> ‘nim’ synonyms for some functions</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:how-nimble-handles"><i class="fa fa-check"></i><b>12.3</b> How NIMBLE handles types of variables</a>
<ul>
<li class="chapter" data-level="12.3.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:nimbleList-RCFuns"><i class="fa fa-check"></i><b>12.3.1</b> nimbleList data structures</a></li>
<li class="chapter" data-level="12.3.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:how-types-work"><i class="fa fa-check"></i><b>12.3.2</b> How numeric types work</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:decl-argum-return"><i class="fa fa-check"></i><b>12.4</b> Declaring argument and return types</a></li>
<li class="chapter" data-level="12.5" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:comp-nimbl-pass"><i class="fa fa-check"></i><b>12.5</b> Compiled nimbleFunctions pass arguments by reference</a></li>
<li class="chapter" data-level="12.6" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:calling-external-code"><i class="fa fa-check"></i><b>12.6</b> Calling external compiled code</a></li>
<li class="chapter" data-level="12.7" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:calling-R-code"><i class="fa fa-check"></i><b>12.7</b> Calling uncompiled R functions from compiled nimbleFunctions</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="cha-user-defined.html"><a href="cha-user-defined.html"><i class="fa fa-check"></i><b>13</b> Creating user-defined distributions and functions for models</a>
<ul>
<li class="chapter" data-level="13.1" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-functions"><i class="fa fa-check"></i><b>13.1</b> User-defined functions</a></li>
<li class="chapter" data-level="13.2" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-distributions"><i class="fa fa-check"></i><b>13.2</b> User-defined distributions</a>
<ul>
<li class="chapter" data-level="13.2.1" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:registerDistributions"><i class="fa fa-check"></i><b>13.2.1</b> Using <em>registerDistributions</em> for alternative parameterizations and providing other information</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:adv-user-def"><i class="fa fa-check"></i><b>13.3</b> Advanced user-defined functions and distributions</a></li>
<li class="chapter" data-level="13.4" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-macros"><i class="fa fa-check"></i><b>13.4</b> User-defined model macros</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="cha-using-models.html"><a href="cha-using-models.html"><i class="fa fa-check"></i><b>14</b> Working with NIMBLE models</a>
<ul>
<li class="chapter" data-level="14.1" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:accessing-variables"><i class="fa fa-check"></i><b>14.1</b> The variables and nodes in a NIMBLE model</a>
<ul>
<li class="chapter" data-level="14.1.1" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:get-nodes"><i class="fa fa-check"></i><b>14.1.1</b> Determining the nodes in a model</a></li>
<li class="chapter" data-level="14.1.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:introduced-nodes"><i class="fa fa-check"></i><b>14.1.2</b> Understanding lifted nodes</a></li>
<li class="chapter" data-level="14.1.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdgetdependencies"><i class="fa fa-check"></i><b>14.1.3</b> Determining dependencies in a model</a></li>
</ul></li>
<li class="chapter" data-level="14.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:nodeInfo"><i class="fa fa-check"></i><b>14.2</b> Accessing information about nodes and variables</a>
<ul>
<li class="chapter" data-level="14.2.1" data-path="cha-using-models.html"><a href="cha-using-models.html#getting-distributional-information-about-a-node"><i class="fa fa-check"></i><b>14.2.1</b> Getting distributional information about a node</a></li>
<li class="chapter" data-level="14.2.2" data-path="cha-using-models.html"><a href="cha-using-models.html#getting-information-about-a-distribution"><i class="fa fa-check"></i><b>14.2.2</b> Getting information about a distribution</a></li>
<li class="chapter" data-level="14.2.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:getParam"><i class="fa fa-check"></i><b>14.2.3</b> Getting distribution parameter values for a node</a></li>
<li class="chapter" data-level="14.2.4" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:getBound"><i class="fa fa-check"></i><b>14.2.4</b> Getting distribution bounds for a node</a></li>
</ul></li>
<li class="chapter" data-level="14.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdcalc-cdsim-cdgetl"><i class="fa fa-check"></i><b>14.3</b> Carrying out model calculations</a>
<ul>
<li class="chapter" data-level="14.3.1" data-path="cha-using-models.html"><a href="cha-using-models.html#core-model-operations-calculation-and-simulation"><i class="fa fa-check"></i><b>14.3.1</b> Core model operations: calculation and simulation</a></li>
<li class="chapter" data-level="14.3.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdsimn-cdcalcn-cdget"><i class="fa fa-check"></i><b>14.3.2</b> Pre-defined nimbleFunctions for operating on model nodes: <em>simNodes</em>, <em>calcNodes</em>, and <em>getLogProbNodes</em></a></li>
<li class="chapter" data-level="14.3.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:access-log-prob"><i class="fa fa-check"></i><b>14.3.3</b> Accessing log probabilities via <em>logProb</em> variables</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="15" data-path="cha-data-structures.html"><a href="cha-data-structures.html"><i class="fa fa-check"></i><b>15</b> Data structures in NIMBLE</a>
<ul>
<li class="chapter" data-level="15.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:modelValues-struct"><i class="fa fa-check"></i><b>15.1</b> The modelValues data structure</a>
<ul>
<li class="chapter" data-level="15.1.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#creating-modelvalues-objects"><i class="fa fa-check"></i><b>15.1.1</b> Creating modelValues objects</a></li>
<li class="chapter" data-level="15.1.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:access-cont-modelv"><i class="fa fa-check"></i><b>15.1.2</b> Accessing contents of modelValues</a></li>
</ul></li>
<li class="chapter" data-level="15.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:nimbleLists"><i class="fa fa-check"></i><b>15.2</b> The nimbleList data structure</a>
<ul>
<li class="chapter" data-level="15.2.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:predef-nimbleLists"><i class="fa fa-check"></i><b>15.2.1</b> Pre-defined nimbleList types</a></li>
<li class="chapter" data-level="15.2.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:eigen-nimFunctions"><i class="fa fa-check"></i><b>15.2.2</b> Using <em>eigen</em> and <em>svd</em> in nimbleFunctions</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="16" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html"><i class="fa fa-check"></i><b>16</b> Writing nimbleFunctions to interact with models</a>
<ul>
<li class="chapter" data-level="16.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:writ-nimble-funct"><i class="fa fa-check"></i><b>16.1</b> Overview</a></li>
<li class="chapter" data-level="16.2" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:using-comp-nimbl"><i class="fa fa-check"></i><b>16.2</b> Using and compiling nimbleFunctions</a></li>
<li class="chapter" data-level="16.3" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#writing-setup-code"><i class="fa fa-check"></i><b>16.3</b> Writing setup code</a>
<ul>
<li class="chapter" data-level="16.3.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#useful-tools-for-setup-functions"><i class="fa fa-check"></i><b>16.3.1</b> Useful tools for setup functions</a></li>
<li class="chapter" data-level="16.3.2" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:access-modify-numer"><i class="fa fa-check"></i><b>16.3.2</b> Accessing and modifying numeric values from setup</a></li>
<li class="chapter" data-level="16.3.3" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#determining-numeric-types-in-nimblefunctions"><i class="fa fa-check"></i><b>16.3.3</b> Determining numeric types in nimbleFunctions</a></li>
<li class="chapter" data-level="16.3.4" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:determ-pers-texttts"><i class="fa fa-check"></i><b>16.3.4</b> Control of setup outputs</a></li>
</ul></li>
<li class="chapter" data-level="16.4" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:nimble-lang-comp"><i class="fa fa-check"></i><b>16.4</b> Writing run code</a>
<ul>
<li class="chapter" data-level="16.4.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:driv-models:-calc"><i class="fa fa-check"></i><b>16.4.1</b> Driving models: <em>calculate</em>, <em>calculateDiff</em>, <em>simulate</em>, <em>getLogProb</em></a></li>
<li class="chapter" data-level="16.4.2" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#getting-and-setting-variable-and-node-values"><i class="fa fa-check"></i><b>16.4.2</b> Getting and setting variable and node values</a></li>
<li class="chapter" data-level="16.4.3" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#getting-parameter-values-and-node-bounds"><i class="fa fa-check"></i><b>16.4.3</b> Getting parameter values and node bounds</a></li>
<li class="chapter" data-level="16.4.4" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:access-model-modelv"><i class="fa fa-check"></i><b>16.4.4</b> Using modelValues objects</a></li>
<li class="chapter" data-level="16.4.5" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:using-model-variable"><i class="fa fa-check"></i><b>16.4.5</b> Using model variables and modelValues in expressions</a></li>
<li class="chapter" data-level="16.4.6" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:incl-other-meth"><i class="fa fa-check"></i><b>16.4.6</b> Including other methods in a nimbleFunction</a></li>
<li class="chapter" data-level="16.4.7" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:using-other-nimbl"><i class="fa fa-check"></i><b>16.4.7</b> Using other nimbleFunctions</a></li>
<li class="chapter" data-level="16.4.8" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:virt-nimbl-nimbl"><i class="fa fa-check"></i><b>16.4.8</b> Virtual nimbleFunctions and nimbleFunctionLists</a></li>
<li class="chapter" data-level="16.4.9" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#character-objects"><i class="fa fa-check"></i><b>16.4.9</b> Character objects</a></li>
<li class="chapter" data-level="16.4.10" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:user-defined-data"><i class="fa fa-check"></i><b>16.4.10</b> User-defined data structures</a></li>
</ul></li>
<li class="chapter" data-level="16.5" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:user-samplers"><i class="fa fa-check"></i><b>16.5</b> Example: writing user-defined samplers to extend NIMBLE’s MCMC engine</a>
<ul>
<li class="chapter" data-level="16.5.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#user-defined-samplers-and-posterior-predictive-nodes"><i class="fa fa-check"></i><b>16.5.1</b> User-defined samplers and posterior predictive nodes</a></li>
</ul></li>
<li class="chapter" data-level="16.6" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#copying-nimblefunctions-and-nimble-models"><i class="fa fa-check"></i><b>16.6</b> Copying nimbleFunctions (and NIMBLE models)</a></li>
<li class="chapter" data-level="16.7" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:debugging"><i class="fa fa-check"></i><b>16.7</b> Debugging nimbleFunctions</a></li>
<li class="chapter" data-level="16.8" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#timing-nimblefunctions-with-run.time"><i class="fa fa-check"></i><b>16.8</b> Timing nimbleFunctions with <em>run.time</em></a></li>
<li class="chapter" data-level="16.9" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#clearing-and-unloading-compiled-objects"><i class="fa fa-check"></i><b>16.9</b> Clearing and unloading compiled objects</a></li>
<li class="chapter" data-level="16.10" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#reducing-memory-usage"><i class="fa fa-check"></i><b>16.10</b> Reducing memory usage</a></li>
</ul></li>
<li class="part"><span><b>V Automatic Derivatives in NIMBLE</b></span></li>
<li class="chapter" data-level="17" data-path="cha-AD.html"><a href="cha-AD.html"><i class="fa fa-check"></i><b>17</b> Automatic Derivatives</a>
<ul>
<li class="chapter" data-level="17.1" data-path="cha-AD.html"><a href="cha-AD.html#sec:use-derivs"><i class="fa fa-check"></i><b>17.1</b> How to turn on derivatives in a model</a></li>
<li class="chapter" data-level="17.2" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-user-def"><i class="fa fa-check"></i><b>17.2</b> How to support derivatives in user-defined functions and distributions</a></li>
<li class="chapter" data-level="17.3" data-path="cha-AD.html"><a href="cha-AD.html#what-operations-are-and-arent-supported-for-ad"><i class="fa fa-check"></i><b>17.3</b> What operations are and aren’t supported for AD</a></li>
<li class="chapter" data-level="17.4" data-path="cha-AD.html"><a href="cha-AD.html#basics-of-obtaining-derivatives-in-nimblefunctions"><i class="fa fa-check"></i><b>17.4</b> Basics of obtaining derivatives in <code>nimbleFunctions</code></a>
<ul>
<li class="chapter" data-level="17.4.1" data-path="cha-AD.html"><a href="cha-AD.html#checking-derivatives-with-uncompiled-execution"><i class="fa fa-check"></i><b>17.4.1</b> Checking derivatives with uncompiled execution</a></li>
<li class="chapter" data-level="17.4.2" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-holding-out"><i class="fa fa-check"></i><b>17.4.2</b> Holding some local variables out of derivative tracking</a></li>
<li class="chapter" data-level="17.4.3" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-multiple-NF"><i class="fa fa-check"></i><b>17.4.3</b> Using AD with multiple nimbleFunctions</a></li>
<li class="chapter" data-level="17.4.4" data-path="cha-AD.html"><a href="cha-AD.html#sec:understanding-more-AD"><i class="fa fa-check"></i><b>17.4.4</b> Understanding more about how AD works: <em>taping</em> of operations</a></li>
<li class="chapter" data-level="17.4.5" data-path="cha-AD.html"><a href="cha-AD.html#resetting-a-nimderivs-call"><i class="fa fa-check"></i><b>17.4.5</b> Resetting a <code>nimDerivs</code> call</a></li>
<li class="chapter" data-level="17.4.6" data-path="cha-AD.html"><a href="cha-AD.html#a-note-on-performance-benchmarking"><i class="fa fa-check"></i><b>17.4.6</b> A note on performance benchmarking</a></li>
</ul></li>
<li class="chapter" data-level="17.5" data-path="cha-AD.html"><a href="cha-AD.html#advanced-uses-double-taping"><i class="fa fa-check"></i><b>17.5</b> Advanced uses: double taping</a></li>
<li class="chapter" data-level="17.6" data-path="cha-AD.html"><a href="cha-AD.html#derivatives-involving-model-calculations"><i class="fa fa-check"></i><b>17.6</b> Derivatives involving model calculations</a>
<ul>
<li class="chapter" data-level="17.6.1" data-path="cha-AD.html"><a href="cha-AD.html#method-1-nimderivs-of-modelcalculate"><i class="fa fa-check"></i><b>17.6.1</b> Method 1: <code>nimDerivs</code> of <code>model$calculate</code></a></li>
<li class="chapter" data-level="17.6.2" data-path="cha-AD.html"><a href="cha-AD.html#method-2-nimderivs-of-a-method-that-calls-modelcalculate"><i class="fa fa-check"></i><b>17.6.2</b> Method 2: <code>nimDerivs</code> of a method that calls <code>model$calculate</code></a></li>
</ul></li>
<li class="chapter" data-level="17.7" data-path="cha-AD.html"><a href="cha-AD.html#sec:parameter-transform"><i class="fa fa-check"></i><b>17.7</b> Parameter transformations</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="example-maximum-likelihood-estimation-using-optim-with-gradients-from-nimderivs..html"><a href="example-maximum-likelihood-estimation-using-optim-with-gradients-from-nimderivs..html"><i class="fa fa-check"></i><b>18</b> Example: maximum likelihood estimation using <code>optim</code> with gradients from <code>nimDerivs</code>.</a></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./"></a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="cha-mcmc" class="section level1 hasAnchor" number="7">
<h1><span class="header-section-number">Chapter 7</span> MCMC<a href="cha-mcmc.html#cha-mcmc" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>NIMBLE provides a variety of paths to creating and executing an MCMC algorithm, which differ greatly in their simplicity of use, and also in the options available and customizability.</p>
<p>The most direct approach to invoking the MCMC engine is using the <code>nimbleMCMC</code> function (Section <a href="cha-mcmc.html#sec:nimbleMCMC">7.1</a>). This one-line call creates and executes an MCMC, and provides a wide range of options for controlling the MCMC: specifying monitors, burn-in, and thinning, running multiple MCMC chains with different initial values, and returning posterior samples, summary statistics, and/or a WAIC value. However, this approach is restricted to using NIMBLE’s default MCMC algorithm; further customization of, for example, the specific samplers employed, is not possible.</p>
<p>The lengthier and more customizable approach to invoking the MCMC engine on a particular NIMBLE model object involves the following steps:</p>
<ol style="list-style-type: decimal">
<li><p>(Optional) Create and customize an MCMC configuration for a particular model:</p>
<ol style="list-style-type: lower-alpha">
<li><p>Use <code>configureMCMC</code> to create an MCMC configuration (see Section <a href="cha-mcmc.html#sec:mcmc-configuration">7.2</a>). The configuration contains a list of samplers with the node(s) they will sample.</p></li>
<li><p>(Optional) Customize the MCMC configuration:</p>
<ol style="list-style-type: lower-roman">
<li>Add, remove, or re-order the list of samplers (Section <a href="cha-mcmc.html#sec:samplers-provided">7.11</a> and <code>help(samplers)</code> in R for details), including adding your own samplers (Section <a href="cha-progr-with-models.html#sec:user-samplers">16.5</a>);</li>
<li>Change the tuning parameters or adaptive properties of individual samplers;</li>
<li>Change the variables to monitor (record for output) and thinning intervals for MCMC samples.</li>
<li>Choose additional “derived quantities” of interest to monitor.</li>
</ol></li>
</ol></li>
<li><p>Use <code>buildMCMC</code> to build the MCMC object and its samplers either from the model (using default MCMC configuration) or from a customized MCMC configuration (Section <a href="cha-mcmc.html#sec:build-compile-mcmc">7.3</a>).</p></li>
<li><p>Compile the MCMC object (and the model), unless one is debugging and wishes to run the uncompiled MCMC.</p></li>
<li><p>Run the MCMC and extract the samples and any derived quantities (Sections <a href="cha-mcmc.html#sec:runMCMC">7.5</a>, <a href="cha-mcmc.html#sec:executing-the-mcmc-algorithm">7.6</a> and <a href="cha-mcmc.html#sec:extracting-samples">7.7</a>).</p></li>
<li><p>Optionally, calculate the WAIC (Section <a href="cha-mcmc.html#sec:WAIC">7.8</a>).</p></li>
</ol>
<p>End-to-end examples of MCMC in NIMBLE can be found in Sections <a href="cha-lightning-intro.html#sec:creating-mcmc">2.5</a>-<a href="cha-lightning-intro.html#sec:customizing-mcmc">2.6</a> and Section <a href="cha-mcmc.html#sec:mcmc-example-litters">7.12</a>.
<!--- ### Creating an MCMC algorithm --></p>
<!--- A default MCMC algorithm can be created using `buildMCMC(model)`. See Section \@ref(sec:build-compile-mcmc). -->
<!--- To customize properties of the MCMC algorithm -- including the sampling algorithms, joint (block) sampling of dimensions, tuning and adaptive properties of sampling algorithms, variables being monitored (posterior samples are collected), and the thinning interval for sample collection -- an MCMC configuration object (`conf`) must be created using `configureMCMC(model)`. Properties of the MCMC algorithm can be modified using the `conf` object, after which an MCMC algorithm (`mcmc`) can be created using `buildMCMC(conf)`. See Section \@ref(mcmc-configuration). -->
<!--- Both the `model` and `mcmc` should generally be compiled using `compileNimble`, for significantly faster execution (Section \@ref(sec:build-compile-mcmc)). We'll refer to the compiled MCMC algorithm as `Cmcmc`. -->
<!--- ### Executing an MCMC algorithm and collecting samples -->
<!--- There are two methods available to execute an MCMC algorithm: -->
<!---
% 1. `Cmcmc$run(...)`
% 1. `runMCMC(Cmcmc, ...)` -->
<!--- Using `Cmcmc$run(...)` provides lower-level options including extending an MCMC run (collecting additional samples from where it last stopped), and also collecting timing information for the internal sampling algorithms. After executing an MCMC in this manner, posterior samples are extracted from within the `Cmcmc` object using `as.matrix(Cmcmc$mvSamples)`. See Sections \@ref(sec:executing-the-mcmc-algorithm) and \@ref(sec:extracting-samples). -->
<!--- Using `runMCMC(Cmcmc, ...)` provides higher-level options such as running multiple chains, setting initial values, removing burn-in, and returning posterior samples in the form of a `coda` `mcmc` object. Running an MCMC in this manner returns an array of samples, a list of sample arrays in the case of multiple chains, or optionally a `coda` object. See Section \@ref(sec:runMCMC). -->
<!--- ### Other topics -->
<!--- In addition to details of the steps outlined above, this chapter also includes: -->
<!---
% %% 1. NIMBLE's algorithm to search blocks of nodes for efficient joint (block) sampling (section \@ref(sec:default-mcmc-conf)) -->
<!--- 1. Information about the sampling algorithms provided with NIMBLE (Section \@ref(sec:samplers-provided))
% 1. A detailed example of using the MCMC engine (section \@ref(sec:mcmc-example-litters)) -->
<!--- 1. Methods to automatically run WinBUGS, OpenBUGS, JAGS, Stan and/or multiple NIMBLE MCMCs on the same model (section \@ref(mcmc-suite-compare-mcmcs)) -->
<div id="sec:nimbleMCMC" class="section level2 hasAnchor" number="7.1">
<h2><span class="header-section-number">7.1</span> One-line invocation of MCMC: <em>nimbleMCMC</em><a href="cha-mcmc.html#sec:nimbleMCMC" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The most direct approach to executing an MCMC algorithm in NIMBLE is using <code>nimbleMCMC</code>. This single function can be used to create an underlying model and associated MCMC algorithm, compile both of these, execute the MCMC, and return samples, summary statistics, and a WAIC value. This approach circumvents the longer (and more flexible) approach using <code>nimbleModel</code>, <code>configureMCMC</code>, <code>buildMCMC</code>, <code>compileNimble</code>, and <code>runMCMC</code>, which is described subsequently.</p>
<p>The <code>nimbleMCMC</code> function provides control over the:</p>
<ul>
<li>number of MCMC iterations in each chain;</li>
<li>number of MCMC chains to execute;</li>
<li>number of burn-in samples to discard from each chain;</li>
<li>thinning interval on which samples should be recorded;</li>
<li>model variables to monitor and return posterior samples;</li>
<li>initial values, or a function for generating initial values for each chain;</li>
<li>setting the random number seed;</li>
<li>returning posterior samples as a matrix or a <code>coda</code> <code>mcmc</code> object;</li>
<li>returning posterior summary statistics (calculated over thinned samples);</li>
<li>returning means and variances (calculated over unthinned samples) of selected nodes;</li>
<li>return model log-probabilities for selected nodes; and</li>
<li>returning a WAIC value calculated using post-burn-in samples from all chains.</li>
</ul>
<p>This entry point for using <code>nimbleMCMC</code> is the <code>code</code>, <code>constants</code>, <code>data</code>, and <code>inits</code> arguments that are used for building a NIMBLE model (see Chapters <a href="cha-writing-models.html#cha-writing-models">5</a> and <a href="cha-building-models.html#cha-building-models">6</a>). However, when using <code>nimbleMCMC</code>, the <code>inits</code> argument can also specify a list of lists of initial values that will be used for each MCMC chain, or a function that generates a list of initial values, which will be generated at the onset of each chain. As an alternative entry point, a NIMBLE <code>model</code> object can also be supplied to <code>nimbleMCMC</code>, in which case this model will be used to build the MCMC algorithm.</p>
<p>Based on its arguments, <code>nimbleMCMC</code> optionally returns any combination of</p>
<ul>
<li>posterior samples,</li>
<li>posterior summary statistics,</li>
<li>posterior means and variances,</li>
<li>posterior log-probabilities, and</li>
<li>WAIC value.</li>
</ul>
<p>The above are calculated and returned for each MCMC chain, all using the post-burnin samples. The samples and log-probabilities are thinned and the summary statistics reported (but not the WAIC calculation) use the thinned samples. Additionally, posterior summary statistics are calculated for all chains combined when multiple chains are run.</p>
<p>Several example usages of <code>nimbleMCMC</code> are shown below:</p>
<div class="sourceCode" id="cb151"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb151-1"><a href="cha-mcmc.html#cb151-1" tabindex="-1"></a>code <span class="ot"><-</span> <span class="fu">nimbleCode</span>({</span>
<span id="cb151-2"><a href="cha-mcmc.html#cb151-2" tabindex="-1"></a> mu <span class="sc">~</span> <span class="fu">dnorm</span>(<span class="dv">0</span>, <span class="at">sd =</span> <span class="dv">1000</span>)</span>
<span id="cb151-3"><a href="cha-mcmc.html#cb151-3" tabindex="-1"></a> sigma <span class="sc">~</span> <span class="fu">dunif</span>(<span class="dv">0</span>, <span class="dv">1000</span>)</span>
<span id="cb151-4"><a href="cha-mcmc.html#cb151-4" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>)</span>
<span id="cb151-5"><a href="cha-mcmc.html#cb151-5" tabindex="-1"></a> x[i] <span class="sc">~</span> <span class="fu">dnorm</span>(mu, <span class="at">sd =</span> sigma)</span>
<span id="cb151-6"><a href="cha-mcmc.html#cb151-6" tabindex="-1"></a>})</span>
<span id="cb151-7"><a href="cha-mcmc.html#cb151-7" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">5</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">3</span>))</span>
<span id="cb151-8"><a href="cha-mcmc.html#cb151-8" tabindex="-1"></a>initsFunction <span class="ot"><-</span> <span class="cf">function</span>() <span class="fu">list</span>(<span class="at">mu =</span> <span class="fu">rnorm</span>(<span class="dv">1</span>,<span class="dv">0</span>,<span class="dv">1</span>), <span class="at">sigma =</span> <span class="fu">runif</span>(<span class="dv">1</span>,<span class="dv">0</span>,<span class="dv">10</span>))</span>
<span id="cb151-9"><a href="cha-mcmc.html#cb151-9" tabindex="-1"></a></span>
<span id="cb151-10"><a href="cha-mcmc.html#cb151-10" tabindex="-1"></a><span class="co"># Execute one MCMC chain, monitoring the `mu` and `sigma` variables and</span></span>
<span id="cb151-11"><a href="cha-mcmc.html#cb151-11" tabindex="-1"></a><span class="co"># the overall model log-probability, with thinning interval 10.</span></span>
<span id="cb151-12"><a href="cha-mcmc.html#cb151-12" tabindex="-1"></a><span class="co"># Fix the random number seed for reproducible</span></span>
<span id="cb151-13"><a href="cha-mcmc.html#cb151-13" tabindex="-1"></a><span class="co"># results. By default, only posterior samples are returned.</span></span>
<span id="cb151-14"><a href="cha-mcmc.html#cb151-14" tabindex="-1"></a>mcmc_out <span class="ot"><-</span> <span class="fu">nimbleMCMC</span>(<span class="at">code =</span> code, <span class="at">data =</span> data, <span class="at">inits =</span> initsFunction,</span>
<span id="cb151-15"><a href="cha-mcmc.html#cb151-15" tabindex="-1"></a> <span class="at">monitors =</span> <span class="fu">c</span>(<span class="st">"mu"</span>, <span class="st">"sigma"</span>), <span class="at">logProb =</span> <span class="cn">TRUE</span>, <span class="at">thin =</span> <span class="dv">10</span>,</span>
<span id="cb151-16"><a href="cha-mcmc.html#cb151-16" tabindex="-1"></a> <span class="at">niter =</span> <span class="dv">20000</span>, <span class="at">nchains =</span> <span class="dv">1</span>, <span class="at">setSeed =</span> <span class="cn">TRUE</span>)</span>
<span id="cb151-17"><a href="cha-mcmc.html#cb151-17" tabindex="-1"></a></span>
<span id="cb151-18"><a href="cha-mcmc.html#cb151-18" tabindex="-1"></a><span class="co"># Note that the `inits` argument to `nimbleModel` must be a list of</span></span>
<span id="cb151-19"><a href="cha-mcmc.html#cb151-19" tabindex="-1"></a><span class="co"># initial values, whereas `nimbleMCMC` can accept `inits` as a function</span></span>
<span id="cb151-20"><a href="cha-mcmc.html#cb151-20" tabindex="-1"></a><span class="co"># for generating new initial values for each chain.</span></span>
<span id="cb151-21"><a href="cha-mcmc.html#cb151-21" tabindex="-1"></a>initsList <span class="ot"><-</span> <span class="fu">initsFunction</span>()</span>
<span id="cb151-22"><a href="cha-mcmc.html#cb151-22" tabindex="-1"></a>Rmodel <span class="ot"><-</span> <span class="fu">nimbleModel</span>(code, <span class="at">data =</span> data, <span class="at">inits =</span> initsList)</span>
<span id="cb151-23"><a href="cha-mcmc.html#cb151-23" tabindex="-1"></a></span>
<span id="cb151-24"><a href="cha-mcmc.html#cb151-24" tabindex="-1"></a><span class="co"># Using the existing Rmodel object, execute three MCMC chains with </span></span>
<span id="cb151-25"><a href="cha-mcmc.html#cb151-25" tabindex="-1"></a><span class="co"># specified burn-in. Return samples, overall log-probability,</span></span>
<span id="cb151-26"><a href="cha-mcmc.html#cb151-26" tabindex="-1"></a><span class="co"># summary statistics, and WAIC.</span></span>
<span id="cb151-27"><a href="cha-mcmc.html#cb151-27" tabindex="-1"></a>mcmc_out <span class="ot"><-</span> <span class="fu">nimbleMCMC</span>(<span class="at">model =</span> Rmodel,</span>
<span id="cb151-28"><a href="cha-mcmc.html#cb151-28" tabindex="-1"></a> <span class="at">niter =</span> <span class="dv">20000</span>, <span class="at">nchains =</span> <span class="dv">3</span>, <span class="at">nburnin =</span> <span class="dv">2000</span>,</span>
<span id="cb151-29"><a href="cha-mcmc.html#cb151-29" tabindex="-1"></a> <span class="at">logProb =</span> <span class="cn">TRUE</span>, <span class="at">summary =</span> <span class="cn">TRUE</span>, <span class="at">WAIC =</span> <span class="cn">TRUE</span>)</span>
<span id="cb151-30"><a href="cha-mcmc.html#cb151-30" tabindex="-1"></a></span>
<span id="cb151-31"><a href="cha-mcmc.html#cb151-31" tabindex="-1"></a><span class="co"># Run ten chains, generating random initial values for each</span></span>
<span id="cb151-32"><a href="cha-mcmc.html#cb151-32" tabindex="-1"></a><span class="co"># chain using the inits function specified above.</span></span>
<span id="cb151-33"><a href="cha-mcmc.html#cb151-33" tabindex="-1"></a><span class="co"># Only return summary statistics from each chain; not all the samples.</span></span>
<span id="cb151-34"><a href="cha-mcmc.html#cb151-34" tabindex="-1"></a>mcmc_out <span class="ot"><-</span> <span class="fu">nimbleMCMC</span>(<span class="at">model =</span> Rmodel, <span class="at">nchains =</span> <span class="dv">10</span>, <span class="at">inits =</span> initsFunction,</span>
<span id="cb151-35"><a href="cha-mcmc.html#cb151-35" tabindex="-1"></a> <span class="at">samples =</span> <span class="cn">FALSE</span>, <span class="at">summary =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p>See <code>help(nimbleMCMC)</code> for further details.</p>
</div>
<div id="sec:mcmc-configuration" class="section level2 hasAnchor" number="7.2">
<h2><span class="header-section-number">7.2</span> The MCMC configuration<a href="cha-mcmc.html#sec:mcmc-configuration" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The MCMC configuration contains information needed for building an MCMC. When no customization is needed, one can jump directly to the <code>buildMCMC</code> step below. An MCMC configuration is an object of class <code>MCMCconf</code>, which includes:</p>
<ul>
<li>The model on which the MCMC will operate.</li>
<li>The model nodes which will be sampled (updated) by the MCMC.</li>
<li>The samplers and their internal configurations, called control parameters.</li>
<li>Two sets of variables that will be monitored (recorded) during execution of the MCMC and thinning intervals for how often each set will be recorded. Two sets are allowed because it can be useful to monitor different variables at different intervals.</li>
<li>Optionally any derived quantities (e.g., model log-probabilities) to return as additional quantities of interest.</li>
</ul>
<div id="sec:default-mcmc-conf" class="section level3 hasAnchor" number="7.2.1">
<h3><span class="header-section-number">7.2.1</span> Default MCMC configuration<a href="cha-mcmc.html#sec:default-mcmc-conf" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Assuming we have a model named <code>Rmodel</code>, the following will generate a default MCMC configuration:</p>
<div class="sourceCode" id="cb152"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb152-1"><a href="cha-mcmc.html#cb152-1" tabindex="-1"></a>mcmcConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(Rmodel)</span></code></pre></div>
<p>The default configuration will contain a single sampler for each node in the model, and the default ordering follows the topological ordering of the model.</p>
<div id="default-assignment-of-sampler-algorithms" class="section level4 hasAnchor" number="7.2.1.1">
<h4><span class="header-section-number">7.2.1.1</span> Default assignment of sampler algorithms<a href="cha-mcmc.html#default-assignment-of-sampler-algorithms" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The default sampler assigned to a stochastic node is determined by the following, in order of precedence:</p>
<ol style="list-style-type: decimal">
<li>If the node has no data nodes in its entire downstream dependency network, a <code>posterior_predictive</code> sampler is assigned. This sampler updates the node in question and all downstream stochastic nodes by simulating new values from each node’s conditional distribution. As of version 0.13.0, the operation of the <code>posterior_predictive</code> sampler changed in order to improve MCMC mixing. See Section <a href="cha-mcmc.html#sec:post-pred-sampling">7.2.1.2</a>.</li>
<li>If the node has a conjugate relationship between its prior distribution and the distributions of its stochastic dependents, a <code>conjugate</code> (`Gibbs’) sampler is assigned.</li>
<li>If the node follows a multinomial distribution, then a <code>RW_multinomial</code> sampler is assigned. This is a discrete random-walk sampler in the space of multinomial outcomes.</li>
<li>If a node follows a Dirichlet distribution, then a <code>RW_dirichlet</code> sampler is assigned. This is a random walk sampler in the space of the simplex defined by the Dirichlet.</li>
<li>If a node follows an LKJ correlation distribution, then a <code>RW_block_lkj_corr_cholesky</code> sampler is assigned. This is a block random walk sampler in a transformed space where the transformation uses the signed stickbreaking approach described in Section 10.12 of <span class="citation">Stan Development Team (<a href="references.html#ref-Stan_Lang_2021">2021b</a>)</span>.</li>
<li>If the node follows any other multivariate distribution, then a <code>RW_block</code> sampler is assigned for all elements. This is a Metropolis-Hastings adaptive random-walk sampler with a multivariate normal proposal <span class="citation">(<a href="references.html#ref-Roberts_Sahu_1997">Roberts and Sahu 1997</a>)</span>. As of NIMBLE version 1.3.0, one can instead use the <code>barker</code> sampler in this case if one sets <code>nimbleOptions(MCMCuseBarkerAsDefaultMV = TRUE)</code>.</li>
<li>If the node is binary-valued (strictly taking values 0 or 1), then a <code>binary</code> sampler is assigned. This sampler calculates the conditional probability for both possible node values and draws the new node value from the conditional distribution, in effect making a Gibbs sampler.</li>
<li>If the node is otherwise discrete-valued, then a <code>slice</code> sampler is assigned <span class="citation">(<a href="references.html#ref-Neal2003">R. M. Neal 2003</a>)</span>.</li>
<li>If none of the above criteria are satisfied, then a <code>RW</code> sampler is assigned. This is a Metropolis-Hastings adaptive random-walk sampler with a univariate normal proposal distribution.</li>
</ol>
<!-- These sampler assignment rules can be inspected, reordered, and easily modified using the system option `nimbleOptions("MCMCdefaultSamplerAssignmentRules")` and customized `samplerAssignmentRules` objects. -->
<p>Details of each sampler and its control parameters can be found by invoking <code>help(samplers)</code>.</p>
<!-- #### Sampler assignment rules
The behavior of `configureMCMC` can be customized to control how samplers are assigned. A new set of sampler assignment rules can be created using `samplerAssignmentRules`, which can be modified using the `addRule` and `reorder` methods, then passed as an argument to `configureMCMC`. Alternatively, the default behavior of `configureMCMC` can be altered by setting the system option `MCMCdefaultSamplerAssignmentRules` to a custom `samplerAssignmentRules` object. See `help(samplerAssignmentRules)` for details. -->
</div>
<div id="sec:post-pred-sampling" class="section level4 hasAnchor" number="7.2.1.2">
<h4><span class="header-section-number">7.2.1.2</span> Sampling posterior predictive nodes<a href="cha-mcmc.html#sec:post-pred-sampling" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>A posterior predictive node is a node that is not itself data and has no data nodes in its entire downstream (descendant) dependency network. Such nodes play no role in inference for model parameters but have often been included in BUGS models to accomplish posterior predictive checks and similar calculations.</p>
<p>As of version 0.13.0, NIMBLE’s handling of posterior predictive nodes in MCMC sampling has changed in order to improve MCMC mixing. Samplers for nodes that are not posterior predictive nodes no longer condition on the values of the posterior predictive nodes. This produces a valid MCMC over the posterior distribution marginalizing over the posterior predictive nodes. This MCMC will generally mix better than an MCMC that conditions on the values of posterior predictive nodes, by reducing the dimensionality of the parameter space and removing the dependence between the sampled nodes and the posterior predictive nodes. At the end of each MCMC iteration, the posterior predictive nodes are sampled by <code>posterior_predictive</code> sampler(s) based on their conditional distribution(s).</p>
<p>Note that predictive node sampling is done at each iteration. When thinning is used, this results in unneeded computation (the sampling computation for the iterations that excluded from the output). To avoid this, one can set <code>samplePredictiveNodes = FALSE</code> as an argument to <code>configureMCMC</code> and then add the <code>predictive</code> derived quantity to the MCMC configuration object via <code>addDerivedQuantity</code> (Section <a href="cha-mcmc.html#sec:derived-quantities">7.2.3</a>). When doing so, samples for any predictive nodes will no longer be included with the MCMC samples array, but rather will appear as part of the <code>derived</code> MCMC output.</p>
</div>
<div id="options-to-control-default-sampler-assignments" class="section level4 hasAnchor" number="7.2.1.3">
<h4><span class="header-section-number">7.2.1.3</span> Options to control default sampler assignments<a href="cha-mcmc.html#options-to-control-default-sampler-assignments" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>Very basic control of default sampler assignments is provided via two arguments to <code>configureMCMC</code>. The <code>useConjugacy</code> argument controls whether conjugate samplers are assigned when possible, and the <code>multivariateNodesAsScalars</code> argument controls whether scalar elements of multivariate nodes are sampled individually. See <code>help(configureMCMC)</code> for usage details.
<!--- % The following optional control arguments to `configureMCMC()` may be used to override the default assignment of sampler algorithms: --></p>
<!---
%% 1.[useConjugacy (default `TRUE`)] If `TRUE`, conjugate samplers will be assigned to nodes determined to be in conjugate relationships. If `FALSE`, no conjugate samplers will be assigned.
% 1.[multivariateNodesAsScalars (default `FALSE`)] If `TRUE`, then independent scalar random walk Metropolis-Hastings samplers (`RW`) will be assigned to all scalar components comprising multivariate nodes. This contrasts the default behavior of a single block sampler being assigned to multivariate nodes. Regardless of the value of this argument, conjugate samplers will be assigned to conjugate (scalar and multivariate nodes), provided `useConjugacy = TRUE`. -->
</div>
<div id="default-monitors" class="section level4 hasAnchor" number="7.2.1.4">
<h4><span class="header-section-number">7.2.1.4</span> Default monitors<a href="cha-mcmc.html#default-monitors" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The default MCMC configuration includes monitors on all top-level stochastic nodes of the model. Only variables that are monitored will have their samples saved for use outside of the MCMC. MCMC configurations include two sets of monitors, each with different thinning intervals. By default, the second set of monitors (<code>monitors2</code>) is empty.</p>
</div>
<div id="automated-parameter-blocking" class="section level4 hasAnchor" number="7.2.1.5">
<h4><span class="header-section-number">7.2.1.5</span> Automated parameter blocking<a href="cha-mcmc.html#automated-parameter-blocking" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p></p>
<p>The default configuration may be replaced by one generated from an automated parameter blocking algorithm. This algorithm determines groupings of model nodes that, when jointly sampled with a <code>RW_block</code> sampler, increase overall MCMC efficiency. Overall efficiency is defined as the effective sample size of the slowest-mixing node divided by computation time. This is done by:</p>
<div class="sourceCode" id="cb153"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb153-1"><a href="cha-mcmc.html#cb153-1" tabindex="-1"></a>autoBlockConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(Rmodel, <span class="at">autoBlock =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p>Note that this using <code>autoBlock = TRUE</code> compiles and runs MCMCs, progressively exploring different sampler assignments, so it takes some time and generates some output. It is most useful for determining effective blocking strategies that can be re-used for later runs. The additional control argument <code>autoIt</code> may also be provided to indicate the number of MCMC samples to be used in each trial of the automated blocking procedure (default 20,000).</p>
</div>
</div>
<div id="sec:customizing-mcmc-conf" class="section level3 hasAnchor" number="7.2.2">
<h3><span class="header-section-number">7.2.2</span> Customizing the MCMC configuration<a href="cha-mcmc.html#sec:customizing-mcmc-conf" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The MCMC configuration may be customized in a variety of ways, either through additional named arguments to <code>configureMCMC</code> or by calling methods of an existing <code>MCMCconf</code> object.</p>
<div id="controlling-which-nodes-to-sample" class="section level4 hasAnchor" number="7.2.2.1">
<h4><span class="header-section-number">7.2.2.1</span> Controlling which nodes to sample<a href="cha-mcmc.html#controlling-which-nodes-to-sample" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>One can create an MCMC configuration with default samplers on just a particular set of nodes using the <code>nodes</code> argument to <code>configureMCMC</code>. The value for the <code>nodes</code> argument may be a character vector containing node and/or variable names. In the case of a variable name, a default sampler will be added for all stochastic nodes in the variable. The order of samplers will match the order of <code>nodes</code>. Any deterministic nodes will be ignored.</p>
<p>If a data node is included in <code>nodes</code>, <em>it will be assigned a sampler</em>. This is the only way in which a default sampler may be placed on a data node and will result in overwriting data values in the node.</p>
</div>
<div id="creating-an-empty-configuration" class="section level4 hasAnchor" number="7.2.2.2">
<h4><span class="header-section-number">7.2.2.2</span> Creating an empty configuration<a href="cha-mcmc.html#creating-an-empty-configuration" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>If you plan to customize the choice of all samplers, it can be useful to obtain a configuration with no sampler assignments at all. This can be done by any of <code>nodes = NULL</code>, <code>nodes = character()</code>, or <code>nodes = list()</code>.</p>
<!-- #### Overriding the default sampler assignment rules
The default rules used for assigning samplers to model nodes can be overridden using the `rules` argument to `configureMCMC`. This argument must be an object of class `samplerAssignmentRules`, which defines an ordered set of rules for assigning samplers. Rules can be modified and reordered, to give different precedence to particular samplers, or to assign user-defined samplers (see section \@ref(sec:user-samplers)). The following example creates a new set of rules (which initially contains the default assignment rules), reorders the rules, adds a new rule, then uses these rules to create an MCMC configuration object.
r, mcmcconfrules, eval=FALSE}
my_rules <- samplerAssignmentRules()
my_rules$reorder(c(8, 1:7))
my_rules$addRule(condition = quote(model$getDistribution(node) == "dmnorm"),
sampler = new_dmnorm_sampler)
mcmcConf <- configureMCMC(Rmodel, rules = my_rules)
In addition, the default behavior of `configureMCMC` can be altered by setting the system option `nimbleOptions(MCMCdefaultSamplerAssignmentRules = my_rules)`, or reset to the original default behavior using `nimbleOptions(MCMCdefaultSamplerAssignmentRules = samplerAssignmentRules())`. -->
</div>
<div id="overriding-the-default-sampler-control-list-values" class="section level4 hasAnchor" number="7.2.2.3">
<h4><span class="header-section-number">7.2.2.3</span> Overriding the default sampler control list values<a href="cha-mcmc.html#overriding-the-default-sampler-control-list-values" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The default values of control list elements for all sampling algorithms may be overridden through use of the <code>control</code> argument to <code>configureMCMC</code>, which should be a named list.
Named elements in the <code>control</code> argument will be used for all default samplers and any subsequent sampler added via <code>addSampler</code> (see below). For example, the following will create the default MCMC configuration, except all <code>RW</code> samplers will have their initial <code>scale</code> set to 3, and none of the samplers (<code>RW</code>, or otherwise) will be adaptive.</p>
<div class="sourceCode" id="cb154"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb154-1"><a href="cha-mcmc.html#cb154-1" tabindex="-1"></a>mcmcConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(Rmodel, <span class="at">control =</span> <span class="fu">list</span>(<span class="at">scale =</span> <span class="dv">3</span>, <span class="at">adaptive =</span> <span class="cn">FALSE</span>))</span></code></pre></div>
<p>When adding samplers to a configuration using <code>addSampler</code>, the default control list can also be over-ridden.</p>
</div>
<div id="adding-samplers-to-the-configuration-addsampler" class="section level4 hasAnchor" number="7.2.2.4">
<h4><span class="header-section-number">7.2.2.4</span> Adding samplers to the configuration: <em>addSampler</em><a href="cha-mcmc.html#adding-samplers-to-the-configuration-addsampler" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>Additional samplers may be added to a configuration using the <code>addSampler</code> method of the <code>MCMCconf</code> object. The <code>addSampler</code> method has two modes of operation, determined by the <code>default</code> argument.</p>
<p>When <code>default = TRUE</code>, <code>addSampler</code> will assign NIMBLE’s default sampling algorithm for each node specified following the same protocol as <code>configureMCMC</code>. This may include conjugate samplers, multivariate samplers, or otherwise, also using additional arguments to guide the selection process (for example, <code>useConjugacy</code> and <code>multivariateNodesAsScalars</code>). In this mode of operation, the <code>type</code> argument is not used.</p>
<p>When <code>default = FALSE</code>, or when this argument is omitted, <code>addSampler</code> uses the <code>type</code> argument to specify the precise sampler to assign. Instances of this particular sampler are assigned to all nodes specified. The <code>type</code> argument may be provided as a character string or a nimbleFunction object. Valid character strings are indicated in <code>help(samplers)</code> (do not include <code>"sampler_"</code>). Added samplers can be labeled with a <code>name</code> argument, which is used in output of <code>printSamplers</code>. Writing a new sampler as a nimbleFunction is covered in Section <a href="cha-progr-with-models.html#sec:user-samplers">16.5</a>.</p>
<p>Regardless of the mode of operation, nodes are specified using either the <code>target</code> or the <code>nodes</code> argument. The <code>target</code> argument does not undergo expansion to constituent nodes (unless <code>default = TRUE</code>), and thus only a single sampler is added. The <code>nodes</code> argument is always expanded to the underlying nodes, and separate samplers are added for each node. Eithe argument is provided as a character vector. Newly added samplers will be appended to the end of current sampler list. Adding a sampler for a node will <em>not</em> remove existing samplers operating on that node.</p>
<p>The hierarchy of precedence for control list elements for added samplers is:</p>
<ol style="list-style-type: decimal">
<li>The <code>control</code> list argument provided to <code>addSampler</code>;</li>
<li>The original <code>control</code> list argument provided to <code>configureMCMC</code>;</li>
<li>The default values, as defined in the sampling algorithm <code>setup</code> function.</li>
</ol>
<p>See <code>help(addSampler)</code> for more details.</p>
</div>
<div id="printing-re-ordering-modifying-and-removing-samplers-printsamplers-removesamplers-setsamplers-and-getsamplerdefinition" class="section level4 hasAnchor" number="7.2.2.5">
<h4><span class="header-section-number">7.2.2.5</span> Printing, re-ordering, modifying and removing samplers: <em>printSamplers</em>, <em>removeSamplers</em>, <em>setSamplers</em>, and <em>getSamplerDefinition</em><a href="cha-mcmc.html#printing-re-ordering-modifying-and-removing-samplers-printsamplers-removesamplers-setsamplers-and-getsamplerdefinition" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The current, ordered, list of all samplers in the MCMC configuration may be printed by calling the <code>printSamplers</code> method. When you want to see only samplers acting on specific model nodes or variables, provide those names as an argument to <code>printSamplers</code>. The <code>printSamplers</code> method accepts arguments controlling the level of detail displayed as discussed in its R help information.</p>
<div class="sourceCode" id="cb155"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb155-1"><a href="cha-mcmc.html#cb155-1" tabindex="-1"></a><span class="co"># Print all samplers</span></span>
<span id="cb155-2"><a href="cha-mcmc.html#cb155-2" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">printSamplers</span>()</span>
<span id="cb155-3"><a href="cha-mcmc.html#cb155-3" tabindex="-1"></a></span>
<span id="cb155-4"><a href="cha-mcmc.html#cb155-4" tabindex="-1"></a><span class="co"># Print all samplers operating on node "a[1]",</span></span>
<span id="cb155-5"><a href="cha-mcmc.html#cb155-5" tabindex="-1"></a><span class="co"># or any of the "beta[]" variables</span></span>
<span id="cb155-6"><a href="cha-mcmc.html#cb155-6" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">printSamplers</span>(<span class="fu">c</span>(<span class="st">"a[1]"</span>, <span class="st">"beta"</span>))</span>
<span id="cb155-7"><a href="cha-mcmc.html#cb155-7" tabindex="-1"></a></span>
<span id="cb155-8"><a href="cha-mcmc.html#cb155-8" tabindex="-1"></a><span class="co"># Print all conjugate and slice samplers</span></span>
<span id="cb155-9"><a href="cha-mcmc.html#cb155-9" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">printSamplers</span>(<span class="at">type =</span> <span class="fu">c</span>(<span class="st">"conjugate"</span>, <span class="st">"slice"</span>))</span>
<span id="cb155-10"><a href="cha-mcmc.html#cb155-10" tabindex="-1"></a></span>
<span id="cb155-11"><a href="cha-mcmc.html#cb155-11" tabindex="-1"></a><span class="co"># Print all RW samplers operating on "x"</span></span>
<span id="cb155-12"><a href="cha-mcmc.html#cb155-12" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">printSamplers</span>(<span class="st">"x"</span>, <span class="at">type =</span> <span class="st">"RW"</span>)</span>
<span id="cb155-13"><a href="cha-mcmc.html#cb155-13" tabindex="-1"></a></span>
<span id="cb155-14"><a href="cha-mcmc.html#cb155-14" tabindex="-1"></a><span class="co"># Print the first 100 samplers</span></span>
<span id="cb155-15"><a href="cha-mcmc.html#cb155-15" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">printSamplers</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">100</span>)</span>
<span id="cb155-16"><a href="cha-mcmc.html#cb155-16" tabindex="-1"></a></span>
<span id="cb155-17"><a href="cha-mcmc.html#cb155-17" tabindex="-1"></a><span class="co"># Print all samplers in their order of execution</span></span>
<span id="cb155-18"><a href="cha-mcmc.html#cb155-18" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">printSamplers</span>(<span class="at">executionOrder =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p>Samplers may be removed from the configuration object using <code>removeSamplers</code>, which accepts a character vector of node or variable names, or a numeric vector of indices.</p>
<div class="sourceCode" id="cb156"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb156-1"><a href="cha-mcmc.html#cb156-1" tabindex="-1"></a><span class="co"># Remove all samplers acting on "x" or any component of it</span></span>
<span id="cb156-2"><a href="cha-mcmc.html#cb156-2" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">removeSamplers</span>(<span class="st">"x"</span>)</span>
<span id="cb156-3"><a href="cha-mcmc.html#cb156-3" tabindex="-1"></a></span>
<span id="cb156-4"><a href="cha-mcmc.html#cb156-4" tabindex="-1"></a><span class="co"># Remove all samplers acting on "alpha[1]" and "beta[1]"</span></span>
<span id="cb156-5"><a href="cha-mcmc.html#cb156-5" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">removeSamplers</span>(<span class="fu">c</span>(<span class="st">"alpha[1]"</span>, <span class="st">"beta[1]"</span>))</span>
<span id="cb156-6"><a href="cha-mcmc.html#cb156-6" tabindex="-1"></a></span>
<span id="cb156-7"><a href="cha-mcmc.html#cb156-7" tabindex="-1"></a><span class="co"># Remove the first five samplers</span></span>
<span id="cb156-8"><a href="cha-mcmc.html#cb156-8" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">removeSamplers</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>)</span>
<span id="cb156-9"><a href="cha-mcmc.html#cb156-9" tabindex="-1"></a></span>
<span id="cb156-10"><a href="cha-mcmc.html#cb156-10" tabindex="-1"></a><span class="co"># Providing no argument removes all samplers</span></span>
<span id="cb156-11"><a href="cha-mcmc.html#cb156-11" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">removeSamplers</span>()</span></code></pre></div>
<p>Samplers to retain may be specified reordered using <code>setSamplers</code>, which also accepts a character vector of node or variable names, or a numeric vector of indices.</p>
<div class="sourceCode" id="cb157"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb157-1"><a href="cha-mcmc.html#cb157-1" tabindex="-1"></a><span class="co"># Set the list of samplers to those acting on any components of the</span></span>
<span id="cb157-2"><a href="cha-mcmc.html#cb157-2" tabindex="-1"></a><span class="co"># model variables "x", "y", or "z".</span></span>
<span id="cb157-3"><a href="cha-mcmc.html#cb157-3" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplers</span>(<span class="fu">c</span>(<span class="st">"x"</span>, <span class="st">"y"</span>, <span class="st">"z"</span>))</span>
<span id="cb157-4"><a href="cha-mcmc.html#cb157-4" tabindex="-1"></a></span>
<span id="cb157-5"><a href="cha-mcmc.html#cb157-5" tabindex="-1"></a><span class="co"># Set the list of samplers to only those acting on model nodes</span></span>
<span id="cb157-6"><a href="cha-mcmc.html#cb157-6" tabindex="-1"></a><span class="co"># "alpha[1]", "alpha[2]", ..., "alpha[10]"</span></span>
<span id="cb157-7"><a href="cha-mcmc.html#cb157-7" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplers</span>(<span class="st">"alpha[1:10]"</span>)</span>
<span id="cb157-8"><a href="cha-mcmc.html#cb157-8" tabindex="-1"></a></span>
<span id="cb157-9"><a href="cha-mcmc.html#cb157-9" tabindex="-1"></a><span class="co"># Truncate the current list of samplers to the first 10 and the 100th</span></span>
<span id="cb157-10"><a href="cha-mcmc.html#cb157-10" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplers</span>(<span class="at">ind =</span> <span class="fu">c</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>, <span class="dv">100</span>))</span></code></pre></div>
<p>The nimbleFunction definition underlying a particular sampler may be viewed using the <code>getSamplerDefinition</code> method, using the sampler index as an argument. A node name argument may also be supplied, in which case the definition of the first sampler acting on that node is returned. In all cases, <code>getSamplerDefinition</code> only returns the definition of the <em>first</em> sampler specified either by index or node name.</p>
<div class="sourceCode" id="cb158"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb158-1"><a href="cha-mcmc.html#cb158-1" tabindex="-1"></a><span class="co"># Return the definition of the third sampler in the mcmcConf object</span></span>
<span id="cb158-2"><a href="cha-mcmc.html#cb158-2" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">getSamplerDefinition</span>(<span class="dv">3</span>)</span>
<span id="cb158-3"><a href="cha-mcmc.html#cb158-3" tabindex="-1"></a></span>
<span id="cb158-4"><a href="cha-mcmc.html#cb158-4" tabindex="-1"></a><span class="co"># Return the definition of the first sampler acting on node "x",</span></span>
<span id="cb158-5"><a href="cha-mcmc.html#cb158-5" tabindex="-1"></a><span class="co"># or the first of any indexed nodes comprising the variable "x"</span></span>
<span id="cb158-6"><a href="cha-mcmc.html#cb158-6" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">getSamplerDefinition</span>(<span class="st">"x"</span>)</span></code></pre></div>
</div>
<div id="customizing-individual-sampler-configurations-getsamplers-setsamplers-setname-setsamplerfunction-settarget-and-setcontrol" class="section level4 hasAnchor" number="7.2.2.6">
<h4><span class="header-section-number">7.2.2.6</span> Customizing individual sampler configurations: <em>getSamplers</em>, <em>setSamplers</em>, <em>setName</em>, <em>setSamplerFunction</em>, <em>setTarget</em>, and <em>setControl</em><a href="cha-mcmc.html#customizing-individual-sampler-configurations-getsamplers-setsamplers-setname-setsamplerfunction-settarget-and-setcontrol" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>Each sampler in an <code>MCMCconf</code> object is represented by a sampler configuration as a <code>samplerConf</code> object. Each <code>samplerConf</code> is a reference class object containing the following (required) fields: <code>name</code> (a character string), <code>samplerFunction</code> (a valid nimbleFunction sampler), <code>target</code> (the model node to be sampled), and <code>control</code> (list of control arguments). The <code>MCMCconf</code> method <code>getSamplers</code> allows access to the <code>samplerConf</code> objects. These can be modified and then passed as an argument to <code>setSamplers</code> to over-write the current list of samplers in the MCMC configuration object. However, no checking of the validity of this modified list is performed; if the list of samplerConf objects is corrupted to be invalid, incorrect behavior will result at the time of calling <code>buildMCMC</code>. The fields of a <code>samplerConf</code> object can be modified using the access functions <code>setName(name)</code>, <code>setSamplerFunction(fun)</code>, <code>setTarget(target, model)</code>, and <code>setControl(control)</code>.</p>
<p>Here are some examples:</p>
<div class="sourceCode" id="cb159"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb159-1"><a href="cha-mcmc.html#cb159-1" tabindex="-1"></a><span class="co"># retrieve samplerConf list</span></span>
<span id="cb159-2"><a href="cha-mcmc.html#cb159-2" tabindex="-1"></a>samplerConfList <span class="ot"><-</span> mcmcConf<span class="sc">$</span><span class="fu">getSamplers</span>()</span>
<span id="cb159-3"><a href="cha-mcmc.html#cb159-3" tabindex="-1"></a></span>
<span id="cb159-4"><a href="cha-mcmc.html#cb159-4" tabindex="-1"></a><span class="co"># change the name of the first sampler</span></span>
<span id="cb159-5"><a href="cha-mcmc.html#cb159-5" tabindex="-1"></a>samplerConfList[[<span class="dv">1</span>]]<span class="sc">$</span><span class="fu">setName</span>(<span class="st">"newNameForThisSampler"</span>)</span>
<span id="cb159-6"><a href="cha-mcmc.html#cb159-6" tabindex="-1"></a></span>
<span id="cb159-7"><a href="cha-mcmc.html#cb159-7" tabindex="-1"></a><span class="co"># change the sampler function of the second sampler,</span></span>
<span id="cb159-8"><a href="cha-mcmc.html#cb159-8" tabindex="-1"></a><span class="co"># assuming existance of a nimbleFunction 'anotherSamplerNF',</span></span>
<span id="cb159-9"><a href="cha-mcmc.html#cb159-9" tabindex="-1"></a><span class="co"># which represents a valid nimbleFunction sampler.</span></span>
<span id="cb159-10"><a href="cha-mcmc.html#cb159-10" tabindex="-1"></a>samplerConfList[[<span class="dv">2</span>]]<span class="sc">$</span><span class="fu">setSamplerFunction</span>(anotherSamplerNF)</span>
<span id="cb159-11"><a href="cha-mcmc.html#cb159-11" tabindex="-1"></a></span>
<span id="cb159-12"><a href="cha-mcmc.html#cb159-12" tabindex="-1"></a><span class="co"># change the 'adaptive' element of the control list of the third sampler</span></span>
<span id="cb159-13"><a href="cha-mcmc.html#cb159-13" tabindex="-1"></a>control <span class="ot"><-</span> samplerConfList[[<span class="dv">3</span>]]<span class="sc">$</span>control</span>
<span id="cb159-14"><a href="cha-mcmc.html#cb159-14" tabindex="-1"></a>control<span class="sc">$</span>adaptive <span class="ot"><-</span> <span class="cn">FALSE</span></span>
<span id="cb159-15"><a href="cha-mcmc.html#cb159-15" tabindex="-1"></a>samplerConfList[[<span class="dv">3</span>]]<span class="sc">$</span><span class="fu">setControl</span>(control)</span>
<span id="cb159-16"><a href="cha-mcmc.html#cb159-16" tabindex="-1"></a></span>
<span id="cb159-17"><a href="cha-mcmc.html#cb159-17" tabindex="-1"></a><span class="co"># change the target node of the fourth sampler</span></span>
<span id="cb159-18"><a href="cha-mcmc.html#cb159-18" tabindex="-1"></a>samplerConfList[[<span class="dv">4</span>]]<span class="sc">$</span><span class="fu">setTarget</span>(<span class="st">"y"</span>, model) <span class="co"># model argument required</span></span>
<span id="cb159-19"><a href="cha-mcmc.html#cb159-19" tabindex="-1"></a></span>
<span id="cb159-20"><a href="cha-mcmc.html#cb159-20" tabindex="-1"></a><span class="co"># use this modified list of samplerConf objects in the MCMC configuration</span></span>
<span id="cb159-21"><a href="cha-mcmc.html#cb159-21" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplers</span>(samplerConfList)</span></code></pre></div>
</div>
<div id="customizing-the-sampler-execution-order" class="section level4 hasAnchor" number="7.2.2.7">
<h4><span class="header-section-number">7.2.2.7</span> Customizing the sampler execution order<a href="cha-mcmc.html#customizing-the-sampler-execution-order" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The ordering of sampler execution can be controlled as well. This allows for sampler functions to execute multiple times within a single MCMC iteration, or the execution of different sampler functions to be interleaved with one another.</p>
<p>The sampler execution order is set using the function <code>setSamplerExecutionOrder</code>, and the current ordering of execution is retrieved using <code>getSamplerExecutionOrder</code>. For example, assuming the MCMC configuration object <code>mcmcConf</code> contains five samplers:</p>
<div class="sourceCode" id="cb160"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb160-1"><a href="cha-mcmc.html#cb160-1" tabindex="-1"></a><span class="co"># first sampler to execute twice, in succession:</span></span>
<span id="cb160-2"><a href="cha-mcmc.html#cb160-2" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplerExecutionOrder</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>))</span>
<span id="cb160-3"><a href="cha-mcmc.html#cb160-3" tabindex="-1"></a></span>
<span id="cb160-4"><a href="cha-mcmc.html#cb160-4" tabindex="-1"></a><span class="co"># first sampler to execute multiple times, interleaved:</span></span>
<span id="cb160-5"><a href="cha-mcmc.html#cb160-5" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplerExecutionOrder</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">5</span>))</span>
<span id="cb160-6"><a href="cha-mcmc.html#cb160-6" tabindex="-1"></a></span>
<span id="cb160-7"><a href="cha-mcmc.html#cb160-7" tabindex="-1"></a><span class="co"># fourth sampler to execute 10 times, only</span></span>
<span id="cb160-8"><a href="cha-mcmc.html#cb160-8" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplerExecutionOrder</span>(<span class="fu">rep</span>(<span class="dv">4</span>, <span class="dv">10</span>))</span>
<span id="cb160-9"><a href="cha-mcmc.html#cb160-9" tabindex="-1"></a></span>
<span id="cb160-10"><a href="cha-mcmc.html#cb160-10" tabindex="-1"></a><span class="co"># omitting the argument to setSamplerExecutionOrder()</span></span>
<span id="cb160-11"><a href="cha-mcmc.html#cb160-11" tabindex="-1"></a><span class="co"># resets the ordering to each sampler executing once, sequentially</span></span>
<span id="cb160-12"><a href="cha-mcmc.html#cb160-12" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setSamplerExecutionOrder</span>()</span>
<span id="cb160-13"><a href="cha-mcmc.html#cb160-13" tabindex="-1"></a></span>
<span id="cb160-14"><a href="cha-mcmc.html#cb160-14" tabindex="-1"></a><span class="co"># retrieve the current ordering of sampler execution</span></span>
<span id="cb160-15"><a href="cha-mcmc.html#cb160-15" tabindex="-1"></a>ordering <span class="ot"><-</span> mcmcConf<span class="sc">$</span><span class="fu">getSamplerExecutionOrder</span>()</span>
<span id="cb160-16"><a href="cha-mcmc.html#cb160-16" tabindex="-1"></a></span>
<span id="cb160-17"><a href="cha-mcmc.html#cb160-17" tabindex="-1"></a><span class="co"># print the sampler functions in the order of execution</span></span>
<span id="cb160-18"><a href="cha-mcmc.html#cb160-18" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">printSamplers</span>(<span class="at">executionOrder =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
</div>
<div id="monitors-and-thinning-intervals-printmonitors-getmonitors-setmonitors-addmonitors-resetmonitors-and-setthin" class="section level4 hasAnchor" number="7.2.2.8">
<h4><span class="header-section-number">7.2.2.8</span> Monitors and thinning intervals: <em>printMonitors</em>, <em>getMonitors</em>, <em>setMonitors</em>, <em>addMonitors</em>, <em>resetMonitors</em> and <em>setThin</em><a href="cha-mcmc.html#monitors-and-thinning-intervals-printmonitors-getmonitors-setmonitors-addmonitors-resetmonitors-and-setthin" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>An MCMC configuration object contains two independent sets of variables to monitor, each with their own thinning interval: <code>thin</code> corresponding to <code>monitors</code>, and <code>thin2</code> corresponding to <code>monitors2</code>. Monitors operate at the <em>variable</em> level. Only entire model variables may be monitored. Specifying a monitor on a <em>node</em>, e.g., <code>x[1]</code>, will result in the entire variable <code>x</code> being monitored.</p>
<p>The variables specified in <code>monitors</code> and <code>monitors2</code> will be recorded (with thinning interval <code>thin</code>) into objects called <code>mvSamples</code> and <code>mvSamples2</code>, contained within the MCMC object. These are both <em>modelValues</em> objects; modelValues are NIMBLE data structures used to store multiple sets of values of model variables<a href="#fn18" class="footnote-ref" id="fnref18"><sup>18</sup></a>. These can be accessed as the member data <code>mvSamples</code> and <code>mvSamples2</code> of the MCMC object, and they can be converted to matrices using <code>as.matrix</code> or lists using <code>as.list</code> (see Section <a href="cha-mcmc.html#sec:extracting-samples">7.7</a>).</p>
<p>Monitors may be added to the MCMC configuration either in the original call to <code>configureMCMC</code> or using the <code>addMonitors</code> method:</p>
<div class="sourceCode" id="cb161"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb161-1"><a href="cha-mcmc.html#cb161-1" tabindex="-1"></a><span class="co"># Using an argument to configureMCMC</span></span>
<span id="cb161-2"><a href="cha-mcmc.html#cb161-2" tabindex="-1"></a>mcmcConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(Rmodel, <span class="at">monitors =</span> <span class="fu">c</span>(<span class="st">"alpha"</span>, <span class="st">"beta"</span>), </span>
<span id="cb161-3"><a href="cha-mcmc.html#cb161-3" tabindex="-1"></a> <span class="at">monitors2 =</span> <span class="st">"x"</span>)</span>
<span id="cb161-4"><a href="cha-mcmc.html#cb161-4" tabindex="-1"></a></span>
<span id="cb161-5"><a href="cha-mcmc.html#cb161-5" tabindex="-1"></a><span class="co"># Calling a member method of the mcmcconf object</span></span>
<span id="cb161-6"><a href="cha-mcmc.html#cb161-6" tabindex="-1"></a><span class="co"># This results in the same monitors as above</span></span>
<span id="cb161-7"><a href="cha-mcmc.html#cb161-7" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">addMonitors</span>(<span class="st">"alpha"</span>, <span class="st">"beta"</span>)</span>
<span id="cb161-8"><a href="cha-mcmc.html#cb161-8" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">addMonitors2</span>(<span class="st">"x"</span>)</span></code></pre></div>
<p>A new set of monitor variables can be added to the MCMC configuration, overwriting the current monitors, using the <code>setMonitors</code> method:</p>
<div class="sourceCode" id="cb162"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb162-1"><a href="cha-mcmc.html#cb162-1" tabindex="-1"></a><span class="co"># Replace old monitors, now monitor "delta" and "gamma" only</span></span>
<span id="cb162-2"><a href="cha-mcmc.html#cb162-2" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setMonitors</span>(<span class="st">"gamma"</span>, <span class="st">"delta"</span>)</span></code></pre></div>
<p>Similarly, either thinning interval may be set at either step:</p>
<div class="sourceCode" id="cb163"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb163-1"><a href="cha-mcmc.html#cb163-1" tabindex="-1"></a><span class="co"># Using an argument to configureMCMC</span></span>
<span id="cb163-2"><a href="cha-mcmc.html#cb163-2" tabindex="-1"></a>mcmcConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(Rmodel, <span class="at">thin =</span> <span class="dv">1</span>, <span class="at">thin2 =</span> <span class="dv">100</span>)</span>
<span id="cb163-3"><a href="cha-mcmc.html#cb163-3" tabindex="-1"></a></span>
<span id="cb163-4"><a href="cha-mcmc.html#cb163-4" tabindex="-1"></a><span class="co"># Calling a member method of the mcmcConf object</span></span>
<span id="cb163-5"><a href="cha-mcmc.html#cb163-5" tabindex="-1"></a><span class="co"># This results in the same thinning intervals as above</span></span>
<span id="cb163-6"><a href="cha-mcmc.html#cb163-6" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setThin</span>(<span class="dv">1</span>)</span>
<span id="cb163-7"><a href="cha-mcmc.html#cb163-7" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">setThin2</span>(<span class="dv">100</span>)</span></code></pre></div>
<p>The current lists of monitors and thinning intervals may be displayed using the <code>printMonitors</code> method. Both sets of monitors (<code>monitors</code> and <code>monitors2</code>) may be reset to empty character vectors by calling the <code>resetMonitors</code> method. The methods <code>getMonitors</code> and <code>getMonitors2</code> return the currently specified <code>monitors</code> and <code>monitors2</code> as character vectors.</p>
</div>
<div id="monitoring-model-log-probabilities" class="section level4 hasAnchor" number="7.2.2.9">
<h4><span class="header-section-number">7.2.2.9</span> Monitoring model log-probabilities<a href="cha-mcmc.html#monitoring-model-log-probabilities" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>One can record log-probabilities for all the individual nodes in a given variable as discussed next. Alternatively, one can monitor log-probabilities for individual nodes or summed log-probabilities for sets of nodes using NIMBLE’s derived quantities system (Section <a href="cha-mcmc.html#sec:derived-quantities">7.2.3</a>.</p>
<p>To record model log-probabilities from an MCMC, one can add monitors for <em>logProb</em> variables (which begin with the prefix <code>logProb_</code>) that correspond to variables with (any) stochastic nodes. For example, to record and extract log-probabilities for the variables <code>alpha</code>, <code>sigma_mu</code>, and <code>Y</code>:</p>
<div class="sourceCode" id="cb164"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb164-1"><a href="cha-mcmc.html#cb164-1" tabindex="-1"></a>mcmcConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(Rmodel, <span class="at">enableWAIC =</span> <span class="cn">TRUE</span>)</span>
<span id="cb164-2"><a href="cha-mcmc.html#cb164-2" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">addMonitors</span>(<span class="st">"logProb_alpha"</span>, <span class="st">"logProb_sigma_mu"</span>, <span class="st">"logProb_Y"</span>)</span>
<span id="cb164-3"><a href="cha-mcmc.html#cb164-3" tabindex="-1"></a>Rmcmc <span class="ot"><-</span> <span class="fu">buildMCMC</span>(mcmcConf)</span>
<span id="cb164-4"><a href="cha-mcmc.html#cb164-4" tabindex="-1"></a>Cmodel <span class="ot"><-</span> <span class="fu">compileNimble</span>(Rmodel)</span>
<span id="cb164-5"><a href="cha-mcmc.html#cb164-5" tabindex="-1"></a>Cmcmc <span class="ot"><-</span> <span class="fu">compileNimble</span>(Rmcmc, <span class="at">project =</span> Rmodel)</span>
<span id="cb164-6"><a href="cha-mcmc.html#cb164-6" tabindex="-1"></a>Cmcmc<span class="sc">$</span><span class="fu">run</span>(<span class="dv">10000</span>)</span>
<span id="cb164-7"><a href="cha-mcmc.html#cb164-7" tabindex="-1"></a>samples <span class="ot"><-</span> <span class="fu">as.matrix</span>(Cmcmc<span class="sc">$</span>mvSamples)</span></code></pre></div>
<p>The <code>samples</code> matrix will contain both MCMC samples and model log-probabilities.</p>
</div>
<div id="using-numeric-samples-to-define-a-prior-distribution" class="section level4 hasAnchor" number="7.2.2.10">
<h4><span class="header-section-number">7.2.2.10</span> Using numeric samples to define a prior distribution<a href="cha-mcmc.html#using-numeric-samples-to-define-a-prior-distribution" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>A set of numeric samples, perhaps generated from another MCMC algorithm, can be used to define the prior distribution of model nodes. This is accomplished using the <code>prior_samples</code> MCMC sampler. When assigning the <code>prior_samples</code> sampler to a <code>target</code> node, you must also provide a vector of numeric values (when <code>target</code> is a scalar node), or a matrix of values in the multidimenional case. A new value (or rows of values) is selected either sequentially (or randomly) from this numeric vector/matrix, and assigned into the <code>target</code> node on each MCMC iteration. See <code>help(prior_samples)</code> for more details:</p>
<div class="sourceCode" id="cb165"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb165-1"><a href="cha-mcmc.html#cb165-1" tabindex="-1"></a>mcmcConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(Rmodel)</span>
<span id="cb165-2"><a href="cha-mcmc.html#cb165-2" tabindex="-1"></a>mcmcConf<span class="sc">$</span><span class="fu">addSampler</span>(<span class="at">target =</span> <span class="st">'theta'</span>, <span class="at">type =</span> <span class="st">'prior_samples'</span>, <span class="at">samples =</span> <span class="fu">rnorm</span>(<span class="dv">100</span>))</span></code></pre></div>
</div>
</div>
<div id="sec:derived-quantities" class="section level3 hasAnchor" number="7.2.3">
<h3><span class="header-section-number">7.2.3</span> Setting up derived quantities for additional quantities of interest<a href="cha-mcmc.html#sec:derived-quantities" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>As of version 1.4.0, NIMBLE allows one to specify “derived quantities” to calculate or record additional quantities of interest. Potential derived quantities include:</p>
<ul>
<li>posterior means,</li>
<li>posterior variances,</li>
<li>log-probabilities (log-density values) for single or (summed) groups of nodes,</li>
<li>posterior predictive samples for predictive nodes, and</li>
<li>arbitrary calculations by providing a user-defined nimbleFunction that follows the required specification.</li>
</ul>
<p>User-defined derived quantity nimbleFunctions provide a general way for a user to intervene at the end of an MCMC iteration and do something. This would often be recording a value (to be returned at the end of the MCMC) or updating some summary statistic, but the derived quantities system doesn’t require this, giving users flexibility.</p>
<p>The operation of every derived quantity function is governed by an <code>interval</code> parameter. Each derived quantity function is executed at the end of every <code>interval</code> MCMC iterations. The default value for <code>interval</code> is given by the <code>thin</code> interval of the MCMC. This means that, by default, each derived quantity function will execute after all the MCMC samplers for the MCMC iterations for which samples are recorded. If <code>interval = 1</code> for a derived quantity function, then that derived quantity function will execute at the end of every MCMC sampling iteration.</p>
<p>Operation of derived quantity functions is entirely post-burnin. Derived quantity functions are not executed during the burnin period.</p>
<div id="specifying-derived-quantities" class="section level4 hasAnchor" number="7.2.3.1">
<h4><span class="header-section-number">7.2.3.1</span> Specifying derived quantities<a href="cha-mcmc.html#specifying-derived-quantities" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>Derived quantity functions can be added to an MCMC configuration object using the <code>addDerivedQuantity</code> method. Alternatively, the <code>mean</code>, <code>variance</code>, and <code>logProb</code> derived quantities are directly supported as arguments to <code>configureMCMC</code>, <code>buildMCMC</code>, and <code>nimbleMCMC</code>.</p>
<p>When using <code>configureMCMC</code>, <code>buildMCMC</code>, and <code>nimbleMCMC</code>, one cannot modify the default <code>interval</code>, which is set equal to the MCMC <code>thin</code> interval.</p>
<p>One can interact with derived quantities by using the following methods with an MCMC configuration object (analogous to similarly-named methods for interacting with the samplers in an MCMC configuration, such as <code>addSampler</code>).</p>
<ul>
<li><code>addDerivedQuantity</code></li>
<li><code>removeDerivedQuantity</code></li>
<li><code>removeDerivedQuantities</code></li>
<li><code>printDerivedQuantities</code></li>
<li><code>getDerivedQuantities</code></li>
<li><code>getDerivedQuantityDefinition</code></li>
</ul>
<p>Detailed information on their use can be found using the R help system, e.g., <code>help(removeDerivedQuantity)</code>.</p>
</div>
<div id="log-probability-derived-quantities" class="section level4 hasAnchor" number="7.2.3.2">
<h4><span class="header-section-number">7.2.3.2</span> Log-probability derived quantities<a href="cha-mcmc.html#log-probability-derived-quantities" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The <code>logProb</code> derived quantity function calculates and stores the log-probability values (log-density values) for:</p>
<ul>
<li>any single nodes,</li>
<li>any sets of nodes (summed), or</li>
<li>all stochastic model nodes (summed).</li>
</ul>
<p>When the <code>nodes</code> argument is a character vector of node names, the log-probability of each node is recorded individually. We’ll provide some examples using an old friend, the <code>pump</code> model.</p>
<div class="sourceCode" id="cb166"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb166-1"><a href="cha-mcmc.html#cb166-1" tabindex="-1"></a>pumpCode <span class="ot"><-</span> <span class="fu">nimbleCode</span>({ </span>
<span id="cb166-2"><a href="cha-mcmc.html#cb166-2" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>N) {</span>
<span id="cb166-3"><a href="cha-mcmc.html#cb166-3" tabindex="-1"></a> theta[i] <span class="sc">~</span> <span class="fu">dgamma</span>(alpha, beta)</span>
<span id="cb166-4"><a href="cha-mcmc.html#cb166-4" tabindex="-1"></a> lambda[i] <span class="ot"><-</span> theta[i]<span class="sc">*</span>t[i]</span>
<span id="cb166-5"><a href="cha-mcmc.html#cb166-5" tabindex="-1"></a> x[i] <span class="sc">~</span> <span class="fu">dpois</span>(lambda[i])</span>
<span id="cb166-6"><a href="cha-mcmc.html#cb166-6" tabindex="-1"></a> }</span>
<span id="cb166-7"><a href="cha-mcmc.html#cb166-7" tabindex="-1"></a> alpha <span class="sc">~</span> <span class="fu">dexp</span>(<span class="fl">1.0</span>)</span>
<span id="cb166-8"><a href="cha-mcmc.html#cb166-8" tabindex="-1"></a> beta <span class="sc">~</span> <span class="fu">dgamma</span>(<span class="fl">0.1</span>, <span class="fl">1.0</span>)</span>
<span id="cb166-9"><a href="cha-mcmc.html#cb166-9" tabindex="-1"></a>})</span>
<span id="cb166-10"><a href="cha-mcmc.html#cb166-10" tabindex="-1"></a></span>
<span id="cb166-11"><a href="cha-mcmc.html#cb166-11" tabindex="-1"></a>pumpConsts <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">N =</span> <span class="dv">10</span>,</span>
<span id="cb166-12"><a href="cha-mcmc.html#cb166-12" tabindex="-1"></a> <span class="at">t =</span> <span class="fu">c</span>(<span class="fl">94.3</span>, <span class="fl">15.7</span>, <span class="fl">62.9</span>, <span class="dv">126</span>, <span class="fl">5.24</span>,</span>
<span id="cb166-13"><a href="cha-mcmc.html#cb166-13" tabindex="-1"></a> <span class="fl">31.4</span>, <span class="fl">1.05</span>, <span class="fl">1.05</span>, <span class="fl">2.1</span>, <span class="fl">10.5</span>))</span>
<span id="cb166-14"><a href="cha-mcmc.html#cb166-14" tabindex="-1"></a>pumpData <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">5</span>, <span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">14</span>, <span class="dv">3</span>, <span class="dv">19</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">22</span>))</span>
<span id="cb166-15"><a href="cha-mcmc.html#cb166-15" tabindex="-1"></a>pumpInits <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">alpha =</span> <span class="dv">1</span>, <span class="at">beta =</span> <span class="dv">1</span>,</span>
<span id="cb166-16"><a href="cha-mcmc.html#cb166-16" tabindex="-1"></a> <span class="at">theta =</span> <span class="fu">rep</span>(<span class="fl">0.1</span>, pumpConsts<span class="sc">$</span>N))</span>
<span id="cb166-17"><a href="cha-mcmc.html#cb166-17" tabindex="-1"></a>pumpModel <span class="ot"><-</span> <span class="fu">nimbleModel</span>(pumpCode, pumpConsts, pumpData, pumpInits)</span>
<span id="cb166-18"><a href="cha-mcmc.html#cb166-18" tabindex="-1"></a>cpumpModel <span class="ot"><-</span> <span class="fu">compileNimble</span>(pumpModel)</span></code></pre></div>
<p>Here’s the use of the <code>logProb</code> derived quantity specified directly as an argument to <code>configureMCMC</code>.</p>
<div class="sourceCode" id="cb167"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb167-1"><a href="cha-mcmc.html#cb167-1" tabindex="-1"></a>conf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(pumpModel, <span class="at">logProb =</span> <span class="fu">c</span>(<span class="st">'alpha'</span>, <span class="st">'beta'</span>))</span></code></pre></div>
<pre><code>## ===== Monitors =====
## thin = 1: alpha, beta
## ===== Samplers =====
## RW sampler (1)
## - alpha
## conjugate sampler (11)
## - beta
## - theta[] (10 elements)
## ===== DerivedQ =====
## - logProb (1)</code></pre>
<div class="sourceCode" id="cb169"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb169-1"><a href="cha-mcmc.html#cb169-1" tabindex="-1"></a>conf<span class="sc">$</span><span class="fu">printDerivedQuantities</span>()</span></code></pre></div>
<pre><code>## [1] derived quantity: logProb, execution interval: thin, nodes: c(alpha, beta)</code></pre>
<div class="sourceCode" id="cb171"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb171-1"><a href="cha-mcmc.html#cb171-1" tabindex="-1"></a>mcmc <span class="ot"><-</span> <span class="fu">buildMCMC</span>(conf)</span>
<span id="cb171-2"><a href="cha-mcmc.html#cb171-2" tabindex="-1"></a>cmcmc <span class="ot"><-</span> <span class="fu">compileNimble</span>(mcmc, <span class="at">project =</span> pumpModel)</span>
<span id="cb171-3"><a href="cha-mcmc.html#cb171-3" tabindex="-1"></a><span class="fu">runMCMC</span>(cmcmc, <span class="at">niter =</span> <span class="dv">100</span>, <span class="at">thin =</span> <span class="dv">20</span>)</span></code></pre></div>
<pre><code>## $samples
## alpha beta
## [1,] 0.8545092 0.7039245
## [2,] 0.3856494 0.2021654
## [3,] 0.5972489 1.2048477
## [4,] 0.5846298 0.4347509
## [5,] 0.7408072 1.1426156
##
## $derived
## $derived$logProb
## alpha beta
## [1,] -0.8545092 -2.640661
## [2,] -0.3856494 -1.016076
## [3,] -0.5972489 -3.625278
## [4,] -0.5846298 -1.937780
## [5,] -0.7408072 -3.515316</code></pre>
<p>We could also have added the <code>logProb</code> derived quantity to an existing MCMC configuration argument:</p>
<div class="sourceCode" id="cb173"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb173-1"><a href="cha-mcmc.html#cb173-1" tabindex="-1"></a>conf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(pumpModel)</span>
<span id="cb173-2"><a href="cha-mcmc.html#cb173-2" tabindex="-1"></a>conf<span class="sc">$</span><span class="fu">addDerivedQuantity</span>(<span class="st">'logProb'</span>, <span class="at">nodes =</span> <span class="fu">c</span>(<span class="st">'alpha'</span>, <span class="st">'beta'</span>))</span></code></pre></div>
<p>When the <code>nodes</code> argument is a list, each list element is treated as a group of nodes, and the summed log-probability of each group is recorded.</p>
<p>The distinction between the character vector and list style argument for <code>nodes</code> is highlighted in the following two examples.
These also demonstrate how the keyword <code>".all"</code> may be provided, corresponding to all stochastic model nodes (including data).</p>
<div class="sourceCode" id="cb174"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb174-1"><a href="cha-mcmc.html#cb174-1" tabindex="-1"></a>conf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(pumpModel, <span class="at">logProb =</span> <span class="fu">c</span>(<span class="st">'theta[1:3]'</span>, <span class="st">'.all'</span>))</span></code></pre></div>
<pre><code>## ===== Monitors =====
## thin = 1: alpha, beta
## ===== Samplers =====
## RW sampler (1)
## - alpha
## conjugate sampler (11)
## - beta
## - theta[] (10 elements)
## ===== DerivedQ =====
## - logProb (1)</code></pre>
<div class="sourceCode" id="cb176"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb176-1"><a href="cha-mcmc.html#cb176-1" tabindex="-1"></a>mcmc2 <span class="ot"><-</span> <span class="fu">buildMCMC</span>(conf)</span>
<span id="cb176-2"><a href="cha-mcmc.html#cb176-2" tabindex="-1"></a>cmcmc2 <span class="ot"><-</span> <span class="fu">compileNimble</span>(mcmc2)</span>
<span id="cb176-3"><a href="cha-mcmc.html#cb176-3" tabindex="-1"></a><span class="fu">runMCMC</span>(cmcmc2, <span class="at">niter =</span> <span class="dv">3</span>)</span></code></pre></div>
<pre><code>## $samples
## alpha beta
## [1,] 0.7408072 0.4136291
## [2,] 0.2996073 0.7130421
## [3,] 0.7110410 0.6128930
##
## $derived
## $derived$logProb
## theta[1] theta[2] theta[3] _all_nodes_
## [1,] -0.03704378 -0.09705425 -0.11644720 -29.50155
## [2,] 0.29422045 0.40714322 0.54669490 -37.49239
## [3,] 0.23264741 0.24866465 0.05638813 -30.60517</code></pre>
<div class="sourceCode" id="cb178"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb178-1"><a href="cha-mcmc.html#cb178-1" tabindex="-1"></a>conf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(pumpModel,</span>
<span id="cb178-2"><a href="cha-mcmc.html#cb178-2" tabindex="-1"></a> <span class="at">logProb =</span> <span class="fu">list</span>(<span class="st">'theta[1:3]'</span>, <span class="st">'.all'</span>))</span></code></pre></div>
<pre><code>## ===== Monitors =====
## thin = 1: alpha, beta
## ===== Samplers =====
## RW sampler (1)
## - alpha
## conjugate sampler (11)
## - beta
## - theta[] (10 elements)
## ===== DerivedQ =====
## - logProb (1)</code></pre>
<div class="sourceCode" id="cb180"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb180-1"><a href="cha-mcmc.html#cb180-1" tabindex="-1"></a>mcmc3 <span class="ot"><-</span> <span class="fu">buildMCMC</span>(conf)</span>
<span id="cb180-2"><a href="cha-mcmc.html#cb180-2" tabindex="-1"></a>cmcmc3 <span class="ot"><-</span> <span class="fu">compileNimble</span>(mcmc3)</span>
<span id="cb180-3"><a href="cha-mcmc.html#cb180-3" tabindex="-1"></a><span class="fu">runMCMC</span>(cmcmc3, <span class="at">niter =</span> <span class="dv">3</span>)</span></code></pre></div>
<pre><code>## $samples
## alpha beta
## [1,] 0.6894996 1.078876
## [2,] 0.7352728 0.781088
## [3,] 0.7352728 0.419471