# causal-lda/trialemulation architecture

> Architecture diagram of the GitHub repository causal-lda/trialemulation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/causal-lda/trialemulation
- Repository: https://github.com/causal-lda/trialemulation

## Overview

TrialEmulation is an R package for conducting a sequence of target-trial analyses. The likely workflow is to prepare and extend observational data into trial-period records, fit outcome and weighting models, then estimate and predict trial outcomes. The tree also shows censoring, robust calculations, and CSV/DuckDB storage capabilities. Only the README and C++ sources were provided as excerpts; the R-level stages and their connections are inferred from filenames and are therefore shown as unverified.

Main entry points: [`R/te_outcome_model.R`](https://github.com/causal-lda/trialemulation/blob/main/R/te_outcome_model.R) (Outcome models), [`R/expand_trials.R`](https://github.com/causal-lda/trialemulation/blob/main/R/expand_trials.R) (Trial expansion), [`R/calculate_weights.R`](https://github.com/causal-lda/trialemulation/blob/main/R/calculate_weights.R) (Weight calculation), [`R/modelling.R`](https://github.com/causal-lda/trialemulation/blob/main/R/modelling.R) (Model fitting)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Trial data"]
  node_sequence["Trial sequence<br/>[trial_sequence.R]"]
  node_preparation["Data preparation<br/>[data_preparation.R]"]
  node_manipulation["Data manipulation"]
  node_extension["Data extension<br/>[data_extension.R]"]
  node_expansion["Trial expansion<br/>[expand_trials.R]"]
  node_censoring["Artificial censoring<br/>[code.cpp]"]
end

subgraph group_analysis["Analysis"]
  node_weights["Weight calculation"]
  node_sampling["Case-control sampling<br/>[sampling.R]"]
  node_robust["Robust calculations<br/>[robust.R]"]
  node_prediction["Predictions<br/>[predict.R]"]
end

subgraph group_models["Models and weights"]
  node_fitting["Model fitting<br/>[modelling.R]"]
  node_outcome["Outcome models<br/>[te_outcome_model.R]"]
  node_weight_models["Weight models<br/>[te_weights.R]"]
  node_parsnip["Parsnip integration<br/>[te_parsnip.R]"]
  node_glm["GLM logit"]
end

subgraph group_storage["Data storage"]
  node_datastore["Data store<br/>[te_datastore.R]"]
  node_csv["CSV store<br/>[te_datastore_csv.R]"]
  node_duckdb["DuckDB store"]
end

node_analyst(("Analyst"))

node_analyst -.->|"defines trials"| node_sequence
node_sequence -.->|"sets up data"| node_preparation
node_preparation -.->|"prepares records"| node_manipulation
node_manipulation -.->|"extends data"| node_extension
node_extension -.->|"expands trials"| node_expansion
node_expansion -.->|"applies censoring"| node_censoring
node_expansion -.->|"supports weighting"| node_weights
node_weights -.->|"fits weights"| node_weight_models
node_fitting -.->|"fits outcomes"| node_outcome
node_fitting -.->|"fits weights"| node_weight_models
node_parsnip -.->|"integrates models"| node_outcome
node_glm -.->|"provides model"| node_outcome
node_outcome -.->|"supports prediction"| node_prediction
node_weights -.->|"informs sampling"| node_sampling
node_prediction -.->|"supports inference"| node_robust
node_datastore -.->|"uses backend"| node_csv
node_datastore -.->|"uses backend"| node_duckdb

click node_sequence "https://github.com/causal-lda/trialemulation/blob/main/R/trial_sequence.R"
click node_preparation "https://github.com/causal-lda/trialemulation/blob/main/R/data_preparation.R"
click node_manipulation "https://github.com/causal-lda/trialemulation/blob/main/R/data_manipulation.R"
click node_extension "https://github.com/causal-lda/trialemulation/blob/main/R/data_extension.R"
click node_expansion "https://github.com/causal-lda/trialemulation/blob/main/R/expand_trials.R"
click node_censoring "https://github.com/causal-lda/trialemulation/blob/main/src/code.cpp"
click node_weights "https://github.com/causal-lda/trialemulation/blob/main/R/calculate_weights.R"
click node_sampling "https://github.com/causal-lda/trialemulation/blob/main/R/sampling.R"
click node_robust "https://github.com/causal-lda/trialemulation/blob/main/R/robust.R"
click node_fitting "https://github.com/causal-lda/trialemulation/blob/main/R/modelling.R"
click node_outcome "https://github.com/causal-lda/trialemulation/blob/main/R/te_outcome_model.R"
click node_weight_models "https://github.com/causal-lda/trialemulation/blob/main/R/te_weights.R"
click node_parsnip "https://github.com/causal-lda/trialemulation/blob/main/R/te_parsnip.R"
click node_glm "https://github.com/causal-lda/trialemulation/blob/main/R/te_stats_glm_logit.R"
click node_prediction "https://github.com/causal-lda/trialemulation/blob/main/R/predict.R"
click node_datastore "https://github.com/causal-lda/trialemulation/blob/main/R/te_datastore.R"
click node_csv "https://github.com/causal-lda/trialemulation/blob/main/R/te_datastore_csv.R"
click node_duckdb "https://github.com/causal-lda/trialemulation/blob/main/R/te_datastore_duckdb.R"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sequence,node_preparation,node_manipulation,node_extension,node_expansion,node_censoring toneBlue
class node_weights,node_sampling,node_robust,node_prediction toneAmber
class node_fitting,node_outcome,node_weight_models,node_parsnip,node_glm toneMint
class node_datastore,node_csv,node_duckdb toneRose
class node_analyst toneIndigo
```

## Components

### Trial data

- [Trial sequence](https://github.com/causal-lda/trialemulation/blob/main/R/trial_sequence.R): `R/trial_sequence.R`
- [Data preparation](https://github.com/causal-lda/trialemulation/blob/main/R/data_preparation.R): `R/data_preparation.R`
- [Data manipulation](https://github.com/causal-lda/trialemulation/blob/main/R/data_manipulation.R): `R/data_manipulation.R`
- [Data extension](https://github.com/causal-lda/trialemulation/blob/main/R/data_extension.R): `R/data_extension.R`
- [Trial expansion](https://github.com/causal-lda/trialemulation/blob/main/R/expand_trials.R): `R/expand_trials.R`
- [Artificial censoring](https://github.com/causal-lda/trialemulation/blob/main/src/code.cpp): `src/code.cpp`

### Analysis

- [Weight calculation](https://github.com/causal-lda/trialemulation/blob/main/R/calculate_weights.R): `R/calculate_weights.R`
- [Case-control sampling](https://github.com/causal-lda/trialemulation/blob/main/R/sampling.R): `R/sampling.R`
- [Robust calculations](https://github.com/causal-lda/trialemulation/blob/main/R/robust.R): `R/robust.R`
- [Predictions](https://github.com/causal-lda/trialemulation/blob/main/R/predict.R): `R/predict.R`

### Models and weights

- [Model fitting](https://github.com/causal-lda/trialemulation/blob/main/R/modelling.R): `R/modelling.R`
- [Outcome models](https://github.com/causal-lda/trialemulation/blob/main/R/te_outcome_model.R): `R/te_outcome_model.R`
- [Weight models](https://github.com/causal-lda/trialemulation/blob/main/R/te_weights.R): `R/te_weights.R`
- [Parsnip integration](https://github.com/causal-lda/trialemulation/blob/main/R/te_parsnip.R): `R/te_parsnip.R`
- [GLM logit](https://github.com/causal-lda/trialemulation/blob/main/R/te_stats_glm_logit.R): `R/te_stats_glm_logit.R`

### Data storage

- [Data store](https://github.com/causal-lda/trialemulation/blob/main/R/te_datastore.R): `R/te_datastore.R`
- [CSV store](https://github.com/causal-lda/trialemulation/blob/main/R/te_datastore_csv.R): `R/te_datastore_csv.R`
- [DuckDB store](https://github.com/causal-lda/trialemulation/blob/main/R/te_datastore_duckdb.R): `R/te_datastore_duckdb.R`

### Other components

- **Analyst**

## Connections

- Analyst → Trial sequence: defines trials
- Trial sequence → Data preparation: sets up data
- Data preparation → Data manipulation: prepares records
- Data manipulation → Data extension: extends data
- Data extension → Trial expansion: expands trials
- Trial expansion → Artificial censoring: applies censoring
- Trial expansion → Weight calculation: supports weighting
- Weight calculation → Weight models: fits weights
- Model fitting → Outcome models: fits outcomes
- Model fitting → Weight models: fits weights
- Parsnip integration → Outcome models: integrates models
- GLM logit → Outcome models: provides model
- Outcome models → Predictions: supports prediction
- Weight calculation → Case-control sampling: informs sampling
- Predictions → Robust calculations: supports inference
- Data store → CSV store: uses backend
- Data store → DuckDB store: uses backend
