Skip to contents

Extract relevant indirect effects indices from lavaan model through lavaan::parameterEstimates with standardized = TRUE. In this case, the beta (B) represents the resulting std.all column. See "Value" section for more details.

Usage

lavaan_ind(
  fit,
  estimate = "B",
  nice_table = FALSE,
  underscores_to_arrows = TRUE,
  ...
)

Arguments

fit

lavaan fit object to extract fit indices from

estimate

What estimate to use, either the standardized estimate ("B", default), or unstandardized estimate ("b").

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.

underscores_to_arrows

Logical, whether to convert underscore to arrows in the "Indirect Effect column".

...

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

(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"
#> 

(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_ind(fit)
#>             Indirect.Effect                       Paths     p      B CI_lower
#> 30   ageyr → visual → speed   ageyr_visual*visual_speed 0.001 -0.151   -0.236
#> 31 ageyr → visual → textual ageyr_visual*visual_textual 0.000 -0.153   -0.237
#> 32   grade → visual → speed   grade_visual*visual_speed 0.000  0.248    0.150
#> 33 grade → visual → textual grade_visual*visual_textual 0.000  0.252    0.160
#>    CI_upper
#> 30   -0.066
#> 31   -0.070
#> 32    0.345
#> 33    0.344