Blavaan convergence warnings

102 views
Skip to first unread message

Niels Skovgaard-Olsen

unread,
Dec 20, 2023, 6:22:59 AM12/20/23
to blavaan
Hi!

I am using a SEM model investigate relationships between parameter values of a separately fitted model and several covariates. These parameters are the endogenous variables and the covariates are the exogenous variables.

When I fit the model in blavaan, I receive the warning that: 
"blavaan 0.5.2 did NOT end normally after X iterations". 

I have experimented with various values of adapt. sample, and burin. 

In one run, I set:
n.chains = 4, burnin = 15000, adapt = 15000,sample = 55000

I another, I set:
n.chains = 15, burnin = 25000, adapt = 25000,sample = 80000

When I fit the model in lavaan, I get a well-fitting model with:

  Test statistic                                40.029
  Degrees of freedom                            28
  P-value (Chi-square)                      0.066
  
  Comparative Fit Index (CFI)         0.998
  Tucker-Lewis Index (TLI)              0.982
  RMSEA                                             0.047
  SRMR                                               0.027

The model includes four groups with equality constraints on some of the path coefficients (see below).

Now I wanted to ask, whether there are ways that I can improve the estimation in blavaan. Note that I set target = "jags" when I fit it in blavaan, because the sampling terminates in error when I set the target to "stan". I have tried to update the packages on the server - but so far with no avail. So, my question concerns optimization of estimation of the model with jags.

m0e_3g <- '
#Regressions
C_i ~ c(p1.d,p1.u,p1.de,p1.a)*P + c(e1.d,e1.u,e1.de,e1.a)*E + c(gg1.d,gg1.u,gg1.de,gg1.a)*GG + c(iwah1.d,iwah1.u,iwah1.de,iwah1.a)*IWAC + c(egop1.d,egop1.u,egop1.de,egop1.a)*EgoP + c(egor1.d,egor1.u,egor1.de,egor1.a)*EgoR + c(egom1.d,egom1.u,egom1.de,egom1.a)*EgoM
C_s ~ c(p2.d,p2.u,p2.de,p2.a)*P + c(e2.d,e2.u,e2.de,e2.a)*E + c(gg2.d,gg2.u,gg2.de,gg2.a)*GG + c(iwah2.d,iwah2.u,iwah2.de,iwah2.a)*IWAC + c(egop2.d,egop2.u,egop2.de,egop2.a)*EgoP + c(egor2.d,egor2.u,egor2.de,egor2.a)*EgoR + c(egom2.d,egom2.u,egom2.de,egom2.a)*EgoM
N_i ~ c(p3.d,p3.u,p3.de,p3.a)*P + c(e3.d,e3.u,e3.de,e3.a)*E + c(gg3.d,gg3.u,gg3.de,gg3.a)*GG + c(iwah3.d,iwah3.u,iwah3.de,iwah3.a)*IWAC + c(egop3.d,egop3.u,egop3.de,egop3.a)*EgoP + c(egor3.d,egor3.u,egor3.de,egor3.a)*EgoR + c(egom3.d,egom3.u,egom3.de,egom3.a)*EgoM  
N_s ~ c(p4.d,p4.u,p4.de,p4.a)*P + c(e4.d,e4.u,e4.de,e4.a)*E + c(gg4.d,gg4.u,gg4.de,gg4.a)*GG + c(iwah4.d,iwah4.u,iwah4.de,iwah4.a)*IWAC + c(egop4.d,egop4.u,egop4.de,egop4.a)*EgoP + c(egor4.d,egor4.u,egor4.de,egor4.a)*EgoR + c(egom4.d,egom4.u,egom4.de,egom4.a)*EgoM
Con ~ c(p5.d,p5.u,p5.de,p5.a)*P + c(e5.d,e5.u,e5.de,e5.a)*E + c(gg5.d,gg5.u,gg5.de,gg5.a)*GG + c(iwah5.d,iwah5.u,iwah5.de,iwah5.a)*IWAC + c(egop5.d,egop5.u,egop5.de,egop5.a)*EgoP + c(egor5.d,egor5.u,egor5.de,egor5.a)*EgoR + c(egom5.d,egom5.u,egom5.de,egom5.a)*EgoM  
Res ~ c(p6.d,p6.u,p6.de,p6.a)*P + c(e6.d,e6.u,e6.de,e6.a)*E + c(gg6.d,gg6.u,gg6.de,gg6.a)*GG + c(iwah6.d,iwah6.u,iwah6.de,iwah6.a)*IWAC + c(egop6.d,egop6.u,egop6.de,egop6.a)*EgoP + c(egor6.d,egor6.u,egor6.de,egor6.a)*EgoR + c(egom6.d,egom6.u,egom6.de,egom6.a)*EgoM  
I ~   c(p7.d,p7.u,p7.de,p7.a)*P + c(e7.d,e7.u,e7.de,e7.a)*E + c(gg7.d,gg7.u,gg7.de,gg7.a)*GG + c(iwah7.d,iwah7.u,iwah7.de,iwah7.a)*IWAC + c(egop7.d,egop7.u,egop7.de,egop7.a)*EgoP + c(egor7.d,egor7.u,egor7.de,egor7.a)*EgoR + c(egom7.d,egom7.u,egom7.de,egom7.a)*EgoM  

################
#equality constraints#
################
#doctrine of double effect is like deontology for intended means; like utilitarianism for side-effect

p1.de == p1.d
p2.de == p2.u
p3.de == p3.d
p4.de == p4.u

e1.de == e1.d
e2.de == e2.u
e3.de == e3.d
e4.de == e4.u

gg1.de == gg1.d
gg2.de == gg2.u
gg3.de == gg3.d
gg4.de == gg4.u

iwah1.de == iwah1.d
iwah2.de == iwah2.u
iwah3.de == iwah3.d
iwah4.de == iwah4.u

egop1.de == egop1.d
egop2.de == egop2.u
egop3.de == egop3.d
egop4.de == egop4.u

egor1.de == egor1.d
egor2.de == egor2.u
egor3.de == egor3.d
egor4.de == egor4.u

egom1.de == egom1.d
egom2.de == egom2.u
egom3.de == egom3.d
egom4.de == egom4.u
'

m0e_3g_model <- sem(m0e_3g, df_class, group ="Classification")

Thanks in advance!
Niels

Ed Merkle

unread,
Dec 20, 2023, 12:33:21 PM12/20/23
to Niels Skovgaard-Olsen, blavaan
Niels,

I would be interested to see the error message from Stan on this one. 

It looks like your model has no latent variables, so that convergence problems are not due to sign indeterminacy issues. I would then guess that the default priors in blavaan clash with your observed data. Especially if you have variables that take values far from 0, this can cause problems. Maybe the best thing to do is specify your own priors that are tailored to your model and data. A fast thing to do is standardize all your variables to see whether that aids in convergence.

Ed
--
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.

Niels Skovgaard-Olsen

unread,
Dec 20, 2023, 1:39:36 PM12/20/23
to Ed Merkle, blavaan
Ed,

thanks for your reply!

Here is the stan call:

m_blavaan <- bsem(model, data = df,
                                        n.chains = 15, burnin = 30000, sample = 80000, adapt = 25000,  
                                        mcmcfile = TRUE)

I run the analysis on an HPC server, so there is an error file with this output:

error occurred during calling the sampler; sampling not done
....
error occurred during calling the sampler; sampling not done
here are whatever error messages were returned
Error in rowvec[samppar] <- which(grepl(stanvec[j], names(b.est)) & !(grepl("_c\\[",  :
  replacement has length zero
Calls: dual_fit ... bsem -> eval -> eval -> blavaan -> coeffun_stanmarg
In addition: Warning message:
In .local(object, ...) :
  some chains had errors; consider specifying chains = 1 to debug
Execution halted

and then there is a further output file with this content:

SAMPLING FOR MODEL 'stanmarg' NOW (CHAIN 1).
...

Chain 7: Unrecoverable error evaluating the log probability at the initial value.
Chain 7: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)
[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 5: Unrecoverable error evaluating the log probability at the initial value.
Chain 5: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)
[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 13: Unrecoverable error evaluating the log probability at the initial value.

...


[[1]]
Stan model 'stanmarg' does not contain samples.

...
Stan model 'stanmarg' does not contain samples.

#################

Note that these dimension mismatch errors occur although both lavaan and blavaan (with the jags target) were able to sample from the model with the same data set.

I have also tried using some of the covariates to add a latent factor to the model in a measurement model. But the X^2 increased quite a lot in lavaan, so although the AIC and BIC preferred that model, it was misfitting the data.

The variables have not been standardized so I could try that. But they have all been transformed to take values in [0,1] so that their variances would be of a similar magnitude.

I can also try to add priors. And I guess for the complaints in stan about the initial values, the estimates from lavaan could be used to specify initial values for the 15 chains. Are there any good blavaan papers that I should look at for these two things?

Thanks!
Niels
--

Ed Merkle

unread,
Dec 20, 2023, 1:54:59 PM12/20/23
to Niels Skovgaard-Olsen, blavaan
Thanks for the further info. That "mismatch in dimension" message looks like a bug in blavaan. I will explore some more.

Ed

Ed Merkle

unread,
Dec 20, 2023, 4:16:23 PM12/20/23
to Niels Skovgaard-Olsen, blavaan
I found a bug related to equality constraints using "==", for target="stan". Please install the github version if you can, to see whether it fixes your problem. Instructions are at the bottom of this page:


Thanks,
Ed
 

On Wed, 2023-12-20 at 19:39 +0100, Niels Skovgaard-Olsen wrote:

Niels Skovgaard-Olsen

unread,
Dec 21, 2023, 6:32:17 PM12/21/23
to Ed Merkle, blavaan
Ed,

thanks a lot!

I was travelling so could not check the server before now.

I uploaded the developer version as you suggested ( blavaan 0.5-2.1205 ?)

and received the following two outputs again.

Interestingly, the second output talks about a " Chain Chain 144" - although there were only 15 mcmc chains by the function call.

#####1#########

This is blavaan 0.5-2.1205
On multicore systems, we suggest use of future::plan("multicore") or
  future::plan("multisession") for faster post-MCMC computations.

error occurred during calling the sampler; sampling not done
...
here are whatever error messages were returned
Error in rowvec[samppar] <- which(grepl(stanvec[j], names(b.est)) & !(grepl("_c\\[",  :
  replacement has length zero
Calls: dual_fit ... bsem -> eval -> eval -> blavaan -> coeffun_stanmarg
In addition: Warning message:
In .local(object, ...) :
  some chains had errors; consider specifying chains = 1 to debug
Execution halted


######2######

Chain 7: Unrecoverable error evaluating the log probability at the initial value.
Chain 7: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)
[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 9: Unrecoverable error evaluating the log probability at the initial value.
Chain 9: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 11: Unrecoverable error evaluating the log probability at the initial value.
Chain 11: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 8: Unrecoverable error evaluating the log probability at the initial value.
Chain 8: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 13: Unrecoverable error evaluating the log probability at the initial value.
Chain 13: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)
Chain 12: Unrecoverable error evaluating the log probability at the initial value.
Chain 12: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 2: Unrecoverable error evaluating the log probability at the initial value.
Chain 2: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 6: Unrecoverable error evaluating the log probability at the initial value.
Chain 6: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 3: Unrecoverable error evaluating the log probability at the initial value.
Chain 3: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 5: Unrecoverable error evaluating the log probability at the initial value.
Chain 5: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)
[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain Chain 144: : Unrecoverable error evaluating the log probability at the initial value.Unrecoverable error evaluating the log probability at the initial value.

Chain Chain 144: : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)


[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 10: Unrecoverable error evaluating the log probability at the initial value.
Chain 10: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
Chain 15: Unrecoverable error evaluating the log probability at the initial value.
Chain 15: Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)

[1] "Error : Exception: mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=Psi_r_free; position=0; dims declared=(0); dims found=(84) (in 'stanmarg', line 1042, column 2 to column 52)"
[[1]]


All the best,
Niels

Ed Merkle

unread,
Dec 28, 2023, 7:21:21 PM12/28/23
to Niels Skovgaard-Olsen, blavaan
Niels,

Thanks for following up, and sorry for the delay. 

Yes, 0.5-2.1205 should have fixed it. Is it possible to send your data off-list? That would help to diagnose.

Thanks,
Ed

Niels Skovgaard-Olsen

unread,
Dec 29, 2023, 5:53:37 AM12/29/23
to Ed Merkle, blavaan
Ed,

that sounds great! I sent you a zip folder to your missouri email account.

All the best,
Niels

Ed Merkle

unread,
Jan 3, 2024, 10:50:19 AM1/3/24
to blavaan
Just a follow-up to this post: we corresponded off list and identified some additional issues with initial values in the development version of blavaan. These should now be fully fixed in 0.5-2.1211.

Ed
Reply all
Reply to author
Forward
0 new messages