Extract relevant indices from lavaan model based on specified operator
Source:R/lavaan_extract.R
lavaan_extract.Rd
Extract relevant indices from lavaan model through lavaan::parameterEstimates and lavaan::standardizedsolution.
Usage
lavaan_extract(
fit,
operator = NULL,
lhs_name = "Left-Hand Side",
rhs_name = "Right-Hand Side",
underscores_to_symbol = "→",
diag = NULL,
nice_table = FALSE,
...
)
Arguments
- fit
lavaan fit object to extract fit indices from
- operator
Which operator to subselect with.
- lhs_name
Name of first column, referring to the left-hand side expression (lhs).
- rhs_name
Name of first column, referring to the right-hand side expression (rhs).
- underscores_to_symbol
Character to convert underscores to arrows in the first column, like for indirect effects. Default to the right arrow symbol, but can be set to NULL or "_", or to any other desired symbol. It is also possible to provide a vector of replacements if they they are not all the same.
- diag
When extracting covariances (
~~
), whether to include or exclude diagonal values (one of "exclude" or "include").- 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, including the indirect effect ("lhs"), corresponding paths ("rhs"), standardized regression coefficient ("std.all"), corresponding p-value, as well as the unstandardized regression coefficient ("est") and its confidence interval ("ci.lower", "ci.upper").
Examples
x <- paste0("x", 1:9)
(latent <- list(
visual = x[1:3],
textual = x[4:6],
speed = x[7:9]
))
#> $visual
#> [1] "x1" "x2" "x3"
#>
#> $textual
#> [1] "x4" "x5" "x6"
#>
#> $speed
#> [1] "x7" "x8" "x9"
#>
(mediation <- list(
speed = "visual",
textual = "visual",
visual = c("ageyr", "grade")
))
#> $speed
#> [1] "visual"
#>
#> $textual
#> [1] "visual"
#>
#> $visual
#> [1] "ageyr" "grade"
#>
(indirect <- list(
IV = c("ageyr", "grade"),
M = "visual",
DV = c("speed", "textual")
))
#> $IV
#> [1] "ageyr" "grade"
#>
#> $M
#> [1] "visual"
#>
#> $DV
#> [1] "speed" "textual"
#>
HS.model <- write_lavaan(mediation,
indirect = indirect,
latent = latent, label = TRUE
)
cat(HS.model)
#> ##################################################
#> # [-----Latent variables (measurement model)-----]
#>
#> visual =~ x1 + x2 + x3
#> textual =~ x4 + x5 + x6
#> speed =~ x7 + x8 + x9
#>
#> ##################################################
#> # [-----------Mediations (named paths)-----------]
#>
#> speed ~ visual_speed*visual
#> textual ~ visual_textual*visual
#> visual ~ ageyr_visual*ageyr + grade_visual*grade
#>
#> ##################################################
#> # [--------Mediations (indirect effects)---------]
#>
#> ageyr_visual_speed := ageyr_visual * visual_speed
#> ageyr_visual_textual := ageyr_visual * visual_textual
#> grade_visual_speed := grade_visual * visual_speed
#> grade_visual_textual := grade_visual * visual_textual
#>
library(lavaan)
fit <- sem(HS.model, data = HolzingerSwineford1939)
lavaan_extract(fit, lhs_name = "Indirect Effect", operator = ":=")
#> Indirect Effect Right-Hand Side SE Z
#> 30 ageyr → visual → speed ageyr_visual*visual_speed 0.02808889 -3.198387
#> 31 ageyr → visual → textual ageyr_visual*visual_textual 0.04191650 -3.461890
#> 32 grade → visual → speed grade_visual*visual_speed 0.07291514 4.257496
#> 33 grade → visual → textual grade_visual*visual_textual 0.10134908 4.947490
#> p b CI_lower CI_upper B CI_lower_B
#> 30 1.381987e-03 -0.08983914 -0.1448924 -0.03478593 -0.1508037 -0.2358595
#> 31 5.363956e-04 -0.14511033 -0.2272652 -0.06295550 -0.1534909 -0.2371048
#> 32 2.067294e-05 0.31043593 0.1675249 0.45334698 0.2477787 0.1503789
#> 33 7.517664e-07 0.50142352 0.3027830 0.70006406 0.2521937 0.1601663
#> CI_upper_B
#> 30 -0.06574796
#> 31 -0.06987694
#> 32 0.34517843
#> 33 0.34422119