An apparent bug in lavaan?

40 views
Skip to first unread message

Darren Johnson

unread,
Apr 30, 2019, 12:33:00 PM4/30/19
to lavaan
    It appears in the lavaan example on R works and matches lme4 results(first part of program)
In my example I show what lavaan and what lme4 did.  They should be the same.  Why not?
I explained what lavaan did with respect to lme4 to explain the mismatch.  
File and code included.
I made it as brief and easy to follow as possible.

Dr. Darren Johnson
CNT
700 Cajun Dome Blvd
Lafayette, LA 70503

lavaan_bug.R
Anderson.plot.data_trimmedTo9.csv

Alex Schoemann

unread,
Apr 30, 2019, 12:57:38 PM4/30/19
to lavaan
Hi Darren,

MSEM (what lavaan is using) and MLM (what lmer is using) will provide identical results when level 1 variables have no variance across level 2 units (this is the case for x1 and x2 in the Demo.twolevel data set). If there is variance at both level 1 and level 2 in a level 1 predictor (uncentered) MLM slopes conflate level 1 and level 2 effects, but MSEM seperates level 1 and level 2 effects. I'm guessing this is what is happening in your data. By group mean centering any level 1 predictors you eliminate level 2 variance in the predictor and MLM and MSEM results should match.

Alex

Darren Johnson

unread,
Apr 30, 2019, 1:04:53 PM4/30/19
to lavaan
So the variance between the x's and w's is almost zero.
In my example grass is highly correlated to temp and insol.
So the solution is to mean center the grass and npk to solve the problem.  Correct?
Correlation of Fixed Effects:
   (Intr) x1     x2     w1   
x1  0.002                    
x2  0.003  0.006             
w1 -0.036 -0.008 -0.014      
w2  0.090 -0.006  0.005  0.144


Correlation of Fixed Effects:
      (Intr) grass  npk    temp 
grass -0.057                    
npk   -0.001 -0.114             
temp   0.882  0.049  0.064      
insol -0.995  0.018 -0.013 -0.917

Alex Schoemann

unread,
Apr 30, 2019, 1:57:33 PM4/30/19
to lavaan
The issue isn't the covariance of the predictors (though what you're reporting is the covariance of the slopes of the predictors, which is a different quantity). The issue really is the ICC of the level 1 predictors (in your case grass and npk) to the degree the ICC of your level 1 predictors is greater than 0 the MLM and MSEM results will diverge.

If you group mean center grass and npk, I would expect lavaan and lmer to match.

Darren Johnson

unread,
Apr 30, 2019, 3:20:22 PM4/30/19
to lavaan
I noticed that when the fixed correlation matrix is smaller, that the methods match.
Would it be correct to say that lavaan is better than lmer in that it adjusts for the intraclass correlation before making estimates
"but MSEM seperates level 1 and level 2 effects", hence I don't have to worry about this problem. 
I now I would still have to look at the predictions, etc..

Darren Johnson

unread,
Apr 30, 2019, 3:22:17 PM4/30/19
to lavaan

I noticed that when the fixed correlation matrix is smaller, that the methods match.
Would it be correct to say that lavaan is better than lmer in that it adjusts for the intraclass correlation before making estimates
"but MSEM seperates level 1 and level 2 effects", hence I don't have to worry about this problem. 
I now I would still have to look at the predictions, etc..

On Tuesday, April 30, 2019 at 12:57:33 PM UTC-5, Alex Schoemann wrote:

Yves Rosseel

unread,
Apr 30, 2019, 5:03:43 PM4/30/19
to lav...@googlegroups.com
I believe the source of the mismatch is the fact that 'temp' and 'nsol'
seem to vary within sites 6 and 10:

site '6' (first column is temp, second is nsol):

[46,] 13.6 8.161525 6
[47,] 13.6 8.161525 6
[48,] 13.6 8.161525 6
[49,] 13.6 8.161525 6
[50,] 7.4 7.577625 6
[51,] 7.4 7.577625 6
[52,] 7.4 7.577625 6
[53,] 7.4 7.577625 6
[54,] 7.4 7.577625 6

site '10':

[64,] 13.9 8.188484 10
[65,] 17.3 8.707429 10
[66,] 17.3 8.707429 10
[67,] 17.3 8.707429 10
[68,] 17.3 8.707429 10
[69,] 17.3 8.707429 10
[70,] 17.3 8.707429 10
[71,] 17.3 8.707429 10
[72,] 17.3 8.707429 10

Perhaps this is an error in the data?

For lmer, this does not matter, as no distinction is made between
level-1 or level-2 variables. For lavaan, level-2 variables are the
means (within cluster) only.

A warning should be in order though.

Yves.



On 4/30/19 9:22 PM, Darren Johnson wrote:
>
> I noticed that when the fixed correlation matrix is smaller, that the
> methods match.
> Would it be correct to say that lavaan is better than lmer in that it
> adjusts for the intraclass correlation before making estimates
> "but MSEM seperates level 1 and level 2 effects", hence I don't have to
> worry about this problem.
> I now I would still have to look at the predictions, etc..
>
> On Tuesday, April 30, 2019 at 12:57:33 PM UTC-5, Alex Schoemann wrote:
>
> The issue isn't the covariance of the predictors (though what you're
> reporting is the covariance of the /slopes /of the predictors, which
> --
> 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.

Darren Johnson

unread,
May 1, 2019, 10:00:39 AM5/1/19
to lavaan
Problem fixed, you are a genius!!!

Darren Johnson

unread,
May 1, 2019, 10:12:19 AM5/1/19
to lavaan
Yves Russell solved it.
Alex, thanks for the help.
Darren
"I believe the source of the mismatch is the fact that 'temp' and 'nsol'
seem to vary within sites 6 and 10:"




On Tuesday, April 30, 2019 at 12:57:33 PM UTC-5, Alex Schoemann wrote:
Reply all
Reply to author
Forward
0 new messages