Skip to contents

Converts id numbers generated by pars_id() to values that can be used to extract the parameters from a source.

Usage

pars_id_to_lorg(pars_id, pars_source, type = c("free", "all"))

Arguments

pars_id

A vector of integers. Usually the output of pars_id.

pars_source

Can be the output of lavaan::parameterEstimates() or lavaan::parameterTable(), or a named vector of free parameters (e.g., the output of coef() applied to a lavaan-class object).

type

The meaning of the values in pars_id. If "free", they are the position in the vector of free parameters (i.e., the output of of coef()). If "all", they are the row numbers in the parameter table (the output of lavaan::parameterTable()). If pars_source is the output of lavaan::parameterEstimates(), which does not indicate whether a parameter is free or fixed, this argument will be ignored.

Value

If pars_source is the output of lavaan::parameterEstimates() or lavaan::parameterTable(), it returns a subset of pars_source, keeping the rows of selected parameters and the columns lhs, op, rhs, and group. If pars_source is a named vector of free parameters, it returns a character vector containing the names of the selected parameters.

Details

If the source is a parameter estimates table (i.e., the output of lavaan::parameterEstimates(), it returns a data frame with columns "lhs", "op", and "rhs". If "group" is present in the source, it also add a column "group". These columns can be used to uniquely identify the parameters specified by the ids.

If the source is a named vector of parameters (e.g., the output of coef()), it returns the names of parameters based on the ids.

Examples

dat <- sem_dat
set.seed(64264)
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)
pars_id_to_lorg(tmp, pars_source = coef(fit_ng))
#> [1] "f1=~x2" "f2=~x5" "f2~f1" 
tmp <- pars_id(pars, fit = fit_ng, where = "partable")
pars_id_to_lorg(tmp, pars_source = parameterEstimates(fit_ng))
#>    lhs op rhs
#> 2   f1 =~  x2
#> 5   f2 =~  x5
#> 10  f2  ~  f1

# Multiple-group models

dat$gp <- sample(c("Alpha", "Beta", "Gamma"),
                 nrow(dat),
                 replace = TRUE)

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

pars <- c("f1 =~ x2", "f2 =~ c(NA, 1, NA) * x5")
tmp <- pars_id(pars, fit = fit_gp)
pars_id_to_lorg(tmp, pars_source = coef(fit_gp))
#> [1] "f1=~x2"    "f2=~x5"    "f1=~x2.g2" "f1=~x2.g3" "f2=~x5.g3"
tmp <- pars_id(pars, fit = fit_gp, where = "partable")
pars_id_to_lorg(tmp, pars_source = parameterEstimates(fit_gp))
#>    lhs op rhs group
#> 2   f1 =~  x2     1
#> 5   f2 =~  x5     1
#> 37  f1 =~  x2     2
#> 72  f1 =~  x2     3
#> 75  f2 =~  x5     3

parameterTable(fit_gp)[tmp, ]
#>    id lhs op rhs user block group free ustart exo label plabel start   est
#> 2   2  f1 =~  x2    1     1     1    1     NA   0         .p2. 0.453 0.450
#> 5   5  f2 =~  x5    1     1     1    3     NA   0         .p5. 0.578 0.840
#> 37 37  f1 =~  x2    1     2     2   30     NA   0        .p37. 0.538 0.687
#> 72 72  f1 =~  x2    1     3     3   59     NA   0        .p72. 1.703 1.626
#> 75 75  f2 =~  x5    1     3     3   61     NA   0        .p75. 1.210 0.486
#>       se
#> 2  0.158
#> 5  0.137
#> 37 0.218
#> 72 1.114
#> 75 0.155
pars2 <- c("f1 =~ x2", "~~.Beta", "f2 =~ x5.Gamma")
tmp <- pars_id(pars2, fit = fit_gp)
pars_id_to_lorg(tmp, pars_source = coef(fit_gp))
#>  [1] "f1=~x2"    "x1~~x1"    "x2~~x2"    "x3~~x3"    "x4~~x4"    "x5~~x5"   
#>  [7] "x6~~x6"    "x7~~x7"    "x8~~x8"    "x9~~x9"    "f1~~f1"    "f2~~f2"   
#> [13] "f3~~f3"    "f1=~x2.g2" "f1=~x2.g3" "f2=~x5.g3"
tmp <- pars_id(pars2, fit = fit_gp, where = "partable")
pars_id_to_lorg(tmp, pars_source = parameterEstimates(fit_gp))
#>    lhs op rhs group
#> 2   f1 =~  x2     1
#> 12  x1 ~~  x1     1
#> 13  x2 ~~  x2     1
#> 14  x3 ~~  x3     1
#> 15  x4 ~~  x4     1
#> 16  x5 ~~  x5     1
#> 17  x6 ~~  x6     1
#> 18  x7 ~~  x7     1
#> 19  x8 ~~  x8     1
#> 20  x9 ~~  x9     1
#> 21  f1 ~~  f1     1
#> 22  f2 ~~  f2     1
#> 23  f3 ~~  f3     1
#> 37  f1 =~  x2     2
#> 72  f1 =~  x2     3
#> 75  f2 =~  x5     3
# Note that group 1 is "Beta", not "Alpha"
lavInspect(fit_gp, "group.label")
#> [1] "Beta"  "Alpha" "Gamma"