Running blavaan model with Rstan

23 views
Skip to first unread message

Spencer Aeschliman

unread,
Apr 16, 2025, 2:51:27 PMApr 16
to blavaan
Hi all,

I have a working model in blavaan that I want to run on an HPC cluster I have access to (the outputs are quite cumbersome for the memory on my personal machine). I'm having some package installation issues on the cluster, but I do have Rstan up and running--so I want to use the `sem.stan` and `semstan.rda` files to run my model directly on Rstan.

However, I am running into some issues. Trying this out on my local machine, my R script looks like this:

library(rstan)
options(mc.cores = parallel::detectCores())
load("lavExport/semstan.rda")

stan_data <- stantrans$data

fit <- stan(
file = "lavExport/sem.stan",
data = stan_data,
pars = stantrans$monitors,
chains = 4,
warmup = 500,
iter = 1000,
cores = 4
)

print(fit)

Unfortunately, this fails immediately with the error: "parser failed badly; maybe try installing the V8 package"

Am I using the `sem.stan` and `stantrans` objects incorrectly?

Thanks,
-Spencer

Ed Merkle

unread,
Apr 16, 2025, 2:58:46 PMApr 16
to blavaan
Spencer,

Thanks for the report. Your stan() call looks correct, and I wonder whether you are using blavaan 0.5-5 or below. There was an issue in the Stan file where a commented-out "#" character led to "parser failed badly" on some systems.

If this is the problem, there are at least two possible fixes: upgrade to the newest version of blavaan (0.5-8), or open that sem.stan file, do a text search for "https", and delete those lines. (The problematic hashtags appear in urls, so you would just need to delete the urls.)

Ed

Spencer Aeschliman

unread,
Apr 16, 2025, 4:11:28 PMApr 16
to blavaan
Sure enough, it turns out I'm running blavaan v. 0.5-3. Deleting those lines from the Stan file worked and the model is sampling. I have a couple follow-up questions:

1) Is there any harm of initially running bsem(..., burnin=1, sample=1, ...) to quickly generate the Stan code, and then setting the proper number of samples later when directly running through Rstan?

2) Is there an easy way to recover the parameter names as written in the lavaan model syntax?

Thanks for the quick help and the great package.

-Spencer

Ed Merkle

unread,
Apr 16, 2025, 4:44:10 PMApr 16
to Spencer Aeschliman, blavaan
Glad to hear that fixed it. For #1, I think you are wanting the argument do.fit = FALSE. For #2, you might try

library(lavaan) # (for parTable)
with(parTable(fit), cbind(lhs, op, rhs, pxnames))

which assumes that fit is your blavaan object.

Ed

On Wed, 2025-04-16 at 13:11 -0700, Spencer Aeschliman wrote:
Sure enough, it turns out I'm running blavaan v. 0.5-3. Deleting those lines from the Stan file worked and the model is sampling. I have a couple follow-up questions:

1) Is there any harm of initially running bsem(..., burnin=1, sample=1, ...) to quickly generate the Stan code, and then setting the proper number of samples later when directly running through Rstan?

2) Is there an easy way to recover the parameter names as written in the lavaan model syntax?

Thanks for the quick help and the great package.

-Spencer

On Wednesday, April 16, 2025 at 1:58:46 PM UTC-5 Ed Merkle wrote:
Spencer,

Thanks for the report. Your stan() call looks correct, and I wonder whether you are using blavaan 0.5-5 or below. There was an issue in the Stan file where a commented-out "#" character led to "parser failed badly" on some systems.

If this is the problem, there are at least two possible fixes: upgrade to the newest version of blavaan (0.5-8), or open that sem.stan file, do a text search for "https", and delete those lines. (The problematic hashtags appear in urls, so you would just need to delete the urls.)

Ed


On Wednesday, April 16, 2025 at 1:51:27 PM UTC-5 aesch....@gmail.com wrote:
Hi all,

I have a working model in blavaan that I want to run on an HPC cluster I have access to (the outputs are quite cumbersome for the memory on my personal machine). I'm having some package installation issues on the cluster, but I do have Rstan up and running--so I want to use the `sem.stan` and `semstan.rda` files to run my model directly on Rstan.

However, I am running into some issues. Trying this out on my local machine, my R script looks like this:

library(rstan)
options(mc.cores= parallel::detectCores())
load("lavExport/semstan.rda")

stan_data <- stantrans$data

fit <-stan(
file="lavExport/sem.stan",
data= stan_data,
pars= stantrans$monitors,
chains=4,
warmup=500,
iter=1000,
cores=4
)

print(fit)

Unfortunately, this fails immediately with the error: "parser failed badly; maybe try installing the V8 package"

Am I using the `sem.stan` and `stantrans` objects incorrectly?

Thanks,
-Spencer
--
You received this message because you are subscribed to the Google Groups "blavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blavaan+u...@googlegroups.com.

Terrence Jorgensen

unread,
Jul 17, 2025, 7:21:55 AMJul 17
to blavaan
2) Is there an easy way to recover the parameter names as written in the lavaan model syntax?

There is a lavaan function for this, that returns parameter names (as used in coef(fit) output) from your parameter table:

example(cfa)
lav_partable_labels(parTable(fit))

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Reply all
Reply to author
Forward
0 new messages