Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ template:
code-color: "#991433"
code-color-dark: "#D10E3B"
link-color-dark: "#D10E3B"
math-rendering: katex

navbar:
structure:
Expand All @@ -23,7 +24,7 @@ navbar:
icon: fa-home
href: index.html
aria-label: home


figures:
dev: grDevices::png
92 changes: 19 additions & 73 deletions vignettes/AfterFitting.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "After Fitting"
subtitle: "Summary, visualization and evaluation of the results"
date: January 03, 2026
date: February 22, 2026
output:
rmarkdown::html_vignette:
toc: true
Expand Down Expand Up @@ -55,10 +55,7 @@ mod13a <- lm_imp(SBP ~ gender + WC + alc + creat, data = NHANES, n.iter = 500,
traceplot(mod13a)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/trace13a-1.svg" alt="plot of chunk trace13a" width="100%" />
<p class="caption">plot of chunk trace13a</p>
</div>
<img src="figures_AfterFitting/trace13a-1.svg" alt="" width="100%" style="display: block; margin: auto;" />


When the sampler has converged the chains show one horizontal band, as in the above figure.
Expand All @@ -81,10 +78,7 @@ traceplot(mod13a, ncol = 3, use_ggplot = TRUE) +
scale_color_brewer(palette = 'Dark2')
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/ggtrace15a-1.svg" alt="plot of chunk ggtrace15a" width="100%" />
<p class="caption">plot of chunk ggtrace15a</p>
</div>
<img src="figures_AfterFitting/ggtrace15a-1.svg" alt="" width="100%" style="display: block; margin: auto;" />


### Density plot
Expand All @@ -103,10 +97,7 @@ densplot(mod13a, ncol = 3, col = c("darkred", "darkblue", "darkgreen"),
col = grey(0.8))))
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/dens13a-2-1.svg" alt="plot of chunk dens13a-2" width="100%" />
<p class="caption">plot of chunk dens13a-2</p>
</div>
<img src="figures_AfterFitting/dens13a-2-1.svg" alt="" width="100%" style="display: block; margin: auto;" />
or marking the posterior mean and 2.5% and 97.5% quantiles:

``` r
Expand All @@ -122,10 +113,7 @@ densplot(mod13a, ncol = 3,
)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/densplot15a-1.svg" alt="plot of chunk densplot15a" width="100%" />
<p class="caption">plot of chunk densplot15a</p>
</div>
<img src="figures_AfterFitting/densplot15a-1.svg" alt="" width="100%" style="display: block; margin: auto;" />


Like with `traceplot()` it is possible to use the **ggplot2** version of `densplot()`
Expand Down Expand Up @@ -179,10 +167,7 @@ p13a +
labels = c('JointAI', 'compl.case'))
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/ggdens15a-1.svg" alt="plot of chunk ggdens15a" width="100%" />
<p class="caption">plot of chunk ggdens15a</p>
</div>
<img src="figures_AfterFitting/ggdens15a-1.svg" alt="" width="100%" style="display: block; margin: auto;" />


## Model Summary
Expand Down Expand Up @@ -319,10 +304,7 @@ estimated posterior distribution.
The figure visualizes three examples of posterior distributions
and the corresponding minimum of $Pr(\theta > 0)$ and $Pr(\theta < 0)$
(shaded area):
<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/tailprob-1.svg" alt="plot of chunk tailprob" width="100%" />
<p class="caption">plot of chunk tailprob</p>
</div>
<img src="figures_AfterFitting/tailprob-1.svg" alt="" width="100%" style="display: block; margin: auto;" />


## Evaluation criteria
Expand Down Expand Up @@ -411,10 +393,7 @@ plot(MC_error(mod13a)) # left panel: all iterations 101:600
plot(MC_error(mod13a, end = 250)) # right panel: iterations 101:250
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/MCE15a-1.svg" alt="plot of chunk MCE15a" width="100%" />
<p class="caption">plot of chunk MCE15a</p>
</div>
<img src="figures_AfterFitting/MCE15a-1.svg" alt="" width="100%" style="display: block; margin: auto;" />


## Subset of output {#sec:subset}
Expand Down Expand Up @@ -515,10 +494,7 @@ densplot(mod13c, subset = list(analysis_main = FALSE,
other = c('beta[4]', 'beta[5]')), nrow = 1)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/densplot13c-1.svg" alt="plot of chunk densplot13c" width="70%" />
<p class="caption">plot of chunk densplot13c</p>
</div>
<img src="figures_AfterFitting/densplot13c-1.svg" alt="" width="70%" style="display: block; margin: auto;" />

This also works when a subset of the imputed values should be displayed:

Expand All @@ -537,10 +513,7 @@ sub3
traceplot(mod13d, subset = list(analysis_main = FALSE, other = sub3), ncol = 2)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/trace15d-1.svg" alt="plot of chunk trace15d" width="70%" />
<p class="caption">plot of chunk trace15d</p>
</div>
<img src="figures_AfterFitting/trace15d-1.svg" alt="" width="70%" style="display: block; margin: auto;" />

When the number of imputed values is large or in order to check convergence
of random effects, it may not be feasible to plot and inspect all trace plots.
Expand All @@ -561,10 +534,7 @@ traceplot(mod13e, subset = list(analysis_main = FALSE,
other = sample(ri, size = 12)), ncol = 4)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/ri-1.svg" alt="plot of chunk ri" width="100%" />
<p class="caption">plot of chunk ri</p>
</div>
<img src="figures_AfterFitting/ri-1.svg" alt="" width="100%" style="display: block; margin: auto;" />


### Subset of MCMC samples
Expand All @@ -586,47 +556,32 @@ mod14 <- lm_imp(SBP ~ gender + WC + alc + creat, data = NHANES, n.iter = 100,
densplot(mod14, ncol = 3)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/mod14-1.svg" alt="plot of chunk mod14" width="90%" />
<p class="caption">plot of chunk mod14</p>
</div>
<img src="figures_AfterFitting/mod14-1.svg" alt="" width="90%" style="display: block; margin: auto;" />

``` r
densplot(mod14, exclude_chains = c(2,4), ncol = 3)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/mod14-2.svg" alt="plot of chunk mod14" width="90%" />
<p class="caption">plot of chunk mod14</p>
</div>
<img src="figures_AfterFitting/mod14-2.svg" alt="" width="90%" style="display: block; margin: auto;" />


``` r
traceplot(mod14, thin = 10, ncol = 3)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/trace14-1.svg" alt="plot of chunk trace14" width="90%" />
<p class="caption">plot of chunk trace14</p>
</div>
<img src="figures_AfterFitting/trace14-1.svg" alt="" width="90%" style="display: block; margin: auto;" />

``` r
traceplot(mod14, start = 150, ncol = 3)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/trace14-2.svg" alt="plot of chunk trace14" width="90%" />
<p class="caption">plot of chunk trace14</p>
</div>
<img src="figures_AfterFitting/trace14-2.svg" alt="" width="90%" style="display: block; margin: auto;" />

``` r
traceplot(mod14, end = 120, ncol = 3)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/trace14-3.svg" alt="plot of chunk trace14" width="90%" />
<p class="caption">plot of chunk trace14</p>
</div>
<img src="figures_AfterFitting/trace14-3.svg" alt="" width="90%" style="display: block; margin: auto;" />



Expand Down Expand Up @@ -697,10 +652,7 @@ matplot(pred$newdata$age, pred$fitted,
xlab = 'age in months', ylab = 'predicted value')
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/predplot-1.svg" alt="plot of chunk predplot" width="90%" />
<p class="caption">plot of chunk predplot</p>
</div>
<img src="figures_AfterFitting/predplot-1.svg" alt="" width="90%" style="display: block; margin: auto;" />

It is possible to have multiple variables vary and to set values for these
variables:
Expand Down Expand Up @@ -734,10 +686,7 @@ ggplot(pred$newdata, aes(x = age, y = fit, color = factor(HEIGHT_M),
scale_y_continuous(name = 'Expected BMI', breaks = seq(15, 18, 0.5))
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/predplotgg-1.svg" alt="plot of chunk predplotgg" width="90%" />
<p class="caption">plot of chunk predplotgg</p>
</div>
<img src="figures_AfterFitting/predplotgg-1.svg" alt="" width="90%" style="display: block; margin: auto;" />



Expand Down Expand Up @@ -785,8 +734,5 @@ of the observed and imputed values.
plot_imp_distr(impDF, nrow = 1)
```

<div class="figure" style="text-align: center">
<img src="figures_AfterFitting/plotimpdistr-1.svg" alt="plot of chunk plotimpdistr" width="100%" />
<p class="caption">plot of chunk plotimpdistr</p>
</div>
<img src="figures_AfterFitting/plotimpdistr-1.svg" alt="" width="100%" style="display: block; margin: auto;" />

4 changes: 3 additions & 1 deletion vignettes/AfterFitting.Rmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ knitr::opts_chunk$set(
fig.width = 7,
out.width = '100%',
dev = "svglite",
fig.cap = "",
fig.scap = "",
fig.align = 'center',
fig.path = "figures_AfterFitting/"
)
Expand Down Expand Up @@ -79,7 +81,7 @@ rows and columns for the layout of the grid of plots.

With the argument `use_ggplot` it is possible to get a [**ggplot2**](https://CRAN.R-project.org/package=ggplot2)
version of the traceplot. It can be extended using standard **ggplot2** syntax.
```{r ggtrace15a, fig.width = 7, fig.height = 3.5, out.width = '100%'}
```{r ggtrace15a, fig.width = 7, fig.height = 3.5, out.width = '100%', message = FALSE}
library(ggplot2)
traceplot(mod13a, ncol = 3, use_ggplot = TRUE) +
theme(legend.position = 'bottom') +
Expand Down
43 changes: 17 additions & 26 deletions vignettes/MinimalExample.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Minimal Example"
description: "A minimal example to demonstrate the usage of the R package JointAI."
author: "Nicole Erler"
date: January 03, 2026
date: February 22, 2026
resource_files:
- man/figures/JointAI_square.png
opengraph:
Expand Down Expand Up @@ -71,10 +71,7 @@ Convergence can be evaluated visually with a trace plot.
traceplot(lm1)
```

<div class="figure" style="text-align: center">
<img src="figures_MinimalExample/results_lm1-1.svg" alt="plot of chunk results_lm1" width="100%" />
<p class="caption">plot of chunk results_lm1</p>
</div>
<img src="figures_MinimalExample/results_lm1-1.svg" alt="" width="100%" style="display: block; margin: auto;" />

The function [`traceplot()`](https://nerler.github.io/JointAI/reference/traceplot.html)
produces a plot of the sampled values across
Expand Down Expand Up @@ -105,22 +102,22 @@ summary(lm1)
#>
#> Posterior summary:
#> Mean SD 2.5% 97.5% tail-prob. GR-crit MCE/SD
#> (Intercept) 59.667 22.5323 13.8296 104.999 0.00800 1.00 0.0263
#> genderfemale -3.088 2.2801 -7.3085 1.497 0.18533 1.00 0.0258
#> age 0.367 0.0730 0.2256 0.512 0.00000 1.01 0.0262
#> raceOther Hispanic 0.857 5.1357 -8.8799 10.758 0.88933 1.00 0.0258
#> raceNon-Hispanic White -1.286 3.0295 -6.9351 4.658 0.66267 1.00 0.0258
#> raceNon-Hispanic Black 9.061 3.4765 2.3074 15.679 0.01067 1.01 0.0258
#> raceother 3.919 3.4728 -2.6699 11.094 0.25467 1.00 0.0258
#> WC 0.243 0.0811 0.0835 0.401 0.00267 1.00 0.0258
#> alc>=1 7.249 2.2744 2.5975 11.650 0.00000 1.00 0.0325
#> educhigh -3.392 2.2355 -8.0464 0.830 0.10400 1.01 0.0267
#> albu 5.390 4.1032 -2.9169 13.239 0.18133 1.01 0.0260
#> bili -5.456 4.8697 -14.3350 4.293 0.25467 1.01 0.0285
#> (Intercept) 60.879 22.8704 19.1247 106.227 0.0040 1.00 0.0271
#> genderfemale -3.177 2.2549 -7.6423 1.099 0.1640 1.00 0.0258
#> age 0.364 0.0708 0.2196 0.505 0.0000 1.01 0.0258
#> raceOther Hispanic 0.518 5.0580 -9.2403 10.675 0.9373 1.00 0.0258
#> raceNon-Hispanic White -1.451 3.0937 -7.2049 4.686 0.6227 1.00 0.0276
#> raceNon-Hispanic Black 9.047 3.6389 2.0441 16.042 0.0147 1.00 0.0267
#> raceother 3.686 3.4550 -3.0852 10.725 0.2653 1.00 0.0268
#> WC 0.236 0.0822 0.0754 0.392 0.0040 1.00 0.0258
#> alc>=1 7.272 2.3870 2.5418 11.763 0.0040 1.03 0.0284
#> educhigh -3.397 2.1457 -7.3896 0.869 0.1120 1.00 0.0258
#> albu 5.320 4.0590 -2.7810 12.922 0.1987 1.00 0.0291
#> bili -5.516 4.9060 -15.2486 4.592 0.2413 1.01 0.0274
#>
#> Posterior summary of residual std. deviation:
#> Mean SD 2.5% 97.5% GR-crit MCE/SD
#> sigma_SBP 13.2 0.716 11.9 14.7 1 0.0281
#> sigma_SBP 13.2 0.738 11.9 14.7 1.02 0.0282
#>
#>
#> MCMC settings:
Expand Down Expand Up @@ -155,10 +152,7 @@ $$2\times\min\left\{Pr(\theta > 0), Pr(\theta < 0)\right\}$$

In the following graphics, the shaded areas represent the minimum of
$Pr(\theta > 0)$ and $Pr(\theta < 0)$:
<div class="figure" style="text-align: center">
<img src="figures_MinimalExample/tailprob-1.svg" alt="plot of chunk tailprob" width="100%" />
<p class="caption">plot of chunk tailprob</p>
</div>
<img src="figures_MinimalExample/tailprob-1.svg" alt="" width="100%" style="display: block; margin: auto;" />

#### Gelman-Rubin criterion
The Gelman-Rubin^[Gelman, A and Rubin, DB (1992) Inference from iterative
Expand Down Expand Up @@ -206,10 +200,7 @@ The posterior distributions can be visualized using the function `densplot()`:
densplot(lm1)
```

<div class="figure" style="text-align: center">
<img src="figures_MinimalExample/densplot-1.svg" alt="plot of chunk densplot" width="100%" />
<p class="caption">plot of chunk densplot</p>
</div>
<img src="figures_MinimalExample/densplot-1.svg" alt="" width="100%" style="display: block; margin: auto;" />

By default, `densplot()` plots the empirical distribution of each of the chains
separately. When `joined = TRUE` the distributions of the combined chains
Expand Down
2 changes: 2 additions & 0 deletions vignettes/MinimalExample.Rmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ knitr::opts_chunk$set(
fig.width = 7,
fig.align = 'center',
out.width = '100%',
fig.cap = "",
fig.scap = "",
dev = "svglite",
fig.path = "figures_MinimalExample/"
)
Expand Down
10 changes: 6 additions & 4 deletions vignettes/ModelSpecification.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Model Specification"
date: January 03, 2026
date: February 22, 2026
output:
rmarkdown::html_vignette:
toc: true
Expand Down Expand Up @@ -685,14 +685,16 @@ distributions they are ordered so that longitudinal (level-1) variables may have
baseline (level-2) variables in their linear predictors but not vice versa.

For example:
$$\begin{align}

\begin{align}
p(y, x, b, \theta) = & p(y \mid x_1, ..., x_4, b_y, \theta_y) && \text{analysis model}\\
& p(x_1\mid \theta_{x1}) && \text{model for a complete baseline covariate}\\
& p(x_2\mid x_1, \theta_{x2}) && \text{model for an incomplete baseline covariate}\\
& p(x_3\mid x_1, x_2, b_{x3}, \theta_{x3}) && \text{model for a complete longitudinal covariate}\\
& p(x_4\mid x_1, x_2, x_3, b_{x4}, \theta_{x4}) && \text{model for an incomplete longitudinal covariate}\\
& p(b_y|\theta_b) p(b_{x3}|\theta_b) p(b_{x4}|\theta_b) && \text{models for the random effects}\\
& p(\theta_y) p(\theta_{x1}) \ldots p(\theta_{x4}) p(\theta_b) && \text{prior distributions}\end{align}$$
& p(\theta_y) p(\theta_{x1}) \ldots p(\theta_{x4}) p(\theta_b) && \text{prior distributions}
\end{align}

Since the parameter vectors $\theta_{x1}$, $\theta_{x2}$, ... are assumed to be
a priori independent, and furthermore $x_1$ is completely observed and modelled
Expand Down Expand Up @@ -1125,7 +1127,7 @@ refs_mod10 <- set_refcat(NHANES, formula = formula(mod10b))
#> How do you want to specify the reference categories?
#>
#> 1: Use the first category for each variable.
#> 2: Use the last category for each variable.
#> 2: Use the last category for each variabe.
#> 3: Use the largest category for each variable.
#> 4: Specify the reference categories individually.
```
Expand Down
Loading
Loading