This vignette gives a first and very brief overview of how the package JointAI can be used. The different settings and options are explained in more depth in the help pages and other vignettes.

Here, we use the NHANES data that are part of the JointAI package. For more info on this data, check the help file for the NHANES data, go to the web page of the National Health and Nutrition Examination Survey (NHANES) and check out the vignette Visualizing Incomplete Data, in which the NHANES data is explored.

Fitting a linear regression model

Fitting a linear regression model with JointAI is straightforward with the function lm_imp():

lm1 <- lm_imp(SBP ~ gender + age + race + WC + alc + educ + albu + bili,
data = NHANES, n.iter = 500)
#>
|
|                                                  |   0%
|
|*                                                 |   2%
|
|**                                                |   4%
|
|***                                               |   6%
|
|****                                              |   8%
|
|*****                                             |  10%
|
|******                                            |  12%
|
|*******                                           |  14%
|
|********                                          |  16%
|
|*********                                         |  18%
|
|**********                                        |  20%
|
|***********                                       |  22%
|
|************                                      |  24%
|
|*************                                     |  26%
|
|**************                                    |  28%
|
|***************                                   |  30%
|
|****************                                  |  32%
|
|*****************                                 |  34%
|
|******************                                |  36%
|
|*******************                               |  38%
|
|********************                              |  40%
|
|*********************                             |  42%
|
|**********************                            |  44%
|
|***********************                           |  46%
|
|************************                          |  48%
|
|*************************                         |  50%
|
|**************************                        |  52%
|
|***************************                       |  54%
|
|****************************                      |  56%
|
|*****************************                     |  58%
|
|******************************                    |  60%
|
|*******************************                   |  62%
|
|********************************                  |  64%
|
|*********************************                 |  66%
|
|**********************************                |  68%
|
|***********************************               |  70%
|
|************************************              |  72%
|
|*************************************             |  74%
|
|**************************************            |  76%
|
|***************************************           |  78%
|
|****************************************          |  80%
|
|*****************************************         |  82%
|
|******************************************        |  84%
|
|*******************************************       |  86%
|
|********************************************      |  88%
|
|*********************************************     |  90%
|
|**********************************************    |  92%
|
|***********************************************   |  94%
|
|************************************************  |  96%
|
|************************************************* |  98%
|
|**************************************************| 100%

The specification of lm_imp() is similar to the specification of a linear regression model for complete data using lm(). In this minimal example, the only difference is that for lm_imp() the number of iterations n.iter has to be specified. Of course, there are many more parameters that can (and sometimes should) be specified. In the vignette Model Specification, many of these parameters are explained in detail.

n.iter specifies the length of the Markov chain, i.e., the number of draws from the posterior distribution of the parameter or unobserved value. How many iterations are necessary depends on the data and complexity of the model and can vary from as few as 100 up to thousands or millions.

One important criterion is that the Markov chains need to have converged. Convergence can be evaluated visually with a trace plot.

Traceplot

traceplot(lm1)

The function traceplot() produces a plot of the sampled values across iterations per parameter. Different chains are represented by different colours.

When the sampler has converged, the chains show a horizontal band, as in the above figure. Consequently, when traces show a trend, convergence has not been reached, and more iterations are necessary (e.g., using the function add_samples()).

When the chains have converged, we can obtain the result of the model from the model summary.

Model Summary

Results from a model fitted with JointAI can be printed using summary():

summary(lm1)
#>
#> Bayesian linear model fitted with JointAI
#>
#> Call:
#> lm_imp(formula = SBP ~ gender + age + race + WC + alc + educ +
#>     albu + bili, data = NHANES, n.iter = 500)
#>
#>
#> Posterior summary:
#>                          Mean      SD     2.5%   97.5% tail-prob. GR-crit MCE/SD
#> (Intercept)            62.175 22.7300  17.3968 109.167    0.00533    1.00 0.0258
#> genderfemale           -3.169  2.3143  -7.7968   1.230    0.15467    1.00 0.0281
#> age                     0.361  0.0726   0.2200   0.503    0.00000    1.00 0.0258
#> raceOther Hispanic      0.419  4.9972  -9.6118  10.125    0.93733    1.00 0.0258
#> raceNon-Hispanic White -1.491  3.0729  -7.2896   4.572    0.61200    1.00 0.0292
#> raceNon-Hispanic Black  8.797  3.5617   2.1012  15.660    0.01467    1.00 0.0258
#> raceother               3.588  3.4812  -3.2597  10.519    0.29600    1.00 0.0258
#> WC                      0.237  0.0845   0.0759   0.410    0.00667    1.00 0.0258
#> alc>=1                  7.175  2.2572   2.7547  11.546    0.00533    1.00 0.0349
#> educhigh               -3.361  2.1779  -7.6632   1.026    0.12800    1.01 0.0268
#> albu                    5.100  3.9600  -2.8385  12.710    0.20667    1.01 0.0270
#> bili                   -5.739  5.0829 -15.8350   4.136    0.25333    1.00 0.0336
#>
#> Posterior summary of residual std. deviation:
#>           Mean    SD 2.5% 97.5% GR-crit MCE/SD
#> sigma_SBP 13.2 0.706 11.9  14.7       1 0.0295
#>
#>
#> MCMC settings:
#> Iterations = 101:600
#> Sample size per chain = 500
#> Thinning interval = 1
#> Number of chains = 3
#>
#> Number of observations: 186

The output gives the posterior summary, i.e., the summary of the MCMC (Markov Chain Monte Carlo) sample, which consists of all chains combined.

By default, summary() will only print the posterior summary for the main model parameters of the analysis model. How one can select which parameters are shown is described in the vignette Selecting Parameters.

The summary consists of the posterior mean, the standard deviation, 2.5% and 97.5% quantiles of the MCMC sample, the tail probability, the Gelman-Rubin criterion for convergence and the ratio of the Monte Carlo error to the standard deviation of the posterior sample.

Tail probability

The tail probability is a measure of how likely the value 0 is under the estimated posterior distribution, and is calculated as $2\times\min\left\{Pr(\theta > 0), Pr(\theta < 0)\right\}$ (where $$\theta$$ is the parameter of interest).

In the following graphics, the shaded areas represent the minimum of $$Pr(\theta > 0)$$ and $$Pr(\theta < 0)$$:

Gelman-Rubin criterion

The Gelman-Rubin1 criterion, also available via the function GR_crit(), compares the within and between chain variation. When it is close enough to 12, the chains can be assumed to have converged.

Monte Carlo error

The Monte Carlo error is a measure of the error that is made because the estimate is based on a finite sample. It can also be obtained using the function MC_error(). Ideally, the Monte Carlo error should be small compared to the standard error of the posterior sample. The values shown in the model summary are the Monte Carlo error divided by the posterior standard deviation.

In the model summary, additionally, some important characteristics of the MCMC samples on which the summary is based, are given. This includes the range and number of iterations (= Sample size per chain), thinning interval and number of chains.

Furthermore, the number of observations (the sample size of the data) is given.

With the arguments start, end and thin it is possible to select which iterations from the MCMC sample are included in the summary.

For example:
When the traceplot shows that the chains only converged after 1500 iterations, start = 1500 should be specified in summary().

A summary of the missing values in all variables involved in the model can be added to the summary by setting missinfo = TRUE.

Plot of the posterior distributions

The posterior distributions can be visualized using the function densplot():

densplot(lm1)

By default, densplot() plots the empirical distribution of each of the chains separately. When joined = TRUE the distributions of the combined chains are plotted.

Other types of models

Besides linear regression models, it is also possible to fit

It is also possible to specify multiple analysis models at the same time, by providing a list of model formulas.

1. Gelman, A and Rubin, DB (1992) Inference from iterative simulation using multiple sequences, Statistical Science, 7, 457-511.
Brooks, SP. and Gelman, A. (1998) General methods for monitoring convergence of iterative simulations. Journal of Computational and Graphical Statistics, 7, 434-455.↩︎

2. for example < 1.1; but this is not a generally accepted cut-off↩︎