D_bmi_ID[2,2]
```
### Other parameters
The element `other` in `monitor_params` allows to specify one or multiple
additional parameters to be monitored. When `other` is used with more than one
element, `monitor_params` has to be a list.
Here, we monitor the probability to be in the `alc>=1` group for subjects 1
through 3 and the expected value of the distribution of `creat` for the first
subject. (This may not make a lot of sense here but being able to monitor
any node in a JAGS model can be quite useful when investigating convergence
issues.)
``` r
lm4 <- lm_imp(SBP ~ gender + WC + alc + creat, data = NHANES,
monitor_params = list(analysis_main = FALSE,
other = c('p_alc[1:3]', "mu_creat[1]")))
parameters(lm4)
#> outcat varname coef outcome
#> 1 NA NA p_alc[1:3] alc
#> 2 NA NA mu_creat[1] creat
```
## Subsets of Parameters for Plots, Summaries, ...
The functions
[`summary()`](https://nerler.github.io/JointAI/reference/summary.JointAI.html),
[`traceplot()`](https://nerler.github.io/JointAI/reference/traceplot.html),
[`densplot()`](https://nerler.github.io/JointAI/reference/densplot.html),
[`GR_crit()`](https://nerler.github.io/JointAI/reference/GR_crit.html) and
[`MC_error()`](https://nerler.github.io/JointAI/reference/MC_error.html)
all have an argument `subset`. This argument allows us to select a subset of
parameters to be shown in the output.
Especially when not only the parameters of the main
analysis model are followed, but also, for example, imputed values, looking at
a subset may be desirable.
For more details about these functions, see also the vignette
[*After Fitting*](https://nerler.github.io/JointAI/articles/AfterFitting.html).
`subset` follows the same logic as `monitor_params` described above.
By default, only the parameters of the main analysis model are displayed if they
were monitored:
``` r
# Run a model monitoring analysis parameters and imputation parameters
lm5 <- lm_imp(SBP ~ gender + WC + alc + creat, data = NHANES, n.iter = 100,
progress.bar = 'none', monitor_params = c(other_models = TRUE))
# model summary
summary(lm5)
#>
#> Bayesian linear model fitted with JointAI
#>
#> Call:
#> lm_imp(formula = SBP ~ gender + WC + alc + creat, data = NHANES,
#> n.iter = 100, monitor_params = c(other_models = TRUE), progress.bar = "none")
#>
#>
#> Posterior summary:
#> Mean SD 2.5% 97.5% tail-prob. GR-crit MCE/SD
#> (Intercept) 81.096 9.6988 60.971 97.166 0.00000 1.01 0.0577
#> genderfemale 0.676 2.5491 -3.776 5.439 0.81333 1.00 0.0577
#> WC 0.300 0.0699 0.164 0.425 0.00000 1.02 0.0577
#> alc>=1 6.645 2.2974 2.500 11.345 0.00667 1.02 0.0689
#> creat 8.206 8.1859 -7.115 24.301 0.34000 1.07 0.0577
#>
#> Posterior summary of residual std. deviation:
#> Mean SD 2.5% 97.5% GR-crit MCE/SD
#> sigma_SBP 14.4 0.806 13 16.3 1.02 0.0577
#>
#>
#> MCMC settings:
#> Iterations = 101:200
#> Sample size per chain = 100
#> Thinning interval = 1
#> Number of chains = 3
#>
#> Number of observations: 186
# traceplot of the MCMC sample
traceplot(lm5)
```
``` r
# density plot of the MCMC sample
densplot(lm5)
```
``` r
# Gelman-Rubin criterion
GR_crit(lm5)
#> Potential scale reduction factors:
#>
#> Point est. Upper C.I.
#> (Intercept) 1.00 1.01
#> genderfemale 1.00 1.00
#> WC 1.00 1.02
#> alc>=1 1.00 1.02
#> creat 1.03 1.07
#> sigma_SBP 1.00 1.02
#>
#> Multivariate psrf
#>
#> 1.03
# Monte Carlo Error of the MCMC sample
MC_error(lm5)
#> est MCSE SD MCSE/SD
#> (Intercept) 81.10 0.560 9.70 0.058
#> genderfemale 0.68 0.147 2.55 0.058
#> WC 0.30 0.004 0.07 0.058
#> alc>=1 6.64 0.158 2.30 0.069
#> creat 8.21 0.473 8.19 0.058
#> sigma_SBP 14.42 0.047 0.81 0.058
```
When `analysis_main` was not switched on the default behaviour is that all
parameters are displayed:
``` r
# Re-run the model from above, now creating MCMC samples
lm4 <- lm_imp(SBP ~ gender + WC + alc + creat,
data = NHANES, n.iter = 100, progress.bar = 'none',
monitor_params = list(analysis_main = FALSE,
other = c('mu_alc[1:3]', "mu_creat[1]")))
traceplot(lm4, ncol = 4)
```
### Select a subset of the variables to display
To display other parts of the MCMC sample, `subset` needs to be specified:
``` r
# we use lm5 from above
GR_crit(lm5, subset = c(analysis_main = FALSE, other_models = TRUE))
#> Potential scale reduction factors:
#>
#> Point est. Upper C.I.
#> alc: (Intercept) 1.081 1.268
#> alc: genderfemale 1.104 1.327
#> alc: WC 1.015 1.061
#> alc: creat 1.061 1.210
#> creat: (Intercept) 1.015 1.056
#> creat: genderfemale 0.996 0.998
#> creat: WC 1.018 1.060
#> WC: (Intercept) 1.003 1.021
#> WC: genderfemale 1.001 1.013
#> sigma_creat 1.006 1.022
#> sigma_WC 1.007 1.010
#>
#> Multivariate psrf
#>
#> 1.1
```
To select only some of the parameters, they can be specified directly by
name via the `other` element of `subset`:
``` r
summary(lm5, subset = list(other = c('creat', 'alc>=1')))
#>
#> Bayesian linear model fitted with JointAI
#>
#> Call:
#> lm_imp(formula = SBP ~ gender + WC + alc + creat, data = NHANES,
#> n.iter = 100, monitor_params = c(other_models = TRUE), progress.bar = "none")
#>
#>
#> Posterior summary:
#> Mean SD 2.5% 97.5% tail-prob. GR-crit MCE/SD
#> (Intercept) 81.096 9.6988 60.971 97.166 0.00000 1.01 0.0577
#> genderfemale 0.676 2.5491 -3.776 5.439 0.81333 1.00 0.0577
#> WC 0.300 0.0699 0.164 0.425 0.00000 1.02 0.0577
#> alc>=1 6.645 2.2974 2.500 11.345 0.00667 1.02 0.0689
#> creat 8.206 8.1859 -7.115 24.301 0.34000 1.07 0.0577
#>
#> Posterior summary of residual std. deviation:
#> Mean SD 2.5% 97.5% GR-crit MCE/SD
#> sigma_SBP 14.4 0.806 13 16.3 1.02 0.0577
#>
#>
#> MCMC settings:
#> Iterations = 101:200
#> Sample size per chain = 100
#> Thinning interval = 1
#> Number of chains = 3
#>
#> Number of observations: 186
```
Note that the model summary will contain separate parts per sub-model when
regression coefficients from different models are monitored.
This also works when a subset of the imputed values should be displayed:
``` r
# Re-run the model from above, now creating MCMC samples
lm2 <- lm_imp(SBP ~ age + WC + alc + smoke + occup,
data = NHANES, n.iter = 100, progress.bar = 'none',
monitor_params = c(imps = TRUE, analysis_main = FALSE)
)
# select only imputed values for 'WC' (4th column of Wc)
sub3 <- grep('M_lvlone\\[[[:digit:]]+,5\\]', parameters(lm2)$coef, value = TRUE)
sub3
#> [1] "M_lvlone[33,5]" "M_lvlone[150,5]"
traceplot(lm2, subset = list(other = sub3), ncol = 2)
```
### Random subset of subject-specific values
When the number of imputed values is larger, or in order to check convergence
of random effects, it may not be feasible to plot all trace plots.
In that case, a random subset of, for instance, the random effects can be selected:
``` r
lme4 <- lme_imp(bmi ~ age + EDUC, random = ~age | ID,
data = simLong, n.iter = 100, progress.bar = 'none',
monitor_params = c(analysis_main = FALSE, ranef_main = TRUE))
# exract random intercepts
ri <- grep('^b_bmi_ID\\[[[:digit:]]+,1\\]$', colnames(lme4$MCMC[[1]]), value = T)
# extract random slopes
rs <- grep('^b_bmi_ID\\[[[:digit:]]+,2\\]$', colnames(lme4$MCMC[[1]]), value = T)
# plot the chains of 8 randomly selected random intercepts
traceplot(lme4, subset = list(other = sample(ri, size = 8)), ncol = 4)
```
``` r
# plot the chains of 8 randomly selected random slopes
traceplot(lme4, subset = list(other = sample(rs, size = 8)), ncol = 4)
```