Need you help about "blavaan" error

525 views
Skip to first unread message

luquany...@gmail.com

unread,
May 4, 2016, 9:11:14 PM5/4/16
to blavaan
 I am a beginner in lavaan and blavaan library. I have some basic questions in my code (Please see the attachment).
Some errors often appear, like this:

library(blavaan)

library(rjags)

library(runjags)

library(modeest)

x<-read.csv("C:/Users/Quanying LU/Desktop/5.csv")

co2.model<-'

# measurement model

co2=~y1

total=~y2+y3+y4+y5+y6

struct=~y7+y8+y9+y10

tech=~y11

# regressions

co2~total+struct+tech

total~struct+tech

struct~tech

# residual covariances

y2~~y3+y4+y5+y6+y7+y8+y9+y10+y11

y3 ~~ y4+y7

y5 ~~ y7+y8+y9

y6 ~~ y7+y8+y9+y11

y7~~y8

y8~~y11

y10~~y11'

fit <- bsem(co2.model, data=x,

            dp=dpriors(nu="dnorm(5,1e-3)", itheta="dlnorm(1,.1)[sd]",

                       ipsi="dlnorm(1,.1)[sd]", rho="dbeta(3,3)"),

            jagcontrol=list(method="rjparallel"))

       

summary(fit)

 

 

Compiling rjags model...

Error : The following error occured when compiling and adapting the model using rjags:

 Error in rjags::jags.model(model, data = dataenv, inits = inits, n.chains = length(runjags.object$end.state),  :

  Error in node theta[1,1]

Invalid parent values

 

 

It may help to use failed.jags(c('model','data','inits')) to see model/data/inits syntax with line numbers

Error in blavaan(co2.model, data = x, dp = dpriors(nu = "dnorm(5,1e-3)",  :

  blavaan ERROR: problem with jags estimation.  The jags model and data have been exported.


I would be greatly appreciated if you could spend some of your time help me.
lavExport.rar
code.docx
5.csv

ecme...@gmail.com

unread,
May 5, 2016, 10:05:01 AM5/5/16
to blavaan
Quanying,

I can reproduce this error and will fix it soon. It is related to the two latent variables with only one observed variable (co2 and tech): the observed variable variances need to be fixed to 0, and blavaan chokes here.

Also, if this is your real dataset (as opposed to an example dataset that throws the error), I would say that your model is very complex for the data. You only have 27 observations, so the prior distributions are likely to have a strong influence on the posterior parameter values.

Finally, you should only need the "library(blavaan)" command; those other packages will be loaded automatically as needed.

Ed

luquany...@gmail.com

unread,
May 6, 2016, 10:52:44 PM5/6/16
to blavaan
Dear Ed:
Thank you for your help!
This is my real data. As you know, I have a tiny sample size, and I want to buid a BSEM model to solve this defect.(Ps. I am not sure if this will work).
I do not know how to fix my code. If I delete one latent variables tech, the same error will occur. I had try to build sem before bsem, there were also some mistakes in  my code. 

If I delet one latent variables tech, the same error will occur.
> library(blavaan)
> x<-read.csv("C:/Users/Quanying LU/Desktop/5.csv")
> co2.model<-'
+ # measurement model
+ co2=~y1
+ total=~y2+y3+y4+y5+y6
+ struct=~y7+y8+y9+y10
+ # regressions
+ co2~total+struct
+ total~struct
+ # residual covariances
+ y2~~y3+y4+y5+y6+y7+y8+y9+y10
+ y3 ~~ y4+y7+y9
+ y5 ~~ y4+y7+y8+y9
+ y6 ~~ y4+y7+y8+y9
+ y7~~y4
+ '
> fit <- bsem(co2.model, data=x, 
+             dp=dpriors(nu="dnorm(5,1e-2)", itheta="dlnorm(1,.1)[sd]",
+                        ipsi="dlnorm(1,.1)[sd]", rho="dbeta(3,3)"),
+             jagcontrol=list(method="rjparallel"))
Compiling rjags model...
Error : The following error occured when compiling and adapting the model using rjags:
 Error in rjags::jags.model(model, data = dataenv, inits = inits, n.chains = length(runjags.object$end.state),  : 
  Error in node theta[1,1]
Invalid parent values



It may help to use failed.jags(c('model','data','inits')) to see model/data/inits syntax with line numbers
Error in blavaan(co2.model, data = x, dp = dpriors(nu = "dnorm(5,1e-2)",  : 
  blavaan ERROR: problem with jags estimation.  The jags model and data have been exported.

When I change the regressions part like below, and add " co2~~0.01*co2", there are same errors. How to correct?
I have a puzzle. If we do not have a tiny sample size, add "co2~~0.01*co2", whether the results will be affected?
Whether we can change "total~~0.01*total" or "struct~~0.01*struct"? What is the foundation? Exogenous variable? Endogenous Variable?
Please forgive me for interrupting!
Thank you very much!

library(lavaan)
> x<-read.csv("C:/Users/Quanying LU/Desktop/5.csv")
> co2model<-'
+ # measurement model
+ co2=~y1
+ total=~y2+y3+y4+y5+y6
+ struct=~y7+y8+y9+y10
+ co2~~0.01*co2
+ # regressions
+ co2~total+struct
+ total~struct
+ '
> fit <- sem(co2model, data = x,meanstructure = "default",
+ conditional.x = "default", fixed.x = "default",
+ orthogonal = FALSE, std.lv = FALSE,
+ parameterization = "default", std.ov = FALSE,
+ missing = "default", ordered = NULL,
+ sample.cov = NULL, sample.cov.rescale = "default",
+ sample.mean = NULL, sample.nobs = NULL,
+ ridge = 1e-05,group = NULL,
+ group.label = NULL, group.equal = "", group.partial = "",
+ group.w.free = FALSE, cluster = NULL, constraints = '',
+ estimator = "default", likelihood = "default", link = "default",
+ information = "default", se = "default", test = "default",
+ bootstrap = 1000L, mimic = "default", representation = "default",
+ do.fit = TRUE, control = list(), WLS.V = NULL, NACOV = NULL,
+ zero.add = "default", zero.keep.margins = "default",zero.cell.warn = TRUE,
+ start = "default", verbose = FALSE, warn = TRUE, debug = FALSE)
Warning messages:
1: In lav_object_post_check(lavobject) :
  lavaan WARNING: some estimated variances are negative
2: In lav_object_post_check(lavobject) :
  lavaan WARNING: observed variable error term matrix (theta) is not positive definite; use inspect(fit,"theta") to investigate.



在 2016年5月5日星期四 UTC+8下午10:05:01,ecme...@gmail.com写道:

luquany...@gmail.com

unread,
May 6, 2016, 10:57:41 PM5/6/16
to blavaan
Dear Ed:

Can you show me how to correct my code?
I would be very grateful for your help!

Quanying 


在 2016年5月5日星期四 UTC+8下午10:05:01,ecme...@gmail.com写道:
Quanying,

ecme...@gmail.com

unread,
May 9, 2016, 11:39:35 AM5/9/16
to blavaan
Quanying,

There are a few issues here:

1. There is a bug in blavaan when you have a latent variable with only one observed variable. This is not yet fixed and may take a little time (it is trickier than I initially thought).

2. I'm not sure about the goal of your analysis, so it is difficult to make suggestions. It might be helpful to talk to a local statistics expert here.

3. Keeping #2 in mind paired with the small sample size, I would remove the correlated residuals and also consider whether regression might be more suitable over SEM.


Ed

luquany...@gmail.com

unread,
May 12, 2016, 10:04:53 AM5/12/16
to blavaan
Dear Ed:

Thank you very much!

在 2016年5月9日星期一 UTC+8下午11:39:35,Ed Merkle写道:

luquany...@gmail.com

unread,
Jun 17, 2016, 1:48:08 AM6/17/16
to blavaan
Dear Ed

I have a new problem. I do not know how to solve it. Can you help me?

library(blavaan)
x<-read.csv("C:/Users/Quanying LU/Desktop/11.csv")

co2.model<-'
+ # measurement model
+ y=~y1+y2
+ total=~y3+y4+y5+y6
+ struct=~y7+y8+y9
+ tech=~y10+y11+y12
+ # regressions
+ y~total+struct+tech
+ total~struct+tech
+ struct~tech
+ '
> fit <- bsem(co2.model, data=x, 
+             dp=dpriors(nu="dnorm(5,1e-2)", itheta="dlnorm(1,.1)[sd]",
+                        ipsi="dlnorm(1,.1)[sd]", rho="dbeta(3,3)"),
+             jagcontrol=list(method="rjparallel"))
Found more than one class "Model" in cache; using the first, from namespace 'lavaan'
Loading required namespace: rjags
Compiling rjags model...
Starting 3 rjags simulations using a PSOCK cluster with 3 nodes on
host ‘localhost’
Simulation complete
Calculating summary statistics...
Calculating the Gelman-Rubin statistic for 42 variables....
Finished running the simulation
Computing posterior predictives...
Found more than one class "Model" in cache; using the first, from namespace 'lavaan'
Warning messages:
1: In lav_data_full(data = data, group = group, group.label = group.label,  :
  lavaan WARNING: some observed variances are (at least) a factor 1000 times larger than others; use varTable(fit) to investigate
2: In blavaan(co2.model, data = x, dp = dpriors(nu = "dnorm(5,1e-2)",  :
  blavaan WARNING: at least one parameter has a psrf > 1.2.



在 2016年5月9日星期一 UTC+8下午11:39:35,Ed Merkle写道:
Quanying,
Message has been deleted

Ed Merkle

unread,
Jun 17, 2016, 12:45:32 PM6/17/16
to blavaan
Quanying,

As the warning suggests, the scales of your variables differ greatly (say, compare y6 to the others). You might try standardizing, or some other transformation so that the variance of each variable is more similar to the others.

Ed

luquany...@gmail.com

unread,
Jun 17, 2016, 9:56:51 PM6/17/16
to blavaan
Dear Ed

Thank you very much!
I standardized the data by  logarithm. 

The model did NOT converge after 5000 adapt+burnin iterations. The error like this

Found more than one class "Model" in cache; using the first, from namespace 'lavaan'
Loading required namespace: rjags
Compiling rjags model...
Starting 3 rjags simulations using a PSOCK cluster with 3
nodes on host ‘localhost’
Simulation complete
Calculating summary statistics...
Calculating the Gelman-Rubin statistic for 39 variables....
Finished running the simulation
Computing posterior predictives...
Found more than one class "Model" in cache; using the first, from namespace 'lavaan'
Warning message:
In blavaan(co2.model, data = x, dp = dpriors(nu = "dnorm(5,1e-2)",  :
  blavaan WARNING: at least one parameter has a psrf > 1.2.
> summary(fit)
** WARNING ** blavaan (0.1-3) did NOT converge after 5000 adapt+burnin iterations
** WARNING ** Proceed with caution

I want to know how to solve this problem.
I am looking forward to your letter!


在 2016年6月18日星期六 UTC+8上午12:45:32,Ed Merkle写道:
10.csv

Ed Merkle

unread,
Jun 18, 2016, 2:45:04 PM6/18/16
to luquany...@gmail.com, blavaan
Quanying,

The lack of convergence is likely due to the particular model you specified, and it is difficult to give advice here without knowing any details. But it seems like, given your sample size, you might be limited in the models you can estimate. I would generally advise to start with simple models and build up to more complex models.

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/2f3df130-2422-4027-8563-ea416bff1b8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

luquany...@gmail.com

unread,
Jun 18, 2016, 11:34:18 PM6/18/16
to blavaan, luquany...@gmail.com
Dear Ed,

Thank you for your help!

在 2016年6月19日星期日 UTC+8上午2:45:04,Ed Merkle写道:
Reply all
Reply to author
Forward
0 new messages