lavaan syntax for covariances among a large set of factors in CFA

51 views
Skip to first unread message

MD Oliur Rahman Tarek

unread,
Oct 16, 2024, 4:50:13 AM (10 days ago) Oct 16
to lavaan
Dear all, 

I wanted to know if there is any shortcut in lavaan to indicate freely-estimated factor covariances among 20 latent factors (4 factors across 5 waves) without writing the syntax manually, when combined with orthogonal=T command? 

I know that factor covariances are default in the cfa() function, but I also have "method" factors to accommodate reverse-keyed items and the correlated residuals of the same item(s) across five waves. I wanted to first use orthogonal=T command to restrict covariances among all factors in the model and then manually write syntax to indicate freely estimated covariances among the 20 "main" latent factors. But this would mean that I have to write a long syntax, which may be possible using a list-like shortcut? 

Doing the opposite -- allowing freely-estimated covariances as per the default and then manually restrict covariances with the "method factors" -- would also require to write a long syntax. 

Any suggestion would be very helpful. 

Best regards, 
Oliur 

Daniel Morillo Cuadrado

unread,
Oct 16, 2024, 5:46:58 AM (10 days ago) Oct 16
to lav...@googlegroups.com
You could do it by writing the syntax programmatically with R code. The {glue} package may be specially handy for such a task.

We (me, or someone else in the list for sure) can provide extra help if you provide a minimal syntax example with at least the variables in one wave.

--
Daniel Morillo, Ph.D.
GitHub | ORCID


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/63ba2e05-4ef3-48b6-a8e2-4710c4cfb463n%40googlegroups.com.

MD Oliur Rahman Tarek

unread,
Oct 16, 2024, 6:37:27 AM (10 days ago) Oct 16
to lavaan
Dear Dr. Morillo, 

Many thanks! I have provided the model syntax below for all waves. AC, NC, HS, and LA are the four factors across five waves and I would like these factors to have covariances among them. 

AC_t1 =~ NA*UN08_01 + UN08_02 + UN08_03
AC_t2 =~ NA*UN08_012 + UN08_022 + UN08_032
AC_t3 =~ NA*UN08_013 + UN08_023 + UN08_033
AC_t4 =~ NA*UN08_014 + UN08_024 + UN08_034
AC_t5 =~ NA*UN08_015 + UN08_025 + UN08_035


NC_t1 =~ NA*UN09_01 + UN09_02 + UN09_03
NC_t2 =~ NA*UN09_012 + UN09_022 + UN09_032
NC_t3 =~ NA*UN09_013 + UN09_023 + UN09_033
NC_t4 =~ NA*UN09_014 + UN09_024 + UN09_034
NC_t5 =~ NA*UN09_015 + UN09_025 + UN09_035


HS_t1 =~ NA*UN10_01 + UN10_02 + UN10_03
HS_t2 =~ NA*UN10_012 + UN10_022 + UN10_032
HS_t3 =~ NA*UN10_013 + UN10_023 + UN10_033
HS_t4 =~ NA*UN10_014 + UN10_024 + UN10_034
HS_t5 =~ NA*UN10_015 + UN10_025 + UN10_035


LA_t1 =~ NA*UN10_04 + UN10_05
LA_t2 =~ NA*UN10_042 + UN10_052
LA_t3 =~ NA*UN10_043 + UN10_053
LA_t4 =~ NA*UN10_044 + UN10_054
LA_t5 =~ NA*UN10_045 + UN10_055

A simple explanation of how to achieve the covariances using the "glue" package would be great :)

Best regards, 
Oliur

Shu Fai Cheung (張樹輝)

unread,
Oct 17, 2024, 6:41:51 PM (8 days ago) Oct 17
to lavaan
Hi,

I happened to have encountered something similar in my work. You can generate syntax for the factor covariances using base R (jusb combn() and paste()).

I drafted the following before reading your sample code but I think you will be able to adapt the following code to your model.

``` r
library(lavaan)
#> This is lavaan 0.6-19
#> lavaan is FREE software! Please report any bugs.
library(psych)
#>
#> Attaching package: 'psych'
#> The following object is masked from 'package:lavaan':
#>
#>     cor2cov

mod <-
"
A =~ A1 + A2 + A3 + A4 + A5
C =~ C1 + C2 + C3 + C4 + C5
E =~ E1 + E2 + E3 + E4 + E5
N =~ N1 + N2 + N3 + N4 + N5
O =~ O1 + O2 + O3 + O4 + O5
"

# Generate pairwise combination
tmp <- combn(c("A", "E", "N"),
             m = 2,
             simplify = FALSE)
tmp
#> [[1]]
#> [1] "A" "E"
#>
#> [[2]]
#> [1] "A" "N"
#>
#> [[3]]
#> [1] "E" "N"
# Generate the covariance syntax for covariances
tmp2 <- sapply(tmp,
          function(x) {
              paste(x[1], "~~", x[2])
            })
tmp2
#> [1] "A ~~ E" "A ~~ N" "E ~~ N"

# Combine it with the original model syntax
# If c() does not work, just use this
# paste(mod, "\n", paste(tmp2, collapse = "\n"))

fit <- cfa(c(mod,
             tmp2),
           data = bfi,
           orthogonal = TRUE)
lavInspect(fit, "cor.lv")
#>       A     C     E     N     O
#> A 1.000                        
#> C 0.000 1.000                  
#> E 0.680 0.000 1.000            
#> N 0.224 0.000 0.249 1.000      
#> O 0.000 0.000 0.000 0.000 1.000

# You can also print the output to copy-and-paste
cat(paste(tmp2, collapse = "\n"), "\n")
#> A ~~ E
#> A ~~ N
#> E ~~ N
```

<sup>Created on 2024-10-18 with [reprex v2.1.1](https://reprex.tidyverse.org)</sup>


Hope this helps.

-- Shu Fai
Reply all
Reply to author
Forward
0 new messages