Skip to contents

Helper functions to select models (parameter tables) based on various criteria.

Usage

have_pars_all(
  partables,
  pars = NULL,
  output = c("models", "partables", "logical")
)

have_pars_any(
  partables,
  pars = NULL,
  output = c("models", "partables", "logical")
)

have_pars_none(
  partables,
  pars = NULL,
  output = c("models", "partables", "logical")
)

remove_x_y_ecov(
  partables,
  output = c("models", "partables", "logical"),
  cl = NULL
)

must_not_be_y(
  partables,
  vars = NULL,
  output = c("models", "partables", "logical")
)

must_be_y(partables, vars = NULL, output = c("models", "partables", "logical"))

must_not_have_paths(
  partables,
  y_on_x = NULL,
  output = c("models", "partables", "logical")
)

must_have_paths(
  partables,
  y_on_x = NULL,
  output = c("models", "partables", "logical")
)

Arguments

partables

A list of parameter tables, such as a partables or eq_partables object. It can also be the output of partables_plots().

pars

A character vector of lavaan model syntax that can be converted to a parameter table. It can be a vector of parameters, such as c("y ~ x", "m ~~ x"), but can also be of other forms as long as lavaan::lavParseModelString() can process it. Covariances such as "m ~~ x" and "x ~~ m" are treated as the same and so only one of them is necessary.

output

The type of output. If "models" or "partables", the output is a subset of the partables, which can be a list of parameter tables or a list of plots of parameter tables. If "logical", a logical vector of the same length as partables is returned to indicate models that match the selection criteria.

cl

A cluster created by parallel::makeCluster(). Used internally. Do not set this argument.

vars

A character vector of variables to be checked.

y_on_x

A character vector of pairs of variables, specified as "y ~ x", for which paths will be checked.

Value

Each of the select functions, by default, returns a list of parameter tables (models) that meet the criterion of that function, or a list of plots of the parameter tables if partables is the output of partables_plots(). If output is set to "logical", each of these functions returns a logical vector to indicate models or plots that meet the criterion. The vector can then be used to extract objects meeting the criterion.

Details

The select functions can be used both for a list of models in the form of parameter tables (the output of lavaan::parameterTable()) or a list of plots based on these models, generated by partables_plots(). They return an object of the same type. Therefore, they can be used to filter both models and plots of models.

Select by free parameters

The function have_pars_all() selects models that have all the free parameters specified in pars.

The function have_pars_any() identifies models that have any of the free parameters specified in pars.

The function have_pars_none() identifies models that have none of the free parameters specified in pars.

Select by covariances with an error term

The function remove_x_y_ecov() removes models from partables that have a covariance between an observed or latent variable (or its error term) and the error term of another variable it predicts, either directly or indirectly.

Select by the role of a variable

The function must_not_be_y() keeps only models with selected variables (observed or latent) not appearing as the outcome in a regression equation (indicators not counted). They are defined as variables not in "eqs.y" as returned by lavaan::lavNames().

The function must_be_y() keeps only models with selected variables (observed or latent) appearing as the outcome in at least one regression equation (indicators not counted). They are defined as variables in "eqs.y" as returned by lavaan::lavNames().

Select by paths

The function must_not_have_paths() keeps only models that do not have any paths, direct or indirect, between selected pairs of variables.

The function must_have_paths() keeps only models with at least one path, direct or indirect, between selected pairs of variables.

Examples


library(lavaan)

# Model 1

mod1 <-
"
fx =~ x1 + x2 + x3
fm =~ m1 + m2 + m3
fy =~ y1 + y2 + y3
fm ~ fx
fy ~ fm + fx
"
fit1 <- sem(
          model = mod1,
          data = data_test_3_factor_3_item
        )
fit1_1_more <- drop_k(fit1)
fit1_1_more_1_less <- lapply(
  fit1_1_more,
  add_k
)

partables1 <- combine_partables(fit1_1_more_1_less)
partables1
#> 
#> Number of models: 4
#> 
#> The models:
#> 
#>   Model                  
#> 1 drop: fm~fx.add: fx~~fm
#> 2 drop: fm~fx.add: fx~fm 
#> 3 drop: fy~fm.add: fm~~fy
#> 4 drop: fy~fm.add: fm~fy  
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.

# === have_pars_all ====

have_pars_all(partables1, c("fx ~~ fm", "fy ~ fx"))
#> 
#> Number of models: 1
#> 
#> The models:
#> 
#>   Model                  
#> 1 drop: fm~fx.add: fx~~fm 
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.
have_pars_all(partables1, c("fx ~~ fm", "fy ~ fx"),
              output = "logical")
#> drop: fm~fx.add: fx~~fm  drop: fm~fx.add: fx~fm drop: fy~fm.add: fm~~fy 
#>                    TRUE                   FALSE                   FALSE 
#>  drop: fy~fm.add: fm~fy 
#>                   FALSE 


# === have_pars_any ====

have_pars_any(partables1, c("fx ~~ fm", "fm ~ fx"))
#> 
#> Number of models: 3
#> 
#> The models:
#> 
#>   Model                  
#> 1 drop: fm~fx.add: fx~~fm
#> 2 drop: fy~fm.add: fm~~fy
#> 3 drop: fy~fm.add: fm~fy  
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.
have_pars_any(partables1, c("fx ~~ fm", "fm ~ fx"),
              output = "logical")
#> drop: fm~fx.add: fx~~fm  drop: fm~fx.add: fx~fm drop: fy~fm.add: fm~~fy 
#>                    TRUE                   FALSE                    TRUE 
#>  drop: fy~fm.add: fm~fy 
#>                    TRUE 


# === have_pars_none ====

have_pars_none(partables1, c("fx ~~ fm", "fm ~ fx"))
#> 
#> Number of models: 1
#> 
#> The models:
#> 
#>   Model                 
#> 1 drop: fm~fx.add: fx~fm 
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.
have_pars_none(partables1, c("fx ~~ fm", "fm ~ fx"),
               output = "logical")
#> drop: fm~fx.add: fx~~fm  drop: fm~fx.add: fx~fm drop: fy~fm.add: fm~~fy 
#>                   FALSE                    TRUE                   FALSE 
#>  drop: fy~fm.add: fm~fy 
#>                   FALSE 


# === must_not_be_y ====

must_not_be_y(partables1, c("fx"))
#> 
#> Number of models: 3
#> 
#> The models:
#> 
#>   Model                  
#> 1 drop: fm~fx.add: fx~~fm
#> 2 drop: fy~fm.add: fm~~fy
#> 3 drop: fy~fm.add: fm~fy  
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.
must_not_be_y(partables1, c("fx"),
              output = "logical")
#> drop: fm~fx.add: fx~~fm  drop: fm~fx.add: fx~fm drop: fy~fm.add: fm~~fy 
#>                    TRUE                   FALSE                    TRUE 
#>  drop: fy~fm.add: fm~fy 
#>                    TRUE 



# === must_be_y ====

must_be_y(partables1, c("fm"))
#> 
#> Number of models: 2
#> 
#> The models:
#> 
#>   Model                  
#> 1 drop: fy~fm.add: fm~~fy
#> 2 drop: fy~fm.add: fm~fy  
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.
must_be_y(partables1, c("fm"),
          output = "logical")
#> drop: fm~fx.add: fx~~fm  drop: fm~fx.add: fx~fm drop: fy~fm.add: fm~~fy 
#>                   FALSE                   FALSE                    TRUE 
#>  drop: fy~fm.add: fm~fy 
#>                    TRUE 


# === must_not_have_paths ====

must_not_have_paths(partables1, y_on_x = c("fx ~ fm"))
#> 
#> Number of models: 3
#> 
#> The models:
#> 
#>   Model                  
#> 1 drop: fm~fx.add: fx~~fm
#> 2 drop: fy~fm.add: fm~~fy
#> 3 drop: fy~fm.add: fm~fy  
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.
must_not_have_paths(partables1, y_on_x = c("fx ~ fm"),
                    output = "logical")
#> drop: fm~fx.add: fx~~fm  drop: fm~fx.add: fx~fm drop: fy~fm.add: fm~~fy 
#>                    TRUE                   FALSE                    TRUE 
#>  drop: fy~fm.add: fm~fy 
#>                    TRUE 


# === must_not_have_paths ====

must_have_paths(partables1, y_on_x = c("fm ~ fx"))
#> 
#> Number of models: 2
#> 
#> The models:
#> 
#>   Model                  
#> 1 drop: fy~fm.add: fm~~fy
#> 2 drop: fy~fm.add: fm~fy  
#> 
#> NOTE: 'default' names are used. Call 'print()' and add 'names_to_use =
#> "long"' to use the long descriptive names, if available, for the
#> models.
must_have_paths(partables1, y_on_x = c("fm ~ fx"),
                output = "logical")
#> drop: fm~fx.add: fx~~fm  drop: fm~fx.add: fx~fm drop: fy~fm.add: fm~~fy 
#>                   FALSE                   FALSE                    TRUE 
#>  drop: fy~fm.add: fm~fy 
#>                    TRUE