pars_id | R Documentation |
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.
pars_id(pars, fit, where = c("coef", "partable"), free_only = TRUE)
pars |
A character vector of parameters specified
in lavaan syntax, e.g., |
fit |
A |
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 |
It supports the following ways to specify the parameters to be included.
lavaan
syntax
For example, "y ~ x"
denotes the regression coefficient
regression y
on x
. It uses lavaan::lavaanify()
to
parse the syntax strings.
Operator
For example, "~"
denotes all regression coefficients.
It also supports :=
, which can be used to select
user-defined parameters.
Label
For example, "ab"
denotes all parameters with this
labels defined in model syntax. It can be used to
select user-defined parameters, such as "ab := a*b"
.
It is used by functions such as est_change()
.
If a model has more than one group, a specification specified as in a single sample model denotes the same parameters in all group.
For example, "f1 =~ x2"
denotes the factor loading of
x2
on f1
in all groups. "~~"
denotes covariances
and error covariances in all groups.
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.
For example, "f2 =~ c(NA, 1, NA) * x5"
selects the
factor loadings of x5
on f2
in the first and third
groups.
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.).
For example, "f2 =~ x5.Alpha"
denotes the factor loading
of x5
on f2
in the group "Alpha"
.
This method can be used for operators. For example,
"=~.Alpha"
denotes all factors loadings in the
group "Alpha"
.
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()
.
For example, "f2 =~ x5.g2"
denotes the factor loading
of x5
on f2
in the second group. "y ~ x.g1"
denotes the regression coefficient from x
to y
in the first group.
This method can also be used for operators. For example,
"=~.g2"
denotes all factors loadings in the
second group.
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.
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.
Shu Fai Cheung https://orcid.org/0000-0002-9871-9448
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.