Which group? Lavaan unclear as to which group it refers to

66 views
Skip to first unread message

Nikola Ćirović

unread,
Nov 22, 2024, 2:08:36 PM11/22/24
to lavaan
Dear all, 
I am confused as to which group lavaan refers to as one with a non-positive latent covariance matrix. Here is the output below. It says group 2 but lists group 1 as second...
In my previous post about this, colleagues were mixed up in terms of which of the correlation matrices includes the non-positive problem so I am posting again with this problem in mind specifically.

> fit.model.factorFIScon <- cfa (factorFIS4, data =InsideMe_identitet_SVE_sirovo_sredjeno,
+                                std.lv=T,  estimator='mlr', group="pol", missing= 'fiml')

Warning messages:

1: lavaan->lav_data_full():  

   group variable ‘pol’ contains missing values 

2: lavaan->lav_object_post_check():  

   covariance matrix of latent variables is not positive definite in group 2; use 

   lavInspect(fit, "cov.lv") to investigate. 


> summary(fit.model.factorFIScon,

+         fit.measures = TRUE,

+         standardize = TRUE, 

+         rsquare = TRUE)

lavaan 0.6-19 ended normally after 83 iterations

  Estimator                                         ML

  Optimization method                           NLMINB

  Number of model parameters                       114

  Number of observations per group:                   

    2                                              301 (female group)

    1                                               80 (male group)

  Number of missing patterns per group:               

    2                                                5

    1                                                1


As you can see, lavaan lists the groups in the reverse order and tells me that the group 2 is a problem. Which one is it then?

Here is the latent covariance matrix for each group:

> lavInspect(fit.model.factorFIScon, "cov.lv")
$`2`
                                 Future Prsn_C Strctr Harmny Goals
Future                     1.000                           
Personal_Control  0.861  1.000                    
Structure                0.662  0.588  1.000             
Harmony                0.706  0.866  0.672  1.000      
Goals                      0.880  0.975  0.547  0.743 1.000

$`1`
                                  Future Prsn_C Strctr Harmny Goals
Future                      1.000                           
Personal_Control   0.882  1.000                    
Structure                 0.675  0.868  1.000             
Harmony                 0.850  0.913  0.731  1.000      
Goals                       0.882  0.902  0.617  0.682 1.000

Kind regards, Nikola

Shu Fai Cheung (張樹輝)

unread,
Nov 22, 2024, 8:07:09 PM11/22/24
to lavaan
The order of the groups is determined by the order of appearance of the group labels in the data.

For the warning, the group number (group 1, group 2, etc.) refers to the position of a group in this order (the first group, the second group, etc.), not to the group label.

This is an illustration:

``` r
library(lavaan)
#> This is lavaan 0.6-19
#> lavaan is FREE software! Please report any bugs.
mod_pop <-
"
f1 =~ c(.7, .7)*x11 + c(.7, .7)*x12 + c(.7, .7)*x13
f2 =~ c(.7, .7)*x21 + c(.7, .7)*x22 + c(.7, .7)*x23
f3 =~ c(.7, .7)*x31 + c(.7, .7)*x32 + c(.7, .7)*x33
f4 =~ c(.7, .7)*x41 + c(.7, .7)*x42 + c(.7, .7)*x43
f4 ~~ c(.8, .3)*f1 + c(.8, .3)*f2 + c(.8, .3)*f3
f3 ~~ c(.8, .3)*f1 + c(.8, .3)*f2
f2 ~~ c(.8, .3)*f1
"
dat <- simulateData(model = mod_pop,
                    sample.nobs = c(50, 150),
                    seed = 1234)
dat$group <- ifelse(dat$group == 1, "GpA", "GpB")
print(head(dat), digits = 1)
#>     x11  x12  x13   x21  x22   x23  x31   x32  x33  x41  x42    x43 group
#> 1  1.99  2.5  1.2 -0.01  0.7  0.02  1.0 -0.19  1.1  1.6 -1.7  2.009   GpA
#> 2 -1.21  0.7  0.9 -0.29  0.7 -0.64  0.2  0.40 -1.5 -1.3 -0.9  0.536   GpA
#> 3  0.02  0.1 -0.5 -0.57  0.4 -0.91 -1.3 -1.68 -2.5 -0.2 -2.1 -0.006   GpA
#> 4  1.65  2.8  2.2  2.13  0.8  1.10  2.9  1.25  1.9  1.6  0.6  1.029   GpA
#> 5  0.88  0.2 -1.8 -0.75  0.8  0.02 -0.9 -0.02  0.1  0.4 -1.6 -1.020   GpA
#> 6 -1.03 -0.2 -0.8  0.07 -0.3  0.86 -1.5 -0.69 -0.7 -0.6  0.6  0.062   GpA

mod <-
"
f1 =~ x11 + x12 + x13
f2 =~ x21 + x22 + x23
f3 =~ x31 + x32 + x33
f4 =~ x41 + x42 + x43
"
fit1 <- cfa(mod,
            data = dat,
            group = "group",
            bounds = "standard")
#> Warning: lavaan->lav_object_post_check():  
#>    covariance matrix of latent variables is not positive definite in group 1;
#>    use lavInspect(fit, "cov.lv") to investigate.
lavInspect(fit1, "cor.lv")
#> $GpA
#>       f1    f2    f3    f4
#> f1 1.000                  
#> f2 0.811 1.000            
#> f3 0.471 0.383 1.000      
#> f4 0.630 1.024 1.114 1.000
#>
#> $GpB
#>       f1    f2    f3    f4
#> f1 1.000                  
#> f2 0.488 1.000            
#> f3 0.122 0.519 1.000      
#> f4 0.255 0.256 0.204 1.000

# Group 2 as the first group
dat2 <- dat[rev(seq_len(nrow(dat))), ]
print(head(dat2), digits = 1)
#>      x11   x12  x13   x21  x22  x23  x31  x32  x33  x41   x42  x43 group
#> 200 -1.5 -0.83 -1.1 -0.73  0.1 -0.7  0.5 -1.1  1.3 -1.3 -1.26 0.74   GpB
#> 199 -0.7 -0.77 -2.7 -0.04  1.0  0.6  0.9  0.4  2.0 -1.6 -0.02 0.18   GpB
#> 198  1.4 -0.78 -0.6 -1.14 -1.8 -1.0  2.0  1.3  0.3 -0.6 -0.09 0.69   GpB
#> 197 -0.5  0.23 -2.5 -2.21 -0.2 -2.8 -1.1  0.2 -1.8 -1.5  0.17 0.02   GpB
#> 196 -0.1 -0.03 -0.8 -1.26 -1.2  0.7  1.0  0.9 -3.2  1.4  1.41 0.91   GpB
#> 195  2.8  0.21  1.2  0.87  0.8  1.7 -1.1 -0.5 -1.7  1.7  0.91 2.66   GpB
fit2 <- cfa(mod,
            data = dat2,
            group = "group",
            bounds = "standard")
#> Warning: lavaan->lav_object_post_check():  
#>    covariance matrix of latent variables is not positive definite in group 2;
#>    use lavInspect(fit, "cov.lv") to investigate.
lavInspect(fit2, "cor.lv")
#> $GpB
#>       f1    f2    f3    f4
#> f1 1.000                  
#> f2 0.488 1.000            
#> f3 0.122 0.519 1.000      
#> f4 0.255 0.256 0.204 1.000
#>
#> $GpA
#>       f1    f2    f3    f4
#> f1 1.000                  
#> f2 0.811 1.000            
#> f3 0.471 0.383 1.000      
#> f4 0.630 1.024 1.114 1.000
```


Hope this helps.

-- Shu Fai

Nikola Ćirović

unread,
Nov 23, 2024, 4:18:42 AM11/23/24
to lavaan
Dear Shu Fai,
Thank you! However, I do not fully understand - is it my male or female group then? I am not sure, please tell me.
Also, I answered your reply to my previous post on the non-positive latent covariance matrix and what you suggested actually worked!
Kind regards,
Nikola

Nikola Ćirović

unread,
Nov 23, 2024, 6:00:39 AM11/23/24
to lavaan
Dear  Shu Fai, I finally understood the comment you made and changed the order in the data to check my understanding and it is the male group - just like you suggested in the previous post. Again, I did what you suggested in the previous post (on the non-positive definite latent covariance matrix) and it worked although I am not sure how do I explain what happened.

Shu Fai Cheung (張樹輝)

unread,
Nov 23, 2024, 8:08:26 AM11/23/24
to lavaan
I rarely read papers that applied SEM explained why non-positive definite covariance matrix happened.

As explained by others, this may be merely due to sampling error. In my demonstration of the group order, the model fitted is a true model because I know the data generation model. However, due to the small sample size in one group, I could artificially find a possible sample that yields a non-positive definite covariance matrix for the latent variables, as in your case.

Though not ideal, you probably can suggest that that warning may be due to the small sample size in one group. Therefore, when the full sample is used to estimate the parameters, the warning disappears.

However, note that, as far as I know, it is not common to constrain the factor variances and covariances to be equal between groups  (though this can be a desirable model in some cases) because this is a very strict form of equivalence (and so may usually fail to fit well). You may need to see whether you can justify these constraints theoretically.

Hope this helps.

-- Shu Fai

Nikola Ćirović

unread,
Nov 23, 2024, 8:37:09 AM11/23/24
to lavaan
Dear Professor Shu Fai, 
Thank you! Everything you wrote on this and my previous post has been helpful! The reviewer asked for a methods literature reference for this but I was only able to find references about Heywood cases not for my particular case, and that is why I needed inputs like yours to clarify what happened. I never read about a similar case in the research literature and was clueless about what to do.
The model with equal variances and covariances of factors, though demanding as you said, fitted well and I interpreted this to mean that multicollinearity exists equally in both groups and took this as a theoretical issue (concerning the instrument and underlying constructs) as you suggested previously and not a SEM issue per see.
Thank you once again!
Kind regards, 
Nikola
Reply all
Reply to author
Forward
0 new messages