Error on non NPD matrices

61 views
Skip to first unread message

gunnarg...@gmail.com

unread,
Oct 21, 2022, 4:08:56 PM10/21/22
to blavaan

Hello,

I am working on a Masters thesis with a comparison of frequentist and Bayesean SEM and estimating with simulations the “same” model in lavaan (SEM) and blavaan (BSEM). I have used the estimation code as a function and then use replicate to get 500 replications. I have checked that the initial covariance matrix to be tested is not NPD and everything works fine with the lavaan analyses.

From the 2021 article it seems that when using blavaan with Stan NPD problems “should” be handled by Stan, but maybe this is a more general statement with exceptions.  In some simulation replications with blavaan I get the message that

“Chain 3: Exception: multi_normal_rng: Matrix covariance matrix argument is not positive definite  (in 'model_stanmarg' at line 1017)”

I assume this happens because when a new sample is drawn it has become a  NPD matrix. I would like to (a) see if these matrices could be transformed to nonNPD or (b) skip this case and continue with the next simulation, noting the number of cases with NPD problems.

Any suggestions how to pursue on this would be much appreciated

Gunnar Gelin, Statistics Dept, Stockholm university

Ed Merkle

unread,
Oct 21, 2022, 4:20:55 PM10/21/22
to gunnarg...@gmail.com, blavaan
Hi Gunnar,

Does Stan stop sampling, or does it just produce many of those "Exception" messages? If the latter, then I think the sampling itself is still ok. That exception comes from generated quantities, when we are generating artificial data for a posterior predictive p-value. So I would not necessarily trust the ppp value that blavaan reports, but I think you could still use the posterior samples as usual.

You might also be able to avoid the messages by adding

test = "none"

to your model estimation command (which would avoid ppp computations).

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/blavaan/00e72455-6af5-48a0-82eb-65b51beb1b1fn%40googlegroups.com.

Brett

unread,
Oct 21, 2022, 4:40:41 PM10/21/22
to gunnarg...@gmail.com, blavaan
Can you provide a minimal working example or at least code for both models?

My understanding is that blavaan allows specifications that can produce non-PD matrices by rejecting them until one is found. Since the PD share of valid matrices is incredibly small in some cases, perhaps a matrix wasn't found within constraints of the sampler? I would imagine that would throw an error. Similarly it could just be a warning that it came across a non-PD matrix.

In any of these cases, comparing Bayesian and non-Bayesian models without really knowing how informative the priors you are using are on the entire matrix doesn't seem like a particularly good idea - presumably you'd want to compare with more or less informative priors. You could try the equivalent of prior predictive checks to see how it works out but I imagine it will be hard to figure out due to the complex interdependencies among the univariate priors. One approach that could be more transparent is found here ttp://srmart.in/informative-priors-for-correlation-matrices-an-easy-approach/



--

gunnarg...@gmail.com

unread,
Oct 21, 2022, 9:40:02 PM10/21/22
to blavaan
I answered both to Ed Markle and Brett but it seems  these answers have not been delivered. So here is a compressed retry to answer again
.
First to Ed Merkel: Thanks for the fast reply! I used your test="none" suggestion, and it more or less solved the problem. However,  the run "hangs" and I have to click to stop this "hanging", whereupon the replication process continues. I do not understand why this happens. The bsem part is in the end of a larger function,  replicated using replicate. There are no such problems when running sem in lavaan.
Also I have an additional question: is it possible to suppress the printed output, since it becomes too much as I get information on the three chains for every replication and have 500 replications. I only want to create a result file with one row for each replication with fit measures etc, to be analyzed later.

To Brett: Yes I have read the "easy approach" blog and some related discussions and of course agree with you that there is a need to investigate how different priors relate to estimation results, Typically it seems that when estimating a correlation the estimated  values for "true" correlations close to 1 bedome lower and vice versa for estimates of "true" corrs close to 0 as is shown in the blog you refer to and other contributions. . Although this discussion is about correlation estimation I guess  it is relevant also for regression and BSEM,  and there is an expanding  literature on this which I have not yet used in full for my work but intend to.
Thanks very much for these responses!
Gunnar Gelin

Gunnar Gelin

unread,
Oct 24, 2022, 9:14:59 AM10/24/22
to blavaan

To Ed Merkle:

At first it seemed ok to use your suggestion test=”none” since the error messages disappeared,  but now I see that the run “hangs” without giving messages. This occurs at various stages of the replication, so it is most presumably a NPD problem due to that the sampling gives a problematic matrix. I have gone through the Stan User Manual and some additional sources but not found out how to deal with this. My intention is to simply skip a replication case that gives this problem, increase a counter variable indicating that his problem occurred, and go in with the next replication case. But how to code this and also keep the replicability goals with seed etc? This is not transparent to me, but I suppose this is not the first time such problems occur, so could you please suggest other studies and/or code to handle this? Thanks in advance!


You received this message because you are subscribed to a topic in the Google Groups "blavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blavaan/feKgiTvqxio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blavaan+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blavaan/51f6ecf5-238b-4ea4-8e47-00ec8f6adeb7n%40googlegroups.com.

Ed Merkle

unread,
Oct 24, 2022, 11:30:51 AM10/24/22
to Gunnar Gelin, blavaan
Hi Gunnar,

I do not know a general solution here because I don't often see Stan hang. When it does hang, I restart my session and then everything seems to work again as expected. I realize that is not optimal in the context of a simulation study.

It might be helpful if you send your model syntax and the blavaan commands that you are using, which might give me other ideas.

Ed

Elizabeth A. Sanders

unread,
Feb 6, 2023, 9:18:50 AM2/6/23
to blavaan
Hi all,

Has anyone found any new solutions regarding the problem of 'hanging' that comes with this error message below?

“Chain 3: Exception: multi_normal_rng: Matrix covariance matrix argument is not positive definite  (in 'model_stanmarg' at line 1017)”

I am running a simulation and only very few of the conditions are giving me this hanging/error message issue. I'd like to be able to tell blavaan to conditionally stop/skip analyses with this error code, and move on to the next datafile.

Thank you in advance if you have any insights!

Best,
Liz

Ed Merkle

unread,
Feb 6, 2023, 9:50:40 AM2/6/23
to Elizabeth A. Sanders, blavaan
Liz,

I had some off-list discussion with Gunnar about this (Gunnar started the thread) but am not sure that the details made it back to the list.

It seems that the hanging was due to extreme initial values. In blavaan 0.4-3, the initial values for each chain are randomly drawn from the priors. If you have non-informative priors, then those initial values sometimes are very extreme, and that seemed to cause Stan to hang. I have since changed the default way of generating initial values, to try to avoid the issue. If you can install blavaan from github, that includes the change along with other speedups for many models:


Or, if you stick with blavaan 0.4-3, then add 

inits = "simple"

to your model estimation command (bsem/bcfa/blavaan). And if you don't care about the ppp, you could also set

test = "none"

which may help avoid the issue.


Ed
Hello,
I am working on a Masters thesis with a comparison of frequentist and Bayesean SEM and estimating with simulations the “same” model in lavaan (SEM) and blavaan (BSEM). I have used the estimation code as a function and then use replicate to get 500 replications.I have checked that the initial covariance matrix to be tested is not NPD and everything works fine with the lavaan analyses.

Elizabeth A. Sanders

unread,
Feb 6, 2023, 9:53:46 AM2/6/23
to Ed Merkle, blavaan
Perfect, that makes sense, thanks Ed! Super appreciate the fixes!
Best,
Liz

***************
Elizabeth A. Sanders, Ph.D.
Associate Professor
Measurement & Statistics
Educational Psychology
College of Education
University of Washington
312G Miller Hall, Box 353600
Seattle, WA 98195-3600
Email: li...@uw.edu
Pronouns: She/Her/Hers
***************
All correspondence to/from this address constitutes a public record per R.C.W. 42.56. As such, please direct all messages unrelated to University of Washington business to liz.sand...@gmail.com.

We acknowledge that we work and live on the ancestral homelands of the Coast Salish Peoples who have lived here from time immemorial.

Gunnar Gelin

unread,
Feb 6, 2023, 6:25:33 PM2/6/23
to Ed Merkle, Elizabeth A. Sanders, blavaan
Hi all,

I would just want to add that I am grateful to and impressed by Ed Merkles analysis and code development. The problem seems to be solved if you follow his advices and/or the Github version, !

Many thanks again Ed Merkle!
Gunnar Gelin Stockholm Sweden

--
You received this message because you are subscribed to a topic in the Google Groups "blavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blavaan/feKgiTvqxio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blavaan+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blavaan/063d28bdbb5f301655fbdbd0bc2b471bd80e8341.camel%40gmail.com.
Reply all
Reply to author
Forward
0 new messages