The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Simulation-based calibration checking

1 About

This vignette shows the results of a simulation-based calibration (SBC) checking study to validate the implementation of the models in brms.mmrm. SBC checking tests the ability of a Bayesian model to recapture the parameters used to simulate prior predictive data. For details on SBC checking, please read Modrák et al. (2024) and the SBC R package (Kim et al. 2022). This particular SBC checking study uses the targets pipeline in the viggnettes/sbc/ subdirectory of the brms.mmrm package source code.

2 Conclusion

From the results below, the SBC rank statistics are approximately uniformly distributed. In other words, the posterior distribution from the brms/Stan MMRM modeling code matches the prior from which the datasets were simulated. This is evidence that both the subgroup and non-subgroup models in brms.mmrm are implemented correctly.

3 Setup

To show the SBC checking results in this vignette, we first load code from the SBC checking study, and we use custom functions to read and plot rank statistics:

library(dplyr)
library(ggplot2)
library(tibble)
library(tidyr)

source("sbc/R/prior.R")
source("sbc/R/response.R")
source("sbc/R/scenarios.R")

read_ranks <- function(path) {
  fst::read_fst(path) |>
    tibble::as_tibble() |>
    pivot_longer(
      cols = everything(),
      names_to = "parameter",
      values_to = "rank"
    )
}

plot_ranks <- function(ranks) {
  ggplot(ranks) +
    geom_histogram(
      aes(x = rank),
      breaks = seq(from = 0, to = max(ranks$rank), length.out = 10)
    ) +
    facet_wrap(~parameter)
}

Each section below is its own SBC checking study based on a given modeling scenario. Each scenario shows results from 1000 independent simulations from the prior.

4 Subgroup scenario

The subgroup scenario distinguishes itself from the others by the presence of a subgroup factor. Assumptions:

Model formula:

#> response ~ group + group:subgroup + group:subgroup:time + group:time + subgroup + subgroup:time + time + continuous1 + continuous2 + balanced + unbalanced + unstr(time = time, gr = patient) 
#> sigma ~ 0 + time

The prior was randomly generated and used for both simulation and analysis:

setup_prior(subgroup) |>
  select(prior, class, coef, dpar) |>
  as.data.frame()
#>                      prior     class                                       coef
#> 1  normal(-0.1975, 2.1614) Intercept                                           
#> 3  normal(-0.2351, 0.7914)         b                             balancedlevel2
#> 4   normal(0.1529, 2.4389)         b                             balancedlevel3
#> 5   normal(-0.154, 1.1574)         b                                continuous1
#> 6    normal(0.0716, 1.141)         b                                continuous2
#> 7  normal(-0.1079, 2.0095)         b                               groupgroup_2
#> 8  normal(-0.0741, 0.7293)         b            groupgroup_2:subgroupsubgroup_2
#> 9  normal(-0.1753, 2.3156)         b groupgroup_2:subgroupsubgroup_2:timetime_2
#> 10  normal(0.1037, 2.9871)         b groupgroup_2:subgroupsubgroup_2:timetime_3
#> 11   normal(0.2096, 1.618)         b                    groupgroup_2:timetime_2
#> 12 normal(-0.2139, 1.1255)         b                    groupgroup_2:timetime_3
#> 13  normal(-0.1425, 0.951)         b                         subgroupsubgroup_2
#> 14  normal(0.1951, 0.7995)         b              subgroupsubgroup_2:timetime_2
#> 15 normal(-0.1195, 1.1148)         b              subgroupsubgroup_2:timetime_3
#> 16  normal(0.1383, 0.4518)         b                                 timetime_2
#> 17  normal(0.1807, 0.3512)         b                                 timetime_3
#> 18 normal(-0.1922, 1.2422)         b                           unbalancedlevel2
#> 19   normal(0.122, 0.4765)         b                           unbalancedlevel3
#> 20             lkj(1.0856)   cortime                                           
#> 22  normal(0.2483, 2.2851)         b                                 timetime_1
#> 23  normal(0.1859, 1.8211)         b                                 timetime_2
#> 24  normal(0.1446, 1.1248)         b                                 timetime_3
#>     dpar
#> 1       
#> 3       
#> 4       
#> 5       
#> 6       
#> 7       
#> 8       
#> 9       
#> 10      
#> 11      
#> 12      
#> 13      
#> 14      
#> 15      
#> 16      
#> 17      
#> 18      
#> 19      
#> 20      
#> 22 sigma
#> 23 sigma
#> 24 sigma

The following histograms show the SBC rank statistics which compare the prior parameter draws draws to the posterior draws. If the data simulation code and modeling code are both correct and consistent, then the rank statistics should be approximately uniformly distributed.

ranks_subgroup <- read_ranks("sbc/results/subgroup.fst")

Fixed effect parameter ranks:

ranks_subgroup |>
  filter(grepl("^b_", parameter)) |>
  filter(!grepl("^b_sigma", parameter)) |>
  plot_ranks()

Standard deviation parameter ranks:

ranks_subgroup |>
  filter(grepl("b_sigma", parameter)) |>
  plot_ranks()

Correlation parameter ranks:

ranks_subgroup |>
  filter(grepl("cortime_", parameter)) |>
  plot_ranks()

5 Unstructured scenario

This scenario uses unstructured correlation and does not use a subgroup variable. Assumptions:

Model formula:

#> response ~ 0 + group + time + unstr(time = time, gr = patient) 
#> sigma ~ 0 + time

The prior was randomly generated and used for both simulation and analysis:

setup_prior(unstructured) |>
  select(prior, class, coef, dpar) |>
  as.data.frame()
#>                      prior   class         coef  dpar
#> 2   normal(0.0648, 1.5827)       b groupgroup_1      
#> 3   normal(0.1181, 1.2757)       b groupgroup_2      
#> 4  normal(-0.1858, 0.3737)       b groupgroup_3      
#> 5   normal(0.0501, 1.2507)       b   timetime_2      
#> 6  normal(-0.2253, 1.3044)       b   timetime_3      
#> 7   normal(0.1698, 0.7262)       b   timetime_4      
#> 8              lkj(1.4206) cortime                   
#> 10  normal(0.0736, 0.8509)       b   timetime_1 sigma
#> 11  normal(-0.247, 1.6962)       b   timetime_2 sigma
#> 12  normal(0.1487, 1.8811)       b   timetime_3 sigma
#> 13 normal(-0.1065, 0.7783)       b   timetime_4 sigma

SBC checking rank statistics:

ranks_unstructured <- read_ranks("sbc/results/unstructured.fst")

Fixed effect parameter ranks:

ranks_unstructured |>
  filter(grepl("^b_", parameter)) |>
  filter(!grepl("^b_sigma", parameter)) |>
  plot_ranks()

Log-scale standard deviation parameter ranks:

ranks_unstructured |>
  filter(grepl("b_sigma", parameter)) |>
  plot_ranks()

Correlation parameter ranks:

ranks_unstructured |>
  filter(grepl("cortime_", parameter)) |>
  plot_ranks()

6 Autoregressive moving average scenario

This scenario uses an autoregressive moving average (ARMA) model with autoregressive order 1 and moving average order 1. Assumptions:

Model formula:

#> response ~ 0 + group + time + arma(time = time, gr = patient, p = 1L, q = 1L, cov = FALSE) 
#> sigma ~ 0 + time

The prior was randomly generated and used for both simulation and analysis:

setup_prior(autoregressive_moving_average) |>
  select(prior, class, coef, dpar) |>
  as.data.frame()
#>                      prior class         coef  dpar
#> 1        uniform(0.1, 0.9)    ar                   
#> 3    normal(5e-04, 2.8963)     b groupgroup_1      
#> 4  normal(-0.1901, 2.9522)     b groupgroup_2      
#> 5    normal(0.2355, 2.758)     b   timetime_2      
#> 6  normal(-0.2381, 2.9875)     b   timetime_3      
#> 7        uniform(0.1, 0.9)    ma                   
#> 9    normal(0.1033, 2.307)     b   timetime_1 sigma
#> 10 normal(-0.1708, 2.9678)     b   timetime_2 sigma
#> 11 normal(-0.1301, 2.2895)     b   timetime_3 sigma

SBC checking rank statistics:

ranks_autoregressive_moving_average <- read_ranks(
  "sbc/results/autoregressive_moving_average.fst"
)

Fixed effect parameter ranks:

ranks_autoregressive_moving_average |>
  filter(grepl("^b_", parameter)) |>
  filter(!grepl("^b_sigma", parameter)) |>
  plot_ranks()

Log-scale standard deviation parameter ranks:

ranks_autoregressive_moving_average |>
  filter(grepl("b_sigma", parameter)) |>
  plot_ranks()

Correlation parameter ranks:

ranks_autoregressive_moving_average |>
  filter(parameter %in% c("ar[1]", "ma[1]")) |>
  plot_ranks()

7 Autoregressive scenario

This scenario is the same as above, but the correlation structure is autoregressive with order 2. Model formula:

#> response ~ 0 + group + time + ar(time = time, gr = patient, p = 2L, cov = FALSE) 
#> sigma ~ 0 + time

The prior was randomly generated and used for both simulation and analysis:

setup_prior(autoregressive) |>
  select(prior, class, coef, dpar) |>
  as.data.frame()
#>                      prior class         coef  dpar
#> 1        uniform(0.1, 0.9)    ar                   
#> 3  normal(-0.0201, 0.7858)     b groupgroup_1      
#> 4  normal(-0.1963, 2.4252)     b groupgroup_2      
#> 5   normal(-0.212, 1.0224)     b   timetime_2      
#> 6   normal(-0.169, 2.9933)     b   timetime_3      
#> 8   normal(0.0387, 2.9823)     b   timetime_1 sigma
#> 9   normal(-0.174, 1.3437)     b   timetime_2 sigma
#> 10 normal(-0.1711, 1.9585)     b   timetime_3 sigma

SBC checking rank statistics:

ranks_autoregressive <- read_ranks("sbc/results/autoregressive.fst")

Fixed effect parameter ranks:

ranks_autoregressive |>
  filter(grepl("^b_", parameter)) |>
  filter(!grepl("^b_sigma", parameter)) |>
  plot_ranks()

Log-scale standard deviation parameter ranks:

ranks_autoregressive |>
  filter(grepl("b_sigma", parameter)) |>
  plot_ranks()

Correlation parameter ranks:

ranks_autoregressive |>
  filter(parameter %in% c("ar[1]", "ar[2]")) |>
  plot_ranks()

8 Moving average scenario

This scenario is the same as above, but it uses a moving average correlation structure with order 2. Model formula:

#> response ~ 0 + group + time + ma(time = time, gr = patient, q = 2L, cov = FALSE) 
#> sigma ~ 0 + time

The prior was randomly generated and used for both simulation and analysis:

setup_prior(moving_average) |>
  select(prior, class, coef, dpar) |>
  as.data.frame()
#>                      prior class         coef  dpar
#> 2   normal(0.0633, 2.6664)     b groupgroup_1      
#> 3   normal(0.1451, 2.8391)     b groupgroup_2      
#> 4  normal(-0.1752, 0.9919)     b   timetime_2      
#> 5   normal(-0.0413, 1.079)     b   timetime_3      
#> 6        uniform(0.1, 0.9)    ma                   
#> 8  normal(-0.1921, 1.1782)     b   timetime_1 sigma
#> 9  normal(-0.1504, 1.2608)     b   timetime_2 sigma
#> 10  normal(-0.0244, 1.608)     b   timetime_3 sigma

SBC checking rank statistics:

ranks_moving_average <- read_ranks("sbc/results/moving_average.fst")

Fixed effect parameter ranks:

ranks_moving_average |>
  filter(grepl("^b_", parameter)) |>
  filter(!grepl("^b_sigma", parameter)) |>
  plot_ranks()

Log-scale standard deviation parameter ranks:

ranks_moving_average |>
  filter(grepl("b_sigma", parameter)) |>
  plot_ranks()

Correlation parameter ranks:

ranks_moving_average |>
  filter(parameter %in% c("ma[1]", "ma[2]")) |>
  plot_ranks()

9 Compound symmetry scenario

This scenario is the same as above, but it uses a compound symmetry correlation structure. Model formula:

#> response ~ 0 + group + time + cosy(time = time, gr = patient) 
#> sigma ~ 0 + time

The prior was randomly generated and used for both simulation and analysis:

setup_prior(compound_symmetry) |>
  select(prior, class, coef, dpar) |>
  as.data.frame()
#>                      prior class         coef  dpar
#> 2  normal(-0.0116, 2.7318)     b groupgroup_1      
#> 3     normal(0.159, 0.608)     b groupgroup_2      
#> 4  normal(-0.1599, 0.9612)     b   timetime_2      
#> 5   normal(-0.248, 0.7898)     b   timetime_3      
#> 6        uniform(0.1, 0.9)  cosy                   
#> 8   normal(0.0669, 0.5404)     b   timetime_1 sigma
#> 9    normal(-0.22, 0.4948)     b   timetime_2 sigma
#> 10 normal(-0.1095, 2.0964)     b   timetime_3 sigma

SBC checking rank statistics:

ranks_compound_symmetry <- read_ranks("sbc/results/compound_symmetry.fst")

Fixed effect parameter ranks:

ranks_compound_symmetry |>
  filter(grepl("^b_", parameter)) |>
  filter(!grepl("^b_sigma", parameter)) |>
  plot_ranks()

Log-scale standard deviation parameter ranks:

ranks_compound_symmetry |>
  filter(grepl("b_sigma", parameter)) |>
  plot_ranks()

Correlation parameter ranks:

ranks_compound_symmetry |>
  filter(parameter == "cosy") |>
  plot_ranks()

10 Diagonal scenario

This scenario is the same as above, but it uses a diagonal correlation structure (independent time points within patients). Model formula:

#> response ~ 0 + group + time 
#> sigma ~ group + group:time + time

The prior was randomly generated and used for both simulation and analysis:

setup_prior(diagonal) |>
  select(prior, class, coef, dpar) |>
  as.data.frame()
#>                      prior     class                    coef  dpar
#> 2   normal(0.0541, 0.7849)         b            groupgroup_1      
#> 3  normal(-0.1169, 2.6509)         b            groupgroup_2      
#> 4   normal(0.1356, 2.8488)         b              timetime_2      
#> 5    normal(0.174, 0.4224)         b              timetime_3      
#> 6  normal(-0.0944, 0.6528) Intercept                         sigma
#> 8   normal(0.2071, 0.8028)         b            groupgroup_2 sigma
#> 9  normal(-0.1064, 1.1558)         b groupgroup_2:timetime_2 sigma
#> 10 normal(-0.0667, 0.3551)         b groupgroup_2:timetime_3 sigma
#> 11 normal(-0.0725, 0.6783)         b              timetime_2 sigma
#> 12 normal(-0.1543, 2.8636)         b              timetime_3 sigma

SBC checking rank statistics:

ranks_diagonal <- read_ranks("sbc/results/diagonal.fst")

Fixed effect parameter ranks:

ranks_diagonal |>
  filter(grepl("^b_", parameter)) |>
  filter(!grepl("^b_sigma", parameter)) |>
  plot_ranks()

Log-scale standard deviation parameter ranks:

ranks_diagonal |>
  filter(grepl("b_sigma", parameter)) |>
  plot_ranks()

References

Kim, S., Moon, H., Modrák, M., and Säilynoja, T. (2022), SBC: Simulation based calibration for rstan/cmdstanr models.
Modrák, M., Moon, A. H., Kim, S., Bürkner, P., Huurre, N., Faltejsková, K., Gelman, A., and Vehtari, A. (2024), Simulation-Based Calibration Checking for Bayesian Computation: The Choice of Test Quantities Shapes Sensitivity,” Bayesian Analysis, forthcoming. https://doi.org/10.1214/23-BA1404.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.