Confusion about brms: random intercept formula notation with two lines (||) or one (|)

Hey all,
I have a simple question about the formula notation for brms with random intercepts.
In the paper at http://138.232.16.156/article/view/v080i01 it is stated that “By default,
group-level coefficients within a grouping factor are assumed to be correlated. Correlations
can be set to zero by using the (coefs || group) syntax”.

The way I understand this is that correlation is being estimated between the random effects pertaining to the same factor. However, I see no difference when running the following R code, which fits a one-way random effects model with both the | and || notation, furthermore when I investigate the first model (correlated random effects), I cannot find the estimated correlations anywhere. Am I interpreting the quote above incorrectly? If not, is there a way to obtain the estimated correlation between the random effects?

rm(list = ls())      #remove all vars
library(brms)
library(pracma)
library(Matrix)

#---------------------------sample data------------------------------
set.seed(1234567)
nvec = 10   # number of independent vectors Y
m     = 10
sigma2 = 1
tau    = 2

# Y_ij = theta_i + epsilon_ij, theta_i ~ N(0,tau), epsilon_ij ~ N(0, sigma2)
Y = as.matrix(bdiag(lapply(seq(nvec), function(i){ones(m,1)}))%*%rnorm(nvec, 0, sqrt(tau)) + rnorm(nvec*m, 0, sqrt(sigma2)))
data = as.data.frame(cbind(Y, Reduce(rbind, lapply(seq(nvec), function(i){i*ones(m,1)}))))
colnames(data) = c("outcomes", "factor")

#----------------------------------fit brms --------------------------------------------
burnin = 5e3
M = 1e4

seed_brms = 123
fit_cor = brm(formula = 'outcomes ~ 0 + (1 | factor)',
          data    = data,
          warmup  = burnin,
          iter    = M,
          chains  = 1,
          control = list(adapt_delta = 0.999),
          seed    = seed_brms)
summary(fit_cor)


fit_uncor = brm(formula = 'outcomes ~ 0 + (1 || factor)',
          data    = data,
          warmup  = burnin,
          iter    = M,
          chains  = 1,
          control = list(adapt_delta = 0.999),
          seed    = seed_brms)
summary(fit_uncor)

The | and || syntax is only relevant when you have multiple random effects. In your model:

outcomes ~ 0 + (1 | factor)

You’ve specified that there is a random intercept that varies across each level of factor. There are no correlations to estimate here since there is only a single random effect

1 Like

Thanks! That makes more sense to me. I think something was wrong in my definition of a random effect/group-level coefficient then, as I understood it the variables theta_i were the random effects, but maybe the total collection (theta_i)_i is more commonly (or at least for brms/stan) thought of as the random effect.

Just FYI, you don’t typically need that many samples (either in warmup or iter) in Stan. Many times, the default (iter=2000 of which half are warmup) is plenty. Just FYI to save you some time if you are used to some other sampler. I didn’t run your code here, so maybe it doesn’t apply, but usually I don’t need that many samples.

Thanks, that could indeed be the case. I didn’t really look at the ess/diagnostics of the samples though as this was an example I wanted to generate for the forum where both formulas gave the same result. The burn-in and number of iterations were from some other script where I analyzed a larger dataset.