The CNTSELECT Procedure
Example 10.3 Bayesian Analysis Using NUTS with Multiple Chains
This example shows how to use the CNTSELECT procedure to estimate a model in the Bayesian framework by using the No-U-Turn Sampler (NUTS) with multiple Markov chains.
The following DATA step creates the data set DocVisit, which contains information for approximately 5,000 Australian individuals about the number and possible determinants of doctor visits that were made during a two-week interval. This data set contains a subset of variables that are taken from the data set Racd3 that was used by Cameron and Trivedi (1998).
data docvisit;
input sex age agesq income levyplus freepoor
freerepa illness actdays hscore chcond1 chcond2
doctorco nondocco hospadmi hospad medicine prescrib nonpresc;
datalines;
1 0.19 0.0361 0.55 1 0 0 1 4 1 0 0 1 0 0 0 1 1 0
1 0.19 0.0361 0.45 1 0 0 1 2 1 0 0 1 0 0 0 2 1 1
0 0.19 0.0361 0.9 0 0 0 3 0 0 0 0 1 0 1 4 2 1 1
0 0.19 0.0361 0.15 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0
0 0.19 0.0361 0.45 0 0 0 2 5 1 1 0 1 0 0 0 3 1 2
1 0.19 0.0361 0.35 0 0 0 5 1 9 1 0 1 0 0 0 1 1 0
1 0.19 0.0361 0.55 0 0 0 4 0 2 0 0 1 0 0 0 0 0 0
1 0.19 0.0361 0.15 0 0 0 3 0 6 0 0 1 0 0 0 1 1 0
1 0.19 0.0361 0.65 1 0 0 2 0 5 0 0 1 0 0 0 1 0 1
0 0.19 0.0361 0.15 1 0 0 1 0 0 0 0 1 0 0 0 1 1 0
0 0.19 0.0361 0.45 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0
... more lines ...
The variable Doctorco represents doctor visits. Additional variables in the data set that you want to evaluate as determinants of doctor visits include Sex (coded as 0=male, 1=female), Age (age in years divided by 100), Illness (number of illnesses during the two-week interval, with five or more coded as five), Income (annual income in Australian dollars divided by 1,000), and Hscore (a score on a general health questionnaire, in which a high score indicates bad health).
You can load the DocVisit data set into your CAS session by naming your CAS engine libref in the first statement of the following DATA step:
data mycas.docvisit;
set docvisit;
run;
These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined libref.
The following statements estimate a Poisson regression model in the Bayesian framework by using the No-U-Turn Sampler (NUTS) with multiple Markov chains. NUTS is specified in the SAMPLER= option in the BAYES statement, and you can set several values that control the sampler as arguments to the SAMPLER=NUTS option. The NCHAIN= option controls the number of Markov chains, and the NSAMPLE= option controls the size of the posterior sample per chain. PROC CNTSELECT automatically aggregates the results of each chain to create the posterior statistics and MCMC diagnostics tables that you request. The BYCHAIN suboption of the STATISTICS= and DIAGNOSTICS= options additionally produces those tables separately for each chain. This example produces the "Posterior Summaries" table, the "MCMC Diagnostic Summaries" table, and the "Heidelberger-Welch Diagnostics" table for each chain as well as for all chains combined.
proc cntselect data = mycas.docvisit;
class sex illness;
model doctorco=sex illness income hscore;
bayes seed = 5317 nsample = 2000 nchain = 4
sampler = nuts(ntune = 2000)
statistics(bychain) = summary
diagnostics(bychain) = (summary heidel);
run;
Output 10.3.1 shows the Bayesian estimation results. The "Posterior Summaries" table displays the estimates of the posterior mean and posterior quantiles that are computed using all four chains. The other posterior summary statistics tables that the STATISTICS= option produces would similarly be computed using all four chains. The "MCMC Diagnostic Summaries" table displays basic convergence diagnostics that are aggregated across all four chains. The "Autocorrelation Diagnostics" table would also be aggregated for multiple chains. For more information about how this aggregation is performed, see the section Using Multiple Chains in Chapter 2, Introduction to Bayesian Analysis Procedures. The "Heidelberger-Welch Diagnostics" table displays a summary of all the Heidelberger-Welch tests that are performed for each chain. Specifically, for each parameter it shows the number of chains that passed each test, as well as the largest number of iterations that were discarded for the stationarity test. The "Geweke Diagnostics" table and the "Raftery-Lewis Diagnostics" table would both be summarized similarly.
Output 10.3.1: Posterior Summaries and MCMC Diagnostics
| Posterior Summaries | ||||||||
|---|---|---|---|---|---|---|---|---|
| Parameter | N | Mean | Standard Deviation | Percentiles | ||||
| 2.5% | 25% | 50% | 75% | 97.5% | ||||
| Intercept | 8000 | -0.3880 | 0.0867 | -0.5604 | -0.4459 | -0.3877 | -0.3300 | -0.2209 |
| sex 0 | 8000 | -0.2200 | 0.0537 | -0.3267 | -0.2553 | -0.2202 | -0.1850 | -0.1145 |
| illness 0 | 8000 | -1.9385 | 0.1220 | -2.1783 | -2.0203 | -1.9384 | -1.8551 | -1.7014 |
| illness 1 | 8000 | -0.6981 | 0.0895 | -0.8773 | -0.7587 | -0.6976 | -0.6386 | -0.5215 |
| illness 2 | 8000 | -0.4704 | 0.0907 | -0.6479 | -0.5321 | -0.4687 | -0.4076 | -0.2932 |
| illness 3 | 8000 | -0.4880 | 0.0988 | -0.6832 | -0.5553 | -0.4863 | -0.4191 | -0.2978 |
| illness 4 | 8000 | -0.2736 | 0.1088 | -0.4857 | -0.3475 | -0.2720 | -0.1998 | -0.0602 |
| income | 8000 | -0.2534 | 0.0766 | -0.4016 | -0.3061 | -0.2530 | -0.2008 | -0.1071 |
| hscore | 8000 | 0.0943 | 0.00899 | 0.0768 | 0.0882 | 0.0944 | 0.1005 | 0.1117 |
| MCMC Diagnostic Summaries | |||||
|---|---|---|---|---|---|
| Parameter | MCSE | MCSE/SD | ESS | Autocorrelation Time | ESS/N |
| Intercept | 0.00195 | 0.0225 | 1983.1 | 4.0340 | 0.2479 |
| sex 0 | 0.000739 | 0.0137 | 5291.7 | 1.5118 | 0.6615 |
| illness 0 | 0.00233 | 0.0191 | 2749.8 | 2.9093 | 0.3437 |
| illness 1 | 0.00184 | 0.0205 | 2380.8 | 3.3602 | 0.2976 |
| illness 2 | 0.00191 | 0.0210 | 2259.7 | 3.5403 | 0.2825 |
| illness 3 | 0.00193 | 0.0195 | 2628.1 | 3.0441 | 0.3285 |
| illness 4 | 0.00192 | 0.0177 | 3197.7 | 2.5018 | 0.3997 |
| income | 0.00115 | 0.0150 | 4474.0 | 1.7881 | 0.5593 |
| hscore | 0.000139 | 0.0154 | 4209.6 | 1.9004 | 0.5262 |
| Heidelberger-Welch Diagnostics | |||||
|---|---|---|---|---|---|
| Parameter | Stationarity Test | Half-Width Test | |||
| Chains Tested | Tests Passed | Largest Iterations Discarded | Chains Tested | Tests Passed | |
| Intercept | 4 | 4 | 0 | 4 | 4 |
| sex 0 | 4 | 4 | 0 | 4 | 4 |
| illness 0 | 4 | 4 | 0 | 4 | 4 |
| illness 1 | 4 | 4 | 0 | 4 | 4 |
| illness 2 | 4 | 4 | 0 | 4 | 4 |
| illness 3 | 4 | 4 | 0 | 4 | 4 |
| illness 4 | 4 | 4 | 0 | 4 | 4 |
| income | 4 | 4 | 0 | 4 | 4 |
| hscore | 4 | 4 | 0 | 4 | 4 |
Detailed results for each chain are available in Output 10.3.2 as a result of the BYCHAIN options. The "Posterior Summaries by Chain" table displays the estimates of the posterior mean and posterior quantiles that are computed separately for each chain. Similarly, the "MCMC Diagnostic Summaries by Chain" table displays basic convergence diagnostics that are computed for each chain, and the "Heidelberger-Welch Diagnostics by Chain" table displays the Heidelberger-Welch diagnostics that are computed for each chain. For more information about the differences between the tables in Output 10.3.1 and Output 10.3.2, see the section Using Multiple Chains in Chapter 2, Introduction to Bayesian Analysis Procedures.
Output 10.3.2: Posterior Summaries and MCMC Diagnostics by Chain
| Posterior Summaries by Chain | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Chain | Parameter | N | Mean | Standard Deviation | Percentiles | ||||
| 2.5% | 25% | 50% | 75% | 97.5% | |||||
| 1 | Intercept | 2000 | -0.3890 | 0.0889 | -0.5713 | -0.4462 | -0.3878 | -0.3280 | -0.2251 |
| sex 0 | 2000 | -0.2187 | 0.0529 | -0.3273 | -0.2531 | -0.2177 | -0.1839 | -0.1174 | |
| illness 0 | 2000 | -1.9370 | 0.1222 | -2.1747 | -2.0219 | -1.9346 | -1.8559 | -1.6948 | |
| illness 1 | 2000 | -0.6966 | 0.0915 | -0.8787 | -0.7594 | -0.6977 | -0.6374 | -0.5124 | |
| illness 2 | 2000 | -0.4700 | 0.0934 | -0.6487 | -0.5323 | -0.4731 | -0.4078 | -0.2739 | |
| illness 3 | 2000 | -0.4842 | 0.1011 | -0.6870 | -0.5518 | -0.4815 | -0.4145 | -0.2853 | |
| illness 4 | 2000 | -0.2708 | 0.1131 | -0.4917 | -0.3475 | -0.2700 | -0.1955 | -0.0506 | |
| income | 2000 | -0.2534 | 0.0781 | -0.4102 | -0.3058 | -0.2503 | -0.2001 | -0.1076 | |
| hscore | 2000 | 0.0942 | 0.00889 | 0.0766 | 0.0881 | 0.0943 | 0.1002 | 0.1112 | |
| 2 | Intercept | 2000 | -0.3952 | 0.0859 | -0.5615 | -0.4570 | -0.3959 | -0.3353 | -0.2266 |
| sex 0 | 2000 | -0.2193 | 0.0552 | -0.3321 | -0.2557 | -0.2188 | -0.1822 | -0.1138 | |
| illness 0 | 2000 | -1.9295 | 0.1209 | -2.1668 | -2.0109 | -1.9284 | -1.8446 | -1.6998 | |
| illness 1 | 2000 | -0.6906 | 0.0902 | -0.8674 | -0.7525 | -0.6890 | -0.6284 | -0.5204 | |
| illness 2 | 2000 | -0.4633 | 0.0911 | -0.6502 | -0.5273 | -0.4612 | -0.3991 | -0.2961 | |
| illness 3 | 2000 | -0.4828 | 0.1008 | -0.6834 | -0.5509 | -0.4783 | -0.4116 | -0.2915 | |
| illness 4 | 2000 | -0.2672 | 0.1071 | -0.4802 | -0.3378 | -0.2648 | -0.1951 | -0.0595 | |
| income | 2000 | -0.2549 | 0.0783 | -0.4036 | -0.3112 | -0.2565 | -0.1998 | -0.1034 | |
| hscore | 2000 | 0.0945 | 0.00915 | 0.0768 | 0.0883 | 0.0946 | 0.1007 | 0.1121 | |
| 3 | Intercept | 2000 | -0.3854 | 0.0876 | -0.5567 | -0.4421 | -0.3858 | -0.3284 | -0.2149 |
| sex 0 | 2000 | -0.2211 | 0.0532 | -0.3244 | -0.2561 | -0.2209 | -0.1859 | -0.1177 | |
| illness 0 | 2000 | -1.9445 | 0.1223 | -2.1891 | -2.0227 | -1.9458 | -1.8619 | -1.7014 | |
| illness 1 | 2000 | -0.6998 | 0.0887 | -0.8772 | -0.7594 | -0.6989 | -0.6417 | -0.5174 | |
| illness 2 | 2000 | -0.4700 | 0.0898 | -0.6480 | -0.5313 | -0.4654 | -0.4091 | -0.2953 | |
| illness 3 | 2000 | -0.4888 | 0.0974 | -0.6759 | -0.5570 | -0.4884 | -0.4236 | -0.2999 | |
| illness 4 | 2000 | -0.2761 | 0.1079 | -0.4858 | -0.3522 | -0.2756 | -0.2011 | -0.0680 | |
| income | 2000 | -0.2544 | 0.0746 | -0.4029 | -0.3052 | -0.2540 | -0.2038 | -0.1121 | |
| hscore | 2000 | 0.0941 | 0.00878 | 0.0767 | 0.0881 | 0.0943 | 0.1002 | 0.1108 | |
| 4 | Intercept | 2000 | -0.3826 | 0.0837 | -0.5373 | -0.4400 | -0.3828 | -0.3258 | -0.2144 |
| sex 0 | 2000 | -0.2208 | 0.0536 | -0.3262 | -0.2564 | -0.2223 | -0.1867 | -0.1100 | |
| illness 0 | 2000 | -1.9431 | 0.1222 | -2.1852 | -2.0227 | -1.9432 | -1.8595 | -1.7071 | |
| illness 1 | 2000 | -0.7052 | 0.0873 | -0.8803 | -0.7631 | -0.7051 | -0.6456 | -0.5390 | |
| illness 2 | 2000 | -0.4781 | 0.0881 | -0.6426 | -0.5385 | -0.4774 | -0.4176 | -0.3060 | |
| illness 3 | 2000 | -0.4959 | 0.0955 | -0.6877 | -0.5606 | -0.4930 | -0.4303 | -0.3160 | |
| illness 4 | 2000 | -0.2801 | 0.1066 | -0.4918 | -0.3562 | -0.2784 | -0.2049 | -0.0751 | |
| income | 2000 | -0.2510 | 0.0753 | -0.3888 | -0.3037 | -0.2517 | -0.1998 | -0.1045 | |
| hscore | 2000 | 0.0946 | 0.00913 | 0.0770 | 0.0884 | 0.0944 | 0.1008 | 0.1126 | |
| MCMC Diagnostic Summaries by Chain | ||||||
|---|---|---|---|---|---|---|
| Chain | Parameter | MCSE | MCSE/SD | ESS | Autocorrelation Time | ESS/N |
| 1 | Intercept | 0.00380 | 0.0427 | 548.7 | 3.6447 | 0.2744 |
| sex 0 | 0.00148 | 0.0280 | 1272.5 | 1.5717 | 0.6363 | |
| illness 0 | 0.00433 | 0.0354 | 796.8 | 2.5099 | 0.3984 | |
| illness 1 | 0.00313 | 0.0342 | 852.5 | 2.3459 | 0.4263 | |
| illness 2 | 0.00364 | 0.0390 | 657.8 | 3.0405 | 0.3289 | |
| illness 3 | 0.00375 | 0.0371 | 728.3 | 2.7459 | 0.3642 | |
| illness 4 | 0.00327 | 0.0289 | 1198.6 | 1.6686 | 0.5993 | |
| income | 0.00217 | 0.0278 | 1293.4 | 1.5463 | 0.6467 | |
| hscore | 0.000253 | 0.0284 | 1238.4 | 1.6150 | 0.6192 | |
| 2 | Intercept | 0.00395 | 0.0460 | 471.6 | 4.2407 | 0.2358 |
| sex 0 | 0.00155 | 0.0280 | 1276.5 | 1.5668 | 0.6382 | |
| illness 0 | 0.00490 | 0.0405 | 608.2 | 3.2881 | 0.3041 | |
| illness 1 | 0.00368 | 0.0408 | 601.8 | 3.3231 | 0.3009 | |
| illness 2 | 0.00405 | 0.0445 | 504.4 | 3.9652 | 0.2522 | |
| illness 3 | 0.00367 | 0.0364 | 755.3 | 2.6480 | 0.3776 | |
| illness 4 | 0.00401 | 0.0374 | 713.4 | 2.8037 | 0.3567 | |
| income | 0.00243 | 0.0311 | 1035.3 | 1.9318 | 0.5176 | |
| hscore | 0.000274 | 0.0299 | 1116.4 | 1.7916 | 0.5582 | |
| 3 | Intercept | 0.00425 | 0.0486 | 423.8 | 4.7187 | 0.2119 |
| sex 0 | 0.00148 | 0.0279 | 1284.7 | 1.5567 | 0.6424 | |
| illness 0 | 0.00476 | 0.0389 | 660.8 | 3.0266 | 0.3304 | |
| illness 1 | 0.00410 | 0.0462 | 468.4 | 4.2699 | 0.2342 | |
| illness 2 | 0.00408 | 0.0455 | 483.2 | 4.1387 | 0.2416 | |
| illness 3 | 0.00422 | 0.0433 | 533.3 | 3.7506 | 0.2666 | |
| illness 4 | 0.00423 | 0.0392 | 650.3 | 3.0757 | 0.3251 | |
| income | 0.00211 | 0.0283 | 1251.4 | 1.5982 | 0.6257 | |
| hscore | 0.000298 | 0.0340 | 866.2 | 2.3090 | 0.4331 | |
| 4 | Intercept | 0.00352 | 0.0420 | 566.3 | 3.5319 | 0.2831 |
| sex 0 | 0.00139 | 0.0260 | 1479.3 | 1.3520 | 0.7397 | |
| illness 0 | 0.00458 | 0.0375 | 711.1 | 2.8125 | 0.3556 | |
| illness 1 | 0.00365 | 0.0418 | 571.1 | 3.5017 | 0.2856 | |
| illness 2 | 0.00342 | 0.0388 | 662.9 | 3.0168 | 0.3315 | |
| illness 3 | 0.00372 | 0.0389 | 659.7 | 3.0318 | 0.3298 | |
| illness 4 | 0.00374 | 0.0351 | 813.2 | 2.4593 | 0.4066 | |
| income | 0.00243 | 0.0322 | 963.4 | 2.0761 | 0.4817 | |
| hscore | 0.000281 | 0.0307 | 1060.3 | 1.8862 | 0.5302 | |
| Heidelberger-Welch Diagnostics by Chain | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Chain | Parameter | Stationarity Test | Half-Width Test | ||||||
| Cramer-von Mises Stat | p-Value | Test Outcome | Iterations Discarded | Half-Width | Mean | Relative Half-Width | Test Outcome | ||
| 1 | Intercept | 0.0933 | 0.6189 | Passed | 0 | 0.00679 | -0.3890 | -0.0174 | Passed |
| sex 0 | 0.1256 | 0.4735 | Passed | 0 | 0.00292 | -0.2187 | -0.0133 | Passed | |
| illness 0 | 0.0868 | 0.6537 | Passed | 0 | 0.00793 | -1.9370 | -0.00409 | Passed | |
| illness 1 | 0.0589 | 0.8208 | Passed | 0 | 0.00628 | -0.6966 | -0.00902 | Passed | |
| illness 2 | 0.0562 | 0.8377 | Passed | 0 | 0.00604 | -0.4700 | -0.0128 | Passed | |
| illness 3 | 0.0830 | 0.6749 | Passed | 0 | 0.00660 | -0.4842 | -0.0136 | Passed | |
| illness 4 | 0.0420 | 0.9225 | Passed | 0 | 0.00675 | -0.2708 | -0.0249 | Passed | |
| income | 0.2201 | 0.2318 | Passed | 0 | 0.00446 | -0.2534 | -0.0176 | Passed | |
| hscore | 0.0851 | 0.6630 | Passed | 0 | 0.000476 | 0.0942 | 0.00506 | Passed | |
| 2 | Intercept | 0.1632 | 0.3516 | Passed | 0 | 0.00785 | -0.3952 | -0.0199 | Passed |
| sex 0 | 0.1064 | 0.5545 | Passed | 0 | 0.00308 | -0.2193 | -0.0140 | Passed | |
| illness 0 | 0.0845 | 0.6664 | Passed | 0 | 0.0103 | -1.9295 | -0.00535 | Passed | |
| illness 1 | 0.1439 | 0.4088 | Passed | 0 | 0.00793 | -0.6906 | -0.0115 | Passed | |
| illness 2 | 0.1492 | 0.3920 | Passed | 0 | 0.00805 | -0.4633 | -0.0174 | Passed | |
| illness 3 | 0.1601 | 0.3601 | Passed | 0 | 0.00771 | -0.4828 | -0.0160 | Passed | |
| illness 4 | 0.2057 | 0.2567 | Passed | 0 | 0.00861 | -0.2672 | -0.0322 | Passed | |
| income | 0.1084 | 0.5454 | Passed | 0 | 0.00460 | -0.2549 | -0.0180 | Passed | |
| hscore | 0.1427 | 0.4127 | Passed | 0 | 0.000506 | 0.0945 | 0.00535 | Passed | |
| 3 | Intercept | 0.0849 | 0.6639 | Passed | 0 | 0.00870 | -0.3854 | -0.0226 | Passed |
| sex 0 | 0.0451 | 0.9054 | Passed | 0 | 0.00283 | -0.2211 | -0.0128 | Passed | |
| illness 0 | 0.0642 | 0.7875 | Passed | 0 | 0.0101 | -1.9445 | -0.00520 | Passed | |
| illness 1 | 0.0761 | 0.7149 | Passed | 0 | 0.00810 | -0.6998 | -0.0116 | Passed | |
| illness 2 | 0.1187 | 0.5006 | Passed | 0 | 0.00837 | -0.4700 | -0.0178 | Passed | |
| illness 3 | 0.0755 | 0.7180 | Passed | 0 | 0.00860 | -0.4888 | -0.0176 | Passed | |
| illness 4 | 0.0379 | 0.9437 | Passed | 0 | 0.00818 | -0.2761 | -0.0296 | Passed | |
| income | 0.1017 | 0.5767 | Passed | 0 | 0.00457 | -0.2544 | -0.0180 | Passed | |
| hscore | 0.0783 | 0.7016 | Passed | 0 | 0.000606 | 0.0941 | 0.00644 | Passed | |
| 4 | Intercept | 0.1059 | 0.5565 | Passed | 0 | 0.00614 | -0.3826 | -0.0160 | Passed |
| sex 0 | 0.0966 | 0.6016 | Passed | 0 | 0.00259 | -0.2208 | -0.0117 | Passed | |
| illness 0 | 0.1086 | 0.5443 | Passed | 0 | 0.00834 | -1.9431 | -0.00429 | Passed | |
| illness 1 | 0.0311 | 0.9724 | Passed | 0 | 0.00703 | -0.7052 | -0.00996 | Passed | |
| illness 2 | 0.0334 | 0.9637 | Passed | 0 | 0.00639 | -0.4781 | -0.0134 | Passed | |
| illness 3 | 0.0314 | 0.9712 | Passed | 0 | 0.00709 | -0.4959 | -0.0143 | Passed | |
| illness 4 | 0.0354 | 0.9551 | Passed | 0 | 0.00720 | -0.2801 | -0.0257 | Passed | |
| income | 0.0622 | 0.8003 | Passed | 0 | 0.00464 | -0.2510 | -0.0185 | Passed | |
| hscore | 0.2856 | 0.1484 | Passed | 0 | 0.000571 | 0.0946 | 0.00604 | Passed | |