Learn R Programming

JointAI: Joint Analysis and Imputation of Incomplete Data

The package JointAI provides functionality to perform joint analysis and imputation of a range of model types in the Bayesian framework. Implemented are (generalized) linear regression models and extensions thereof, models for (un-/ordered) categorical data, as well as multi-level (mixed) versions of these model types.

Moreover, survival models and joint models for longitudinal and survival data are available. It is also possible to fit multiple models of mixed types simultaneously. Missing values in (if present) will be imputed automatically.

JointAI performs some preprocessing of the data and creates a JAGS model, which will then automatically be passed to JAGS with the help of the R package rjags.

Besides the main modelling functions, JointAI also provides a number of functions to summarize and visualize results and incomplete data.

Installation

JointAI can be installed from CRAN:

install.packages('JointAI')

Alternatively, you can install JointAI from GitHub:

# install.packages("remotes")
remotes::install_github("NErler/JointAI")

Main functions

JointAI provides the following main functions:

lm_imp()                 # linear regression
glm_imp()                # generalized linear regression
clm_imp()                # cumulative logit model
mlogit_imp()             # multinomial logit model
lognorm_imp()            # log-normal regression
betareg_imp()            # beta regression
lme_imp() / lmer_imp()   # linear mixed model
glme_imp() / glmer_imp() # generalized linear mixed model
clmm_imp()               # cumulative logit mixed model
mlogitmm_imp()           # multinomial logit model
lognormmm_imp()          # log-normal regression
betamm_imp()             # beta regression
survreg_imp()            # parametric (Weibull) survival model
coxph_imp()              # proportional hazards survival model
JM_imp()                 # joint model for longitudinal and survival data

The functions use specification similar to that of well known standard functions like lm() and glm() from base R, nlme::lme() (from the package nlme) , lme4::lmer() or lme4::glmer() (from the package lme4) and survival::survreg() and survival::coxph() (from the package survival).

Functions summary(), coef(), traceplot() and densplot() provide a summary of the posterior distribution and its visualization.

GR_crit() and MC_error() implement the Gelman-Rubin diagnostic for convergence and the Monte Carlo error of the MCMC sample, respectively.

JointAI also provides functions for exploration of the distribution of the data and missing values, export of imputed values and prediction.

Minimal Example

Visualize the observed data and missing data pattern

library(JointAI)

plot_all(
  NHANES[c(1, 5:6, 8:12)],
  fill = '#D10E3B',
  border = '#460E1B',
  ncol = 4,
  breaks = 30
)
md_pattern(NHANES, color = c('#460E1B', '#D10E3B'))

Fit a linear regression model with incomplete covariates

lm1 <- lm_imp(
  SBP ~ gender + age + WC + alc + educ + bili,
  data = NHANES,
  n.iter = 500,
  progress.bar = 'none',
  seed = 2020
)

Visualize the MCMC sample

traceplot(lm1, col = c('#d4af37', '#460E1B', '#D10E3B'), ncol = 4)
densplot(lm1, col = c('#d4af37', '#460E1B', '#D10E3B'), ncol = 4, lwd = 2)

Summarize the Result

summary(lm1)
#> 
#> Bayesian linear model fitted with JointAI
#> 
#> Call:
#> lm_imp(formula = SBP ~ gender + age + WC + alc + educ + bili, 
#>     data = NHANES, n.iter = 500, seed = 2020, progress.bar = "none")
#> 
#> 
#> Posterior summary:
#>                Mean     SD     2.5%   97.5% tail-prob. GR-crit MCE/SD
#> (Intercept)  87.662 8.6088  70.3830 104.899    0.00000    1.00 0.0271
#> genderfemale -3.487 2.2407  -7.9563   0.818    0.10533    1.01 0.0258
#> age           0.334 0.0683   0.1986   0.468    0.00000    1.01 0.0258
#> WC            0.230 0.0721   0.0876   0.376    0.00133    1.00 0.0258
#> alc>=1        6.419 2.3862   1.6656  11.112    0.00667    1.03 0.0358
#> educhigh     -2.805 2.0681  -6.9371   1.339    0.17067    1.00 0.0258
#> bili         -5.277 4.7332 -14.7727   3.596    0.25333    1.01 0.0275
#> 
#> Posterior summary of residual std. deviation:
#>           Mean    SD 2.5% 97.5% GR-crit MCE/SD
#> sigma_SBP 13.5 0.725 12.2    15    1.01 0.0258
#> 
#> 
#> MCMC settings:
#> Iterations = 101:600
#> Sample size per chain = 500 
#> Thinning interval = 1 
#> Number of chains = 3 
#> 
#> Number of observations: 186
coef(lm1)
#> $SBP
#>  (Intercept) genderfemale          age           WC       alc>=1     educhigh 
#>   87.6622381   -3.4873104    0.3335133    0.2302755    6.4194926   -2.8054874 
#>         bili    sigma_SBP 
#>   -5.2768560   13.5278177

confint(lm1)
#> $SBP
#>                      2.5%       97.5%
#> (Intercept)   70.38301720 104.8986161
#> genderfemale  -7.95631510   0.8182921
#> age            0.19857014   0.4678630
#> WC             0.08761699   0.3756334
#> alc>=1         1.66562640  11.1121370
#> educhigh      -6.93714769   1.3389344
#> bili         -14.77269911   3.5955383
#> sigma_SBP     12.16165429  15.0367180

Copy Link

Version

Install

install.packages('JointAI')

Monthly Downloads

489

Version

1.1.0

License

GPL (>= 2)

Maintainer

Nicole S. Erler

Last Published

January 30th, 2026

Functions in JointAI (1.1.0)

check_vars_in_data

Check that all variables in formulas are in the data
check_groups_vary_within_lvl

Check if a grouping variable varies within another grouping variable
expand_rd_vcov_full

Expand rd_vcov using variable names in case "full" is used
duration_obj

Create a duration object
extract_state

Return the current state of a 'JointAI' model
combine_formulas

Combine a fixed and random effects formula
extract_lhs_string

Extract the left hand side of a formula
drop_levels

Check for empty factor levels
check_duplicate_groupings

Check for duplicate grouping levels
check_fixed_random

Check whether fixed or formula contains a random effects specification
extract_ranef_formula

Extract random effects formula from lme4-type formula
compare_data_structure

Compare the structure of two data.frames
clean_names

Replace ":" with "_" in a string
check_na_groupings

Check for missing values in grouping variables
choose_default_model

Choose default analysis model based on outcome and data level
difftime_df

Converts a difftime object to a data.frame
extract_lhs_varnames

Extract variable names from the left-hand side of a formula
get_resp_mat

Identify the data matrix containing a given response variable
get_Mlist

Re-create the full Mlist from a "JointAI" object
get_nranef

Extract the number of random effects
get_missinfo

Obtain a summary of the missing values involved in an object of class JointAI
hc_rdslope_info

Get info on main effects in a rd slope structure for a level and sub-model
hc_rdslope_interact

Get info on the interactions with random slopes for a given level and sub-model
get_datlvls

Determine grouping level of data
densplot

Plot the posterior density from object of class JointAI
default_hyperpars

Get the default values for hyper-parameters
parameters

Parameter names of an JointAI object
model_imp

Joint Analysis and Imputation of incomplete data
plot_all

Visualize the distribution of all variables in the dataset
merge_call_args

Merge call arguments with default formals
check_classes

Check classes of all variables used in the model
predDF

Create a new data frame for prediction
paste_analysis_type

Paste analysis type with family information
check_data

Run all data related checks
plot_imp_distr

Plot the distribution of observed and imputed values
check_rd_vcov_list

First validation for rd_vcov
internal_clean_survname

Convert a survival outcome to a model name
check_rd_vcov

Check / create the random effects variance-covariance matrix specification
extract_grouping

Extract grouping variables from a (list of) formula(s)
extract_fixef_formula

Extract fixed effects formula from lme4-type formula
get_modeltype

Identify the general model type from the covariate model type
paste_coef

Write the coefficient part of a linear predictor
get_family

Identify the family from the covariate model type
get_grouping_levels

Get grouping levels
sharedParams

Parameters used by several functions in JointAI
traceplot

Create traceplots for a MCMC sample
set_refcat

Specify reference categories for all categorical covariates in the model
predict.JointAI

Predict values from an object of class JointAI
remove_formula_grouping

Remove grouping part from (random effects) formula
remove_grouping

Remove grouping part from (random effects) formulas
varname_to_modelframe

Create data.frame from variable term and data
wideDF

Cross-sectional example dataset
two_value_to_factor

Convert two-value vectors to factors
longDF

Longitudinal example dataset
md_pattern

Missing data pattern
list_models

List model details
reformat_difftime

Set all elements of a difftime object to the same, largest meaningful unit
replace_nan_with_na

Replace NaN values with NA
prep_arglist

Prepare list of arguments for model_imp()
sum_duration

Calculate the sum of the computational duration of a JointAI object
clean_survname

Convert a survival outcome to a model name
combine_formula_lists

Combine fixed and random effects formulas
paste_data

Write the data element of a linear predictor
factor_to_integer

Convert a factor to an integer representation
rd_terms_by_grouping

Extract terms by grouping variables from a formula
paste_scaling

Wrap a data element of a linear predictor in scaling syntax
remove_lhs

Remove the left hand side of a (list of) formula(s)
split_formula_list

Split a list of formulas into fixed and random effects parts.
simLong

Simulated Longitudinal Data in Long and Wide Format
get_MIdat

Extract multiple imputed datasets from an object of class JointAI
cross_corr

Cross-correlation of MCMC samples
convert_variables

Convert variables
resolve_family_obj

Resolve family object
paste_linpred

Write a linear predictor
print.Dmat

Summarize the results from an object of class JointAI
get_groups

Get grouping information
plot.JointAI

Plot an object object inheriting from class 'JointAI'
get_listelement

Get an element of a list, return a default value if it does not exist
rd_vcov

Extract the random effects variance covariance matrix
normalize_formula_args

Normalize formula arguments in arglist
residuals.JointAI

Extract residuals from an object of class JointAI
paste_scale

Create the scaling in a data element of a linear predictor
ns

Generate a Basis Matrix for Natural Cubic Splines
NHANES

National Health and Nutrition Examination Survey (NHANES) Data
add_samples

Continue sampling from an object of class JointAI
GR_crit

Gelman-Rubin criterion for convergence
JointAI

JointAI: Joint Analysis and Imputation of Incomplete Data
JointAIObject

Fitted object of class 'JointAI'
MC_error

Calculate and plot the Monte Carlo error
auto_corr

Autocorrelation of MCMC samples
all_vars

Extract names of variables from several objects
check_redundant_lvls

Check for unnecessary grouping levels
Surv

Create a Survival Object
add_linebreaks

Add line breaks to a linear predictor string
PBC

PBC data
bs

B-Spline Basis for Polynomial Splines
check_full_blockdiag

Replace a full with a block-diagonal variance covariance matrix
check_formula_list

Ensure object is a (list of) formula(s)