multilevel sem error

472 views
Skip to first unread message

Elise Deschenes

unread,
Nov 27, 2018, 8:09:03 PM11/27/18
to lavaan
Hi all,

I am trying to test a 2 level multilevel SEM, where my trees are nested within my sites. I include my path diagram. 
sm.PNG













Where RW, MAGE, RD and hautrond are measured on each tree ring, and P_et, P_hi and P_au  are climatic variables associated with each site, but they vary according to my tree rings because each ring is associated with a specific year which had a certain climate (could this complicate things?). 

My data is nested in 3 levels. My trees are nested within my sites, but I also have nesting within my trees, as a tree ring is nested within a tree and I have many rings per tree. Since lavaan can only handle 2 levels, I decided that my first level would be my ring, and my second level would be my site, as those are the two levels on which my variables are measure. Here is my model. 

model <- '
        level: 1
            RD  ~ a*RW +b*MAGE+c*hautrond
            RW  ~ g*MAGE
        level: 2
            RD  ~ d*P_hi+e*P_au+f*P_et
            RW  ~ h*P_hi+i*P_et
   
# Specification
  indirect := h*a 
  total := h*a + d         
    '

fit <- sem(model = model, data = densite, cluster = "site", verbose = TRUE)

 I am getting an error message that I do not understand. After reading up, it seems that it could be because of my model specification, having place my variables in the wrong levels. I am not at all sure, and I wonder if it is because I am trying to only take into account 2 of 4 levels. 

Error in if (fx.delta < tol) { : missing value where TRUE/FALSE needed

Does anyone know what might be the issue? 
Thank you in advance! 


Terrence Jorgensen

unread,
Nov 29, 2018, 5:55:11 AM11/29/18
to lavaan

P_et, P_hi and P_au  are climatic variables associated with each site, but they vary according to my tree rings because each ring is associated with a specific year which had a certain climate (could this complicate things?). 

If they vary within the “tree” level, then they are tree-level predictors. If you had repeated measures over time nested within human subjects, age is a “variable associated with” people, but it is not a person-level predictor because age changes within person. So you have to treat age as a time-varying predictor. Sounds like your climatic variables vary within each tree, which makes sense because climate changes over time as the trees grow.

Since lavaan can only handle 2 levels, I decided that my first level would be my ring, and my second level would be my site

Seems bad to ignore the effect of tree. How many sites do you have? You could make dummy codes to partial out the (fixed) effects of between-site variability. If you set fixed.x=TRUE and conditional.x=TRUE, lavaan() will fit the model to the residual covariance matrices after partialing out the effect of site.


I am getting an error message that I do not understand. After reading up, it seems that it could be because of my model specification, having place my variables in the wrong levels.
Error in if (fx.delta < tol) { : missing value where TRUE/FALSE needed

No, this happens because when lavaan is estimating the separate within and between components of the covariance matrix and mean vector using the EM algorithm, it is checking an internally stored value “fx” to see how much it changed (fx.delta) at the current iteration. If the change is close enough to zero (i.e., < tol), the EM algorithm as “converged”. For some reason, that value is missing (NA), so when it is checked by if (...), it returns an error because NA is neither TRUE nor FALSE.

This issue might have already been resolved in the development version, which you can install:

install.packages("lavaan", repos = "http://www.da.ugent.be", type = "source")

If the problem persists, please post your syntax and enough data to reproduce the error, so the reason for that NA can hopefully be tracked down and fixed.

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam
http://www.uva.nl/profile/t.d.jorgensen

Diana Zhu

unread,
Apr 1, 2019, 5:36:26 PM4/1/19
to lavaan
Dear Dr. Jorgensen,

I got the same error message as Elise when running a simple multi-level analysis with simulated data. I downloaded the development version but the error message still showed up. Below is my code. Do you by any chance know when will the error message issue be resolve?

> ####model####
> model1 = '
+ level:1
+ F1 =~ Y1 + Y2 + Y3
+ F2 =~ Y4 + Y5 + Y6
+ level:2
+ F1 =~ Y1 + Y2 + Y3
+ F2 =~ Y4 + Y5 + Y6'
> estimate1 = sem(model=model1, data=simdata, cluster = "group", std.lv=TRUE, orthogonal=TRUE)

Thank you!

Diana Zhu

Yves Rosseel

unread,
Apr 2, 2019, 9:25:34 AM4/2/19
to lav...@googlegroups.com
Could you end me the simulated data? That will allow me to investigate
what is causing this, and hopefully fix it.

Yves.

On 4/1/19 11:36 PM, Diana Zhu wrote:
> Dear Dr. Jorgensen,
>
> I got the same error message as Elise when running a simple multi-level
> analysis with simulated data. I downloaded the development version but
> the error message still showed up. Below is my code. Do you by any
> chance know when will the error message issue be resolve?
>
> > ####model####
> > model1 = '
> + level:1
> + F1 =~ Y1 + Y2 + Y3
> + F2 =~ Y4 + Y5 + Y6
> + level:2
> + F1 =~ Y1 + Y2 + Y3
> + F2 =~ Y4 + Y5 + Y6'
> >
> > estimate1 = sem(model=model1, data=simdata, cluster = "group",
> std.lv=TRUE, orthogonal=TRUE)
>
> Thank you!
>
> Diana Zhu
> On Thursday, November 29, 2018 at 5:55:11 AM UTC-5, Terrence Jorgensen
> wrote:
>
> P_et, P_hi and P_au  are climatic variables associated with each
> site, but they vary according to my tree rings because each ring
> is associated with a specific year which had a certain climate
> (could this complicate things?).
>
> If they vary within the “tree” level, then they are tree-level
> predictors. If you had repeated measures over time nested within
> human subjects, age is a “variable associated with” people, but it
> is not a person-level predictor because age changes within person.
> So you have to treat age as a time-varying predictor. Sounds like
> your climatic variables vary within each tree, which makes sense
> because climate changes over time as the trees grow.
>
> Since lavaan can only handle 2 levels, I decided that my first
> level would be my ring, and my second level would be my site
>
> Seems bad to ignore the effect of tree. How many sites do you have?
> You could make dummy codes to partial out the (fixed) effects of
> between-site variability. If you set |fixed.x=TRUE| and
> |conditional.x=TRUE|, |lavaan()| will fit the model to the residual
> covariance matrices after partialing out the effect of site.
>
>
> I am getting an error message that I do not understand. After
> reading up, it seems that it could be because of my model
> specification, having place my variables in the wrong levels.
> Error in if (fx.delta < tol) { : missing value where TRUE/FALSE
> needed
>
> No, this happens because when lavaan is estimating the separate
> within and between components of the covariance matrix and mean
> vector using the EM algorithm, it is checking an internally stored
> value “fx” to see how much it changed (fx.delta) at the current
> iteration. If the change is close enough to zero (i.e., < tol), the
> EM algorithm as “converged”. For some reason, that value is missing
> (NA), so when it is checked by |if (...)|, it returns an error
> because NA is neither TRUE nor FALSE.
>
> This issue might have already been resolved in the development
> version, which you can install:
>
> |
> install.packages("lavaan",repos ="http://www.da.ugent.be",type
> ="source")
> |
>
> If the problem persists, please post your syntax and enough data to
> reproduce the error, so the reason for that NA can hopefully be
> tracked down and fixed.
>
> Terrence D. Jorgensen
> Assistant Professor, Methods and Statistics
> Research Institute for Child Development and Education, the
> University of Amsterdam
> http://www.uva.nl/profile/t.d.jorgensen
> <http://www.uva.nl/profile/t.d.jorgensen>
>
> ​
>
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To post to this group, send email to lav...@googlegroups.com
> <mailto:lav...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/lavaan.
> For more options, visit https://groups.google.com/d/optout.

Nadya Majeed

unread,
Oct 28, 2024, 3:40:22 AM10/28/24
to lavaan
Hi all,

Sorry to revive this old thread—facing a similar issue (using lavaan version 0.6-19) and this is the closest thing I could find online.

I'm trying to run multilevel SEM (two levels) with three variables (X1 + X2 --> Y, or Y ~ X1 + X2), each variable having three observed indicators, and each cluster (1000 clusters in total) having 20 rows of data. Here's my model:

  level: 1
  Ydi =~ Ydi_i1 + Ydi_i2 + Ydi_i3
  X1di =~ X1di_i1 + X1di_i2 + X1di_i3
  X2di =~ X2di_i1 + X2di_i2 + X2di_i3
  Ydi ~ X1di + X2di
  level: 2
  Ydi =~ Ydi_i1 + Ydi_i2 + Ydi_i3
  X1di =~ X1di_i1 + X1di_i2 + X1di_i3
  X2di =~ X2di_i1 + X2di_i2 + X2di_i3
  Ydi ~ X1di + X2di


However I'm getting the fx.delta error ("Error in if (fx.delta < 0) { : missing value where TRUE/FALSE needed") when trying to analyse simulated data. I'm attaching one of my example simulated datasets + code to help with troubleshooting, but it happens with many datasets, not just this one specifically.

Thanks!

Nadya
test.csv
test.R

Yves Rosseel

unread,
Oct 28, 2024, 3:45:04 AM10/28/24
to lav...@googlegroups.com
Hello Nadya,

I don't have a solution yet, but at least you provided a reprex! Thanks
for that. I will investigate this, and report back.

Yves.
> > std.lv <http://std.lv>=TRUE, orthogonal=TRUE)
> <https://groups.google.com/group/lavaan>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion visit
> https://groups.google.com/d/msgid/lavaan/d59d0c6e-9f2f-4029-b667-347b5e9f5b6fn%40googlegroups.com <https://groups.google.com/d/msgid/lavaan/d59d0c6e-9f2f-4029-b667-347b5e9f5b6fn%40googlegroups.com?utm_medium=email&utm_source=footer>.

Yves Rosseel

unread,
Oct 28, 2024, 5:52:33 AM10/28/24
to lav...@googlegroups.com
I think I have found the cause: the indicators of the same factor are
perfectly correlated:

> cor(testData)
ID Ydi_i1 Ydi_i2 Ydi_i3 X1di_i1
ID 1.000000000 -0.01150182 -0.01150182 -0.01150182 -0.03022818
Ydi_i1 -0.011501823 1.00000000 1.00000000 1.00000000 0.41192634
Ydi_i2 -0.011501823 1.00000000 1.00000000 1.00000000 0.41192634
Ydi_i3 -0.011501823 1.00000000 1.00000000 1.00000000 0.41192634
X1di_i1 -0.030228178 0.41192634 0.41192634 0.41192634 1.00000000
X1di_i2 -0.030228178 0.41192634 0.41192634 0.41192634 1.00000000
X1di_i3 -0.030228178 0.41192634 0.41192634 0.41192634 1.00000000
X2di_i1 0.009349751 0.41347929 0.41347929 0.41347929 -0.01316847
X2di_i2 0.009349751 0.41347929 0.41347929 0.41347929 -0.01316847
X2di_i3 0.009349751 0.41347929 0.41347929 0.41347929 -0.01316847
X1di_i2 X1di_i3 X2di_i1 X2di_i2 X2di_i3
ID -0.03022818 -0.03022818 0.009349751 0.009349751 0.009349751
Ydi_i1 0.41192634 0.41192634 0.413479285 0.413479285 0.413479285
Ydi_i2 0.41192634 0.41192634 0.413479285 0.413479285 0.413479285
Ydi_i3 0.41192634 0.41192634 0.413479285 0.413479285 0.413479285
X1di_i1 1.00000000 1.00000000 -0.013168473 -0.013168473 -0.013168473
X1di_i2 1.00000000 1.00000000 -0.013168473 -0.013168473 -0.013168473
X1di_i3 1.00000000 1.00000000 -0.013168473 -0.013168473 -0.013168473
X2di_i1 -0.01316847 -0.01316847 1.000000000 1.000000000 1.000000000
X2di_i2 -0.01316847 -0.01316847 1.000000000 1.000000000 1.000000000
X2di_i3 -0.01316847 -0.01316847 1.000000000 1.000000000 1.000000000

This results in singular matrices, causing the (uninformative) fx.delta
error message.

You need to revise the way you generate the data. Did you add random
noise to the indicators?

On the lavaan side: lavaan should check for 'perfect correlations' in
the input data (something that usually never happens in practice; only
in simulated data).

Yves.

On 10/28/24 5:52 AM, Nadya Majeed wrote:
> > std.lv <http://std.lv>=TRUE, orthogonal=TRUE)
> <https://groups.google.com/group/lavaan>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages