Blavaan: how to get started

Developed by Naomi Schalken, Lion Behrens and Rens van de Schoot

This tutorial expects: 

  • Basic knowledge of correlation and regression
  • An installed version of R on your electronic device
  • Basic knowledge of R

⇒ If you feel like you need a refresher of your R skills, have a look at R: how to get started


This tutorial provides the reader with a basic introduction to the software package blavaan (Merkle & Rosseel, 2016a). blavaan is a free package that can be used within the R environment. The reader will be guided through the process of downloading blavaan and reading and running the R code to conduct a Bayesian regression analysis. Most of the written text consists of instructions. The exercises that are described in this document can also be found in the Regressionblavaan.txt file, which includes all the commands to run.


Exercise 1 - Preparing your data and regression model (default priors)

Exercise 1a. Installing blavaan

To install and activate the blavaan package, run the following code in R:


This step may take several minutes. When the R prompt shows R is done installing and you’re ready to use R again.


Exercise 1b. Loading data

To load in your datafile, specify your working directory and import your data.

data <- read.table("regression.txt")   


Exercise 1c. Naming variables

Currently, the datafile consists of three columns that don't have any name yet. Give names to the variables in the dataset (“y”, “x1”, “x2”) by executing the code below.

colnames(data)<- c("y", "x1", "x2")


Exercise 1d. Specify the regression model

Execute the following code in R.

model.regression <- '
# regression
y ~ x1 + x2 

# variances
y ~~ y

# intercepts

Here, we create something named “model.regression” that we call later on. This “model” is denoted by a bunch of characters (called a string) between single quotation marks. This string is assigned by the arrow symbol (<-). The string itself says that y, the dependent variable, is regressed on (~) the linear combination of x1 and x2 .


Exercise 2. Multiple Regression in blavaan (default priors)

Exercise 2a. Fitting the Bayesian regression model

To run your regression model in blavaan, you can first fit the regression to an object called fit.regression1.

fit.regression1 <- blavaan(model.regression, data=data)


Exercise 2b. Obtaining summary estimates of the parameters

You have new stored the fitted regression model to the object fitregression1. In order to inspect your model results, request a summary of this newly created object.


Question: Interpret your regression results. How would you summarize the effects of x1 and x2 on y? What do you expect the posterior distributions to look like?


Exercise 2c. Obtaining different plots of the parameters of the fitted model

Next to inspecting your model results graphically, checking model convergence is crucial for Bayesian inference. By executing the code below, you will obtain different kinds of plots that entail information on the sampling process and posterior distributions.

plot(fit.regression1, pars= 1:4, plot.type="trace") #Traceplots
plot(fit.regression1, pars= 1:4, plot.type="autocorr") #Autocorrelation plots
plot(fit.regression1, pars= 1:4, plot.type="ecdf") #ECDF plots
plot(fit.regression1, pars= 1:4, plot.type="density") #Density plots

Question: Interpret all obtained plots. What do they tell you about convergence, autocorrelation and the posterior distributions of the regression parameters?

Exercise 3 Multiple Regression in Blavaan (specified priors)

Exercise 3a. Fit the new Bayesian regression model in blavaan

Apart from relying on priors that are automatically created, you can also configure specific prior distributions for all relevant parameters. Here, we choose so called non-informative priors, which have a mean of 0 and a very low precision. Your prior distributions will thus hardly influence your posterior results.

fit.regression2 <- blavaan(model.regression, data=data, dp = dpriors(itheta = "dgamma(0.01, 0.01)", nu= "dnorm(0.0, 0.001)", beta = "dnorm(0.0, 0.001)"))


Exercise 3b. Obtaining summary estimates

Just like in Exercise 2, go ahead and inspect your parameter estimates by executing the code below.


How did this model output change compared to the one of Exercise 2?


Exercise 3c. Obtain the plots of the parameters again

In Exercise 2c, you were asked to inspect the obtained posterior distributions and the underlying sampling process graphically. Do so again, but now using the new model object.



Merkle, E. C., & Rosseel, Y. (2016a). Blavaan: Bayesian Structural Equation Models via Parameter Expansion.

Merkle, E. C., & Rosseel, Y. (2016b). Bayesian Latent Variable Analysis.

Other software exercises you might be interested in


How to get started

First Bayesian Inference

How to avoid and when to worry about the misuse of Bayesian Statistics



How to get started



How to get started



First Bayesian Inference

How to avoid and when to worry about the misuse of Bayesian Statistics



How to get started

First Bayesian Inference

How to avoid and when to worry about the misuse of Bayesian Statistics



How to get started