# Confirmatory Factor Analysis using brms

Hi all,

Not a problem, more a little bit of helpful code. `brms` can already estimate latent variables using the `mi()` function for missing data. It can also constrain parameters to be constant. This, I think, provides all the means to fit rudimentary confirmatory factor analysis models.

Simulation code below.

``````
library(tidyverse)
library(brms)

N <- 200

dta <-
tibble(
x = rnorm(N, 0, 1),
y1 = rnorm(N, 2*x, 1),
y2 = rnorm(N, 1*x, 1),
y3 = rnorm(N, 0.5*x, 1),
xo = as.numeric(NA)
)

m1 <-
brm(
formula =
bf(y1 ~ 0 + mi(xo)) +
bf(y2 ~ 0 + mi(xo)) +
bf(y3 ~ 0 + mi(xo)) +
bf(xo | mi() ~ 1) +
set_rescor(rescor = FALSE),
family = gaussian(),
prior =
prior(constant(1), class = "b", resp = "y1") +
prior(constant(1), class = "sigma", resp = "y1") +
prior(normal(0, 10), class = "b", resp = "y2") +
prior(constant(1), class = "sigma", resp = "y2") +
prior(normal(0, 10), class = "b", resp = "y3") +
prior(constant(1), class = "sigma", resp = "y3") +
prior(normal(0, 10), class = "Intercept", resp = "xo") +
prior(cauchy(0, 1), class = "sigma", resp = "xo"),
data = dta,
backend = "cmdstanr",
cores = 4,
chains = 4,