knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 8, fig.height = 6 ) library(brms.mmrm) library(dplyr) library(fst) library(ggplot2) library(tibble) library(tidyr)
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 @Modrak2024 and the SBC
R package [@sbc]. This particular SBC checking study uses the targets
pipeline in the viggnettes/sbc/
subdirectory of the brms.mmrm
package source code.
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.
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.
The subgroup scenario distinguishes itself from the others by the presence of a subgroup factor. Assumptions:
Model formula:
subgroup()$formula
The prior was randomly generated and used for both simulation and analysis:
setup_prior(subgroup) |> select(prior, class, coef, dpar) |> as.data.frame()
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()
This scenario uses unstructured correlation and does not use a subgroup variable. Assumptions:
Model formula:
unstructured()$formula
The prior was randomly generated and used for both simulation and analysis:
setup_prior(unstructured) |> select(prior, class, coef, dpar) |> as.data.frame()
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()
This scenario uses an autoregressive moving average (ARMA) model with autoregressive order 1 and moving average order 1. Assumptions:
Model formula:
autoregressive_moving_average()$formula
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()
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()
This scenario is the same as above, but the correlation structure is autoregressive with order 2. Model formula:
autoregressive()$formula
The prior was randomly generated and used for both simulation and analysis:
setup_prior(autoregressive) |> select(prior, class, coef, dpar) |> as.data.frame()
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()
This scenario is the same as above, but it uses a moving average correlation structure with order 2. Model formula:
moving_average()$formula
The prior was randomly generated and used for both simulation and analysis:
setup_prior(moving_average) |> select(prior, class, coef, dpar) |> as.data.frame()
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()
This scenario is the same as above, but it uses a compound symmetry correlation structure. Model formula:
compound_symmetry()$formula
The prior was randomly generated and used for both simulation and analysis:
setup_prior(compound_symmetry) |> select(prior, class, coef, dpar) |> as.data.frame()
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()
This scenario is the same as above, but it uses a diagonal correlation structure (independent time points within patients). Model formula:
diagonal()$formula
The prior was randomly generated and used for both simulation and analysis:
setup_prior(diagonal) |> select(prior, class, coef, dpar) |> as.data.frame()
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()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.