pars_id {semfindr}R Documentation

Convert Parameter Syntax to Position or Row Numbers in the Parameter Vector or Table

Description

Converts a vector of lavaan syntax to the ids of parameters in the vector of free parameters or the row numbers in the parameter table.

Usage

pars_id(pars, fit, where = c("coef", "partable"), free_only = TRUE)

Arguments

pars

A character vector of parameters specified in lavaan syntax, e.g., "y ~ x" and f1 =~ x3. For multisample models, if only the parameters in some groups are needed, use the modifier for labeling parameters and use NA to denote parameters to be requested. E.g., f1 =~ c(NA, 0, NA, NA) * x2 denotes the loadings of x2 on f1 in the first, third, and fourth groups.

fit

A lavaan-class object. This object is used to determine the number of groups and the parameters in the model. Only parameters in pars that appear in this model will be considered.

where

Where the values are to be found. Can be "partable" (parameter table) or "coef" (coefficient vector). Default is "coef".

free_only

Whether only free parameters will be kept. Default is TRUE.

Details

It supports the following ways to specify the parameters to be included.

It is used by functions such as est_change().

Multisample model

If a model has more than one group, a specification specified as in a single sample model denotes the same parameters in all group.

There are two ways to select parameters only in selected groups. First, the syntax to fix parameter values can be used, with NA denoting parameters to be selected.

Users can also add ".grouplabel" to a specification, grouplabel being the group label of a group (the one appears in summary(), not the one of the form ".g2", "g3", etc.).

Though not recommended, users can use labels such as ".g2" and ".g3" to denote the parameter in a specific group. These are the labels appear in the output of some functions of lavaan. Although lavaan does not label the parameters in the first group by ".g1", this can still be used in pars_id().

However, this method is not as reliable as using grouplabel because the numbering of groups depends on the order they appear in the data set.

Value

A numeric vector of the ids. If where is "partable", the ids are row numbers. If where is "coef", the ids are the positions in the vector.

Author(s)

Shu Fai Cheung https://orcid.org/0000-0002-9871-9448

Examples


dat <- sem_dat

library(lavaan)
sem_model <-
"
f1 =~  x1 + x2 + x3
f2 =~  x4 + x5 + x6
f3 =~  x7 + x8 + x9
f2 ~   f1
f3 ~   f2
"

fit_ng <- sem(sem_model, dat)

pars <- c("f1 =~ x2", "f2 =~ x5", "f2 ~ f1")
tmp <- pars_id(pars, fit = fit_ng)
coef(fit_ng)[tmp]
tmp <- pars_id(pars, fit = fit_ng, where = "partable")
parameterTable(fit_ng)[tmp, ]

# Multiple-group models

dat <- sem_dat
set.seed(64264)
dat$gp <- sample(c("Alpha", "Beta", "Gamma"),
                 nrow(dat),
                 replace = TRUE)

library(lavaan)
sem_model <-
"
f1 =~  x1 + x2 + x3
f2 =~  x4 + x5 + x6
f3 =~  x7 + x8 + x9
f2 ~   f1
f3 ~   f2
"

fit_ng <- sem(sem_model, dat)
fit_gp <- sem(sem_model, dat, group = "gp")

pars <- c("f1 =~ x2", "f2 =~ x5", "f2 ~ f1")
tmp <- pars_id(pars, fit = fit_ng)
coef(fit_ng)[tmp]
tmp <- pars_id(pars, fit = fit_ng, where = "partable")
parameterTable(fit_ng)[tmp, ]

pars <- c("f1 =~ x2", "f2 =~ c(NA, 1, NA) * x5")
tmp <- pars_id(pars, fit = fit_gp)
coef(fit_gp)[tmp]
tmp <- pars_id(pars, fit = fit_gp, where = "partable")
parameterTable(fit_gp)[tmp, ]

pars2 <- c("f1 =~ x2", "~~.Beta", "f2 =~ x5.Gamma")
tmp <- pars_id(pars2, fit = fit_gp)
coef(fit_gp)[tmp]
tmp <- pars_id(pars2, fit = fit_gp, where = "partable")
parameterTable(fit_gp)[tmp, ]
# Note that group 1 is "Beta", not "Alpha"
lavInspect(fit_gp, "group.label")



[Package semfindr version 0.1.8 Index]