https://github.com/haziqj/inlavaan
An R package for Bayesian structural equation modeling using INLA
https://github.com/haziqj/inlavaan
bayesian-inference bayesian-statistics factor-analysis growth-curve-models inla laplace-approximation latent-variables path-analysis psychometrics skew-normal statistical-modeling structural-equation-modeling
Last synced: 2 months ago
JSON representation
An R package for Bayesian structural equation modeling using INLA
- Host: GitHub
- URL: https://github.com/haziqj/inlavaan
- Owner: haziqj
- License: gpl-3.0
- Created: 2024-01-23T00:39:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-04-02T11:03:57.000Z (2 months ago)
- Last Synced: 2026-04-02T21:12:14.824Z (2 months ago)
- Topics: bayesian-inference, bayesian-statistics, factor-analysis, growth-curve-models, inla, laplace-approximation, latent-variables, path-analysis, psychometrics, skew-normal, statistical-modeling, structural-equation-modeling
- Language: R
- Homepage: https://inlavaan.haziqj.ml/
- Size: 50.4 MB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
options(cli.dynamic = FALSE, cli.progress_show_after = 2)
library(INLAvaan)
library(lavaan)
library(blavaan)
library(tidyverse)
```
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://github.com/haziqj/INLAvaan/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/haziqj/INLAvaan?branch=main)
[](https://cran.r-project.org/package=INLAvaan)
[](https://cran.r-project.org/package=INLAvaan)
[](https://cran.r-project.org/package=INLAvaan)
[](https://github.com/haziqj/INLAvaan/stargazers)
> Efficient approximate Bayesian inference for Structural Equation Models.
While Markov Chain Monte Carlo (MCMC) methods remain the gold standard for exact Bayesian inference, they can be prohibitively slow for iterative model development.
`{INLAvaan}` offers a rapid alternative for **la**tent **va**riable **an**alysis, delivering Bayesian results at (or near) the speed of frequentist estimators.
It achieves this through a custom, ground-up implementation of the [Integrated Nested Laplace Approximation (INLA)](https://www.r-inla.org), engineered specifically for the [lavaan](https://lavaan.ugent.be) modelling framework.
## A familiar interface
`{INLAvaan}` is designed to fit seamlessly into your existing workflow.
If you are familiar with the [(b)lavaan syntax](https://lavaan.ugent.be/tutorial/syntax1.html), you can begin using `{INLAvaan}` immediately.
As a first impression of the package, consider the canonical example of SEM applied to the Industrialisation and Political Democracy data set of Bollen (1989)^[Bollen, K. A. (1989). *Structural equations with latent variables* (pp. xiv, 514). John Wiley & Sons. ]:
```{r}
#| label: inlavaan-poldem
library(INLAvaan)
mod_poldem <- "
# Latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3
dem65 =~ y5 + y6 + y7 + y8
# Latent regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# Residual correlations
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
# Fixed loading
dem60 =~ 1.5*y4
# Custom priors on latent variances
ind60 ~~ prior('gamma(1, 1)')*ind60
dem60 ~~ prior('gamma(2, 1)')*dem60
dem65 ~~ prior('gamma(1,.5)')*dem65
"
utils::data("PoliticalDemocracy", package = "lavaan")
fit <- asem(model = mod_poldem, data = PoliticalDemocracy)
summary(fit)
```
## Validation against MCMC
Computation speed is valuable only when accuracy is preserved.
Our method yields posterior distributions that are visually and numerically comparable to those obtained via MCMC (e.g., via `{blavaan}`/Stan), but at a fraction of the computational cost.
The figure below illustrates the posterior density overlap for the example above.
The percentages refer to the one minus the [Jensen-Shannon distance](https://en.wikipedia.org/wiki/Jensen–Shannon_divergence), which gives a measure of similarity between two probability distributions.
```{r}
#| label: fig-compare-poldem
#| message: false
#| warning: false
#| results: "hide"
#| fig-height: 4.7
#| fig-width: 8
#| out-width: 100%
#| fig-dpi: 300
# install.packages("blavaan")
library(blavaan)
fit_blav <- bsem(model = mod_poldem, data = PoliticalDemocracy, seed = 2026)
res <- INLAvaan:::compare_mcmc(fit_blav, INLAvaan = fit)
print(res$p_compare)
```
## Installation
Install the CRAN version of `{INLAvaan}` using:
```r
install.packages("INLAvaan")
```
Alternatively, install the development version of `{INLAvaan}` from GitHub using:
```r
# install.packages("pak")
pak::pak("haziqj/INLAvaan")
```
*Optionally*^[R-INLA dependency has been removed temporarily from v0.2.0.], you may wish to install [INLA](https://www.r-inla.org).
Following the official instructions given [here](https://www.r-inla.org/download-install), install the package by running this command in R:
```r
install.packages(
"INLA",
repos = c(getOption("repos"),
INLA = "https://inla.r-inla-download.org/R/stable"),
dep = TRUE
)
```
## Citation
There are two papers related to `{INLAvaan}` and its underlying methodology.
To cite `{INLAvaan}` in publications, consider citing both.
To cite the methodological contribution exclusively, please use:
> Jamil, H., & Rue, H. (2026). _Approximate Bayesian inference for structural equation models using integrated nested Laplace approximations_ (2603.25690 [stat.ME]). arXiv. https://doi.org/10.48550/arXiv.2603.25690
To cite the software implementation and workflows, please use:
> Jamil, H., & Rue, H. (2026). _Implementation and workflows for INLA-based approximate Bayesian structural equation modelling_ (2604.00671 [stat.CO]). arXiv. https://doi.org/10.48550/arXiv.2604.00671
BibTeX entries for LaTeX users:
```{r, include = FALSE}
knitr::opts_chunk$set(comment = "")
```
```{r, echo = FALSE}
toBibtex(citation("INLAvaan"))
```
## License
The `{INLAvaan}` package is licensed under the [GPL-3](https://www.gnu.org/licenses/gpl-3.0.en.html).
```plaintext
INLAvaan: Bayesian Latent Variable Analysis using INLA
Copyright (C) 2026 Haziq Jamil
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
```
