# cran/mgcv architecture

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

- Interactive diagram: https://gitdiagram.com/cran/mgcv
- Repository: https://github.com/cran/mgcv

## Overview

mgcv is an R package for generalized additive models: users specify model formulas and data, fit penalized smooth models, then predict, inspect, and visualize results. Its documented capabilities also include large-data fitting, mixed-model and multi-linear-predictor models, specialized families, and optional INLA/JAGS workflows. The README is empty and the supplied source excerpts are partial, so subsystem paths and broad capabilities are grounded in the tree and manual-page names; uncertain internal call links are omitted.

Main entry points: [`R/mgcv.r`](https://github.com/cran/mgcv/blob/master/R/mgcv.r) (GAM interface), [`R/gam.fit3.r`](https://github.com/cran/mgcv/blob/master/R/gam.fit3.r) (GAM fitting), [`R/bam.r`](https://github.com/cran/mgcv/blob/master/R/bam.r) (Large-data fitting), [`R/smooth.r`](https://github.com/cran/mgcv/blob/master/R/smooth.r) (Smooth bases)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_model["Model specification"]
  node_gam_api["GAM interface<br/>[mgcv.r]"]
  node_smooths["Smooth bases<br/>[smooth.r]"]
  node_families["Model families<br/>[misc.r]"]
end

subgraph group_fit["Model fitting"]
  node_fit_core["GAM fitting<br/>[gam.fit3.r]"]
  node_fit_alternate["Alternate fitting<br/>[gam.fit4.r]"]
  node_sp_selection["Smoothing selection<br/>[fast-REML.r]"]
end

subgraph group_extensions["Model extensions"]
  node_bam["Large-data fitting<br/>[bam.r]"]
  node_mixed["Mixed GAMs<br/>[gamm.r]"]
  node_multi_predictor["Multi-predictor GAMs<br/>[gfam.r]"]
  node_gamlss["GAMLSS models<br/>[gamlss.r]"]
  node_jagam["JAGS interface<br/>[jagam.r]"]
  node_inla["INLA interface<br/>[inla.r]"]
end

subgraph group_results["Results and diagnostics"]
  node_predict["Prediction<br/>[mgcv.r]"]
  node_diagnostics["Model diagnostics<br/>[mgcv.r]"]
  node_visualization["Plots and summaries<br/>[plots.r]"]
end

subgraph group_native["Numerical engine"]
  node_magic["Penalty optimization<br/>[magic.c]"]
  node_gdi["Model derivatives<br/>[gdi.c]"]
  node_matrix["Matrix operations<br/>[mat.c]"]
  node_sparse["Sparse computations<br/>[sparse.c]"]
  node_discrete["Discrete computations<br/>[discrete.c]"]
  node_qp["Constrained least squares<br/>[qp.c]"]
  node_tprs["Thin-plate splines<br/>[tprs.c]"]
end

node_analyst(("R analyst"))
node_jags_service(("JAGS"))
node_inla_service(("INLA"))

node_analyst -->|"specifies and fits"| node_gam_api
node_gam_api -->|"constructs bases"| node_smooths
node_gam_api -->|"uses family"| node_families
node_gam_api -->|"fits model"| node_fit_core
node_fit_core -->|"selects penalties"| node_sp_selection
node_sp_selection -->|"optimizes penalties"| node_magic
node_fit_core -->|"computes derivatives"| node_gdi
node_fit_core -->|"uses matrix routines"| node_matrix
node_fit_core -->|"solves constraints"| node_qp
node_smooths -->|"constructs spline basis"| node_tprs
node_fit_alternate -->|"optimizes penalties"| node_magic
node_bam -->|"uses discrete methods"| node_discrete
node_bam -->|"uses sparse methods"| node_sparse
node_mixed -->|"builds GAM components"| node_gam_api
node_multi_predictor -->|"fits predictors"| node_fit_core
node_gamlss -->|"fits distributional models"| node_fit_core
node_jagam -.->|"generates JAGS model"| node_jags_service
node_inla -.->|"interfaces with INLA"| node_inla_service
node_analyst -->|"requests predictions"| node_predict
node_analyst -->|"checks fit"| node_diagnostics
node_analyst -->|"summarizes and plots"| node_visualization
node_predict -->|"uses fitted model"| node_gam_api
node_diagnostics -->|"inspects fitted model"| node_gam_api
node_visualization -->|"uses fitted model"| node_gam_api

click node_gam_api "https://github.com/cran/mgcv/blob/master/R/mgcv.r"
click node_smooths "https://github.com/cran/mgcv/blob/master/R/smooth.r"
click node_families "https://github.com/cran/mgcv/blob/master/R/misc.r"
click node_fit_core "https://github.com/cran/mgcv/blob/master/R/gam.fit3.r"
click node_fit_alternate "https://github.com/cran/mgcv/blob/master/R/gam.fit4.r"
click node_sp_selection "https://github.com/cran/mgcv/blob/master/R/fast-REML.r"
click node_bam "https://github.com/cran/mgcv/blob/master/R/bam.r"
click node_mixed "https://github.com/cran/mgcv/blob/master/R/gamm.r"
click node_multi_predictor "https://github.com/cran/mgcv/blob/master/R/gfam.r"
click node_gamlss "https://github.com/cran/mgcv/blob/master/R/gamlss.r"
click node_jagam "https://github.com/cran/mgcv/blob/master/R/jagam.r"
click node_inla "https://github.com/cran/mgcv/blob/master/R/inla.r"
click node_predict "https://github.com/cran/mgcv/blob/master/R/mgcv.r"
click node_diagnostics "https://github.com/cran/mgcv/blob/master/R/mgcv.r"
click node_visualization "https://github.com/cran/mgcv/blob/master/R/plots.r"
click node_magic "https://github.com/cran/mgcv/blob/master/src/magic.c"
click node_gdi "https://github.com/cran/mgcv/blob/master/src/gdi.c"
click node_matrix "https://github.com/cran/mgcv/blob/master/src/mat.c"
click node_sparse "https://github.com/cran/mgcv/blob/master/src/sparse.c"
click node_discrete "https://github.com/cran/mgcv/blob/master/src/discrete.c"
click node_qp "https://github.com/cran/mgcv/blob/master/src/qp.c"
click node_tprs "https://github.com/cran/mgcv/blob/master/src/tprs.c"

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_gam_api,node_smooths,node_families toneBlue
class node_fit_core,node_fit_alternate,node_sp_selection toneAmber
class node_bam,node_mixed,node_multi_predictor,node_gamlss,node_jagam,node_inla toneMint
class node_predict,node_diagnostics,node_visualization toneRose
class node_magic,node_gdi,node_matrix,node_sparse,node_discrete,node_qp,node_tprs,node_analyst,node_jags_service,node_inla_service toneIndigo
```

## Components

### Model specification

- [GAM interface](https://github.com/cran/mgcv/blob/master/R/mgcv.r): `R/mgcv.r`
- [Smooth bases](https://github.com/cran/mgcv/blob/master/R/smooth.r): `R/smooth.r`
- [Model families](https://github.com/cran/mgcv/blob/master/R/misc.r): `R/misc.r`

### Model fitting

- [GAM fitting](https://github.com/cran/mgcv/blob/master/R/gam.fit3.r): `R/gam.fit3.r`
- [Alternate fitting](https://github.com/cran/mgcv/blob/master/R/gam.fit4.r): `R/gam.fit4.r`
- [Smoothing selection](https://github.com/cran/mgcv/blob/master/R/fast-REML.r): `R/fast-REML.r`

### Model extensions

- [Large-data fitting](https://github.com/cran/mgcv/blob/master/R/bam.r): `R/bam.r`
- [Mixed GAMs](https://github.com/cran/mgcv/blob/master/R/gamm.r): `R/gamm.r`
- [Multi-predictor GAMs](https://github.com/cran/mgcv/blob/master/R/gfam.r): `R/gfam.r`
- [GAMLSS models](https://github.com/cran/mgcv/blob/master/R/gamlss.r): `R/gamlss.r`
- [JAGS interface](https://github.com/cran/mgcv/blob/master/R/jagam.r): `R/jagam.r`
- [INLA interface](https://github.com/cran/mgcv/blob/master/R/inla.r): `R/inla.r`

### Results and diagnostics

- [Prediction](https://github.com/cran/mgcv/blob/master/R/mgcv.r): `R/mgcv.r`
- [Model diagnostics](https://github.com/cran/mgcv/blob/master/R/mgcv.r): `R/mgcv.r`
- [Plots and summaries](https://github.com/cran/mgcv/blob/master/R/plots.r): `R/plots.r`

### Numerical engine

- [Penalty optimization](https://github.com/cran/mgcv/blob/master/src/magic.c): `src/magic.c`
- [Model derivatives](https://github.com/cran/mgcv/blob/master/src/gdi.c): `src/gdi.c`
- [Matrix operations](https://github.com/cran/mgcv/blob/master/src/mat.c): `src/mat.c`
- [Sparse computations](https://github.com/cran/mgcv/blob/master/src/sparse.c): `src/sparse.c`
- [Discrete computations](https://github.com/cran/mgcv/blob/master/src/discrete.c): `src/discrete.c`
- [Constrained least squares](https://github.com/cran/mgcv/blob/master/src/qp.c): `src/qp.c`
- [Thin-plate splines](https://github.com/cran/mgcv/blob/master/src/tprs.c): `src/tprs.c`

### Other components

- **R analyst**
- **JAGS**
- **INLA**

## Connections

- R analyst → GAM interface: specifies and fits
- GAM interface → Smooth bases: constructs bases
- GAM interface → Model families: uses family
- GAM interface → GAM fitting: fits model
- GAM fitting → Smoothing selection: selects penalties
- Smoothing selection → Penalty optimization: optimizes penalties
- GAM fitting → Model derivatives: computes derivatives
- GAM fitting → Matrix operations: uses matrix routines
- GAM fitting → Constrained least squares: solves constraints
- Smooth bases → Thin-plate splines: constructs spline basis
- Alternate fitting → Penalty optimization: optimizes penalties
- Large-data fitting → Discrete computations: uses discrete methods
- Large-data fitting → Sparse computations: uses sparse methods
- Mixed GAMs → GAM interface: builds GAM components
- Multi-predictor GAMs → GAM fitting: fits predictors
- GAMLSS models → GAM fitting: fits distributional models
- JAGS interface → JAGS: generates JAGS model
- INLA interface → INLA: interfaces with INLA
- R analyst → Prediction: requests predictions
- R analyst → Model diagnostics: checks fit
- R analyst → Plots and summaries: summarizes and plots
- Prediction → GAM interface: uses fitted model
- Model diagnostics → GAM interface: inspects fitted model
- Plots and summaries → GAM interface: uses fitted model
