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

604

Version

1.0.6

License

GPL (>= 2)

Maintainer

Last Published

April 2nd, 2024

Functions in JointAI (1.0.6)

clean_survname

Convert a survival outcome to a model name
densplot

Plot the posterior density from object of class JointAI
default_hyperpars

Get the default values for hyper-parameters
check_full_blockdiag

Replace a full with a block-diagonal variance covariance matrix Check if a full random effects variance covariance matrix is specified for a single variable. In that case, it is identical to a block-diagonal matrix. Change the rd_vcov specification to blockdiag for clarity (because then the variable name is used in the name of b, D, invD, ...)
check_rd_vcov

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

B-Spline Basis for Polynomial Splines
check_rd_vcov_list

First validation for rd_vcov
combine_formulas

Combine a fixed and random effects formula
combine_formula_lists

Combine fixed and random effects formulas
extract_state

Return the current state of a 'JointAI' model
check_formula_list

Check/convert formula to list
expand_rd_vcov_full

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

Extract the left hand side of a formula
get_MIdat

Extract multiple imputed datasets from an object of class JointAI
get_Mlist

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

Extract all id variables from a list of random effects formulas
duration_obj

Create a duration object
difftime_df

Converts a difftime object to a data.frame
get_missinfo

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

Identify the family from the covariate model type
longDF

Longitudinal example dataset
model_imp

Joint Analysis and Imputation of incomplete data
hc_rdslope_interact

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

Identify the general model type from the covariate model type
get_resp_mat

Identify the data matrix containing a given response variable
ns

Generate a Basis Matrix for Natural Cubic Splines
list_models

List model details
get_nranef

Extract the number of random effects
plot_imp_distr

Plot the distribution of observed and imputed values
paste_data

Write the data element of a linear predictor
predDF

Create a new data frame for prediction
paste_linpred

Write a linear predictor
split_formula_list

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

Parameter names of an JointAI object
paste_coef

Write the coefficient part of a linear predictor
reformat_difftime

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

Extract the random effects variance covariance matrix Returns the posterior mean of the variance-covariance matrix/matrices of the random effects in a fitted JointAI object.
split_formula

Split a formula into fixed and random effects parts
remove_lhs

Remove the left hand side of a (list of) formula(s)
plot.JointAI

Plot an object object inheriting from class 'JointAI'
md_pattern

Missing data pattern
predict.JointAI

Predict values from an object of class JointAI
plot_all

Visualize the distribution of all variables in the dataset
sum_duration

Calculate the sum of the computational duration of a JointAI object
residuals.JointAI

Extract residuals from an object of class JointAI
traceplot

Create traceplots for a MCMC sample
hc_rdslope_info

Get info on the main effects in a random slope structure for a given level and sub-model
simLong

Simulated Longitudinal Data in Long and Wide Format
paste_scale

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

Specify reference categories for all categorical covariates in the model
print.Dmat

Summarize the results from an object of class JointAI
paste_scaling

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

Cross-sectional example dataset
sharedParams

Parameters used by several functions in JointAI
JointAIObject

Fitted object of class 'JointAI'
Surv

Create a Survival Object
MC_error

Calculate and plot the Monte Carlo error
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
all_vars

Extract names of variables from a (list of) formula(s)
add_linebreaks

Add line breaks to a linear predictor string
PBC

PBC data
NHANES

National Health and Nutrition Examination Survey (NHANES) Data