Skip to contents

Extract relevant covariance indices from lavaan model through lavaan::parameterEstimates with standardized = TRUE. In this case, the correlation coefficient (r) represents the resulting std.all column.

Usage

lavaan_cov(fit, nice_table = FALSE, ...)

Arguments

fit

lavaan fit object to extract covariance indices from

nice_table

Logical, whether to print the table as a rempsyc::nice_table as well as print the reference values at the bottom of the table.

...

Arguments to be passed to rempsyc::nice_table

Value

A dataframe of covariances, including the covaried variables, the covariance, and corresponding p-value.

Examples


(latent <- list(visual = paste0("x", 1:3),
                textual = paste0("x", 4:6),
                speed = paste0("x", 7:9)))
#> $visual
#> [1] "x1" "x2" "x3"
#> 
#> $textual
#> [1] "x4" "x5" "x6"
#> 
#> $speed
#> [1] "x7" "x8" "x9"
#> 

(regression <- list(ageyr = c("visual", "textual", "speed"),
                    grade = c("visual", "textual", "speed")))
#> $ageyr
#> [1] "visual"  "textual" "speed"  
#> 
#> $grade
#> [1] "visual"  "textual" "speed"  
#> 

(covariance <- list(speed = "textual", ageyr = "grade"))
#> $speed
#> [1] "textual"
#> 
#> $ageyr
#> [1] "grade"
#> 

HS.model <- write_lavaan(regression = regression, covariance = covariance,
                         latent = latent, label = TRUE)
cat(HS.model)
#> ##################################################
#> # [-----Latent variables (measurement model)-----]
#> 
#> visual =~ x1 + x2 + x3
#> textual =~ x4 + x5 + x6
#> speed =~ x7 + x8 + x9
#> 
#> ##################################################
#> # [---------Regressions (Direct effects)---------]
#> 
#> ageyr ~ visual + textual + speed
#> grade ~ visual + textual + speed
#> 
#> ##################################################
#> # [------------------Covariances-----------------]
#> 
#> speed ~~ textual
#> ageyr ~~ grade
#> 

library(lavaan)
fit <- lavaan(HS.model, data=HolzingerSwineford1939,
              auto.var=TRUE, auto.fix.first=TRUE,
              auto.cov.lv.x=TRUE)
lavaan_cov(fit)
#>    Variable.1 Variable.2     r     p
#> 16    textual      speed 0.268 0.001
#> 17      ageyr      grade 0.533 0.000
#> 18         x1         x1 0.395 0.000
#> 19         x2         x2 0.820 0.000
#> 20         x3         x3 0.667 0.000
#> 21         x4         x4 0.268 0.000
#> 22         x5         x5 0.264 0.000
#> 23         x6         x6 0.311 0.000
#> 24         x7         x7 0.641 0.000
#> 25         x8         x8 0.437 0.000
#> 26         x9         x9 0.610 0.000
#> 27      ageyr      ageyr 0.839 0.000
#> 28      grade      grade 0.809 0.000
#> 29     visual     visual 1.000 0.000
#> 30    textual    textual 1.000 0.000
#> 31      speed      speed 1.000 0.000
#> 32     visual    textual 0.458 0.000
#> 33     visual      speed 0.438 0.000