# paul-buerkner/brms architecture

> Architecture diagram of the GitHub repository paul-buerkner/brms, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/paul-buerkner/brms
- Repository: https://github.com/paul-buerkner/brms

## Overview

##### Main Components Architecture

a) Core Components:
- Formula Interface Layer
- Model Building Layer
- Stan Code Generation Layer
- Posterior Analysis Layer
- Visualization Layer

b) External Dependencies:
- Stan (C++ backend)
- R Statistical Environment
- Supporting R packages (rstan, Rcpp, bayesplot, ggplot2, etc.)

##### Component Relationships

**User Input (R Formula) → Formula Processing → Model Building → Stan Code Generation → Model Compilation → MCMC Sampling → Post-Processing → Output/Visualization**

##### Detailed Component Breakdown

a) Formula Interface Layer:
- brmsformula component (R/brmsformula.R)
- Family specifications (R/families.R)
- Prior specifications (R/priors.R)

b) Model Building Layer:
- Model terms processing (R/brmsterms.R)
- Data preparation (R/data-*.R files)
- Parameter structuring (R/standata.R)

c) Stan Code Generation Layer:
- Stan code templates (inst/chunks/)
- Code generation utilities (R/stancode.R)
- Model compilation interface

d) Posterior Analysis Layer:
- MCMC diagnostics (R/diagnostics.R)
- Model comparison tools (R/loo.R)
- Prediction utilities (R/posterior_*.R files)

e) Visualization Layer:
- Plot methods (R/plot.R)
- Conditional effects (R/conditional_effects.R)
- Posterior checks (R/pp_check.R)

##### Design Elements

Use these visual elements:

##### Key Workflows to Highlight

a) Model Fitting Pipeline:
**brm() → Formula Processing → Stan Code Generation → Model Compilation → MCMC Sampling**

b) Post-processing Pipeline:
**fitted model → posterior analysis → diagnostics → visualization**

##### Integration Points

Clear integration with:
- Stan compiler and runtime
- R package ecosystem
- External visualization tools
- Data input/output systems

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Legend
        L1[R Component]:::rcomp
        L2[Stan Component]:::stan
        L3[Data Flow]:::data
        L4[External Dependency]:::external
        L5["/Optional Flow/"]:::optional
    end

    subgraph "Formula Interface Layer"
        BF[brmsformula]:::rcomp
        FM[Families]:::rcomp
        PR[Priors]:::rcomp
    end

    subgraph "Model Building Layer"
        BT[brmsterms]:::rcomp
        DP[Data Preparation]:::rcomp
        PS[Parameter Structure]:::rcomp
    end

    subgraph "Stan Code Generation Layer"
        ST[Stan Templates]:::stan
        CG[Code Generation]:::stan
        MC[Model Compilation]:::stan
    end

    subgraph "Posterior Analysis Layer"
        DG[MCMC Diagnostics]:::rcomp
        LC[LOO Comparison]:::rcomp
        PP[Posterior Processing]:::rcomp
    end

    subgraph "Visualization Layer"
        PL[Plot Methods]:::rcomp
        CE[Conditional Effects]:::rcomp
        PC[Posterior Checks]:::rcomp
        GT[GGPlot Themes]:::rcomp
    end

    subgraph "External Dependencies"
        STAN[Stan C++]:::external
        RENV[R Environment]:::external
        RPKG[R Packages]:::external
    end

    %% Main workflow
    BF --> BT
    FM --> BT
    PR --> BT
    BT --> DP
    DP --> PS
    PS --> CG
    ST --> CG
    CG --> MC
    MC --> PP
    PP --> DG
    PP --> LC
    PP --> PL
    PP --> CE
    PP --> PC

    %% External dependencies
    STAN -.-> MC
    RENV -.-> BF
    RPKG -.-> PL

    %% Component mappings
    click BF "https://github.com/paul-buerkner/brms/blob/master/R/brmsformula.R"
    click FM "https://github.com/paul-buerkner/brms/blob/master/R/families.R"
    click PR "https://github.com/paul-buerkner/brms/blob/master/R/priors.R"
    click BT "https://github.com/paul-buerkner/brms/blob/master/R/brmsterms.R"
    click DP "https://github.com/paul-buerkner/brms/blob/master/R/data-predictor.R"
    click PS "https://github.com/paul-buerkner/brms/blob/master/R/standata.R"
    click ST "https://github.com/paul-buerkner/brms/tree/master/inst/chunks/"
    click CG "https://github.com/paul-buerkner/brms/blob/master/R/stancode.R"
    click DG "https://github.com/paul-buerkner/brms/blob/master/R/diagnostics.R"
    click LC "https://github.com/paul-buerkner/brms/blob/master/R/loo.R"
    click PP "https://github.com/paul-buerkner/brms/blob/master/R/posterior_predict.R"
    click PL "https://github.com/paul-buerkner/brms/blob/master/R/plot.R"
    click CE "https://github.com/paul-buerkner/brms/blob/master/R/conditional_effects.R"
    click PC "https://github.com/paul-buerkner/brms/blob/master/R/pp_check.R"
    click GT "https://github.com/paul-buerkner/brms/blob/master/R/ggplot-themes.R"

    classDef rcomp fill:#a8d1f0,stroke:#333,stroke-width:2px
    classDef stan fill:#ffb366,stroke:#333,stroke-width:2px
    classDef data fill:#90EE90,stroke:#333,stroke-width:2px
    classDef external fill:#d3d3d3,stroke:#333,stroke-width:2px
    classDef optional fill:none,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5
```
