Skip to contents

The lavaan model syntax parser can be extended with new operators, modifiers and groupings. These extensions are only used in the 'open' parser.

Usage

lav_parse_options(
    pkgname   = NULL,
    operators = NULL,
    modifiers = NULL,
    groupings  = NULL
  )

Arguments

pkgname

The name of the package that extends the elements.

operators

A character vector with new operators. These operators must start and end with a percentage sign and be at least 3 characters long.

modifiers

A data.frame with columns mod, side and expect. The elements in mod must be character strings in snake_case. The elements in side must be l or r, indicating whether the modifier is expected on the left-hand side or on the right-hand side of the operator. The elements in expect must be char, num or raw, specifying the type of value the modifier expects, where raw means the value is not evaluated by the parser but returned as an uninterpreted string.

groupings

A character vector with new groupings; they must be in snake_case.

Value

A list with data.frames containing the currently active parser configuration.

Examples

curconfig <- lav_parse_options("testPKG",
    operators = c('%w%', '%q%'),
    modifiers = data.frame(mod = c("linksc", "linksn", "rechtsn"),
                           side = c("l", "l", "r"),
                           expect = c("char", "num", "raw")),
    groupings = c("color", "colour")
)
for (j in seq_along(curconfig)) {
    cat(names(curconfig)[j], ":\n")
    print(curconfig[[j]])
}
#> operators :
#>     op     pkg
#> 1   =~  lavaan
#> 2   <~  lavaan
#> 3  ~*~  lavaan
#> 4   ~~  lavaan
#> 5    ~  lavaan
#> 6   |~  lavaan
#> 7   ==  lavaan
#> 8    <  lavaan
#> 9    >  lavaan
#> 10  :=  lavaan
#> 11   :  lavaan
#> 12   |  lavaan
#> 13   %  lavaan
#> 14  :~  lavaan
#> 15 %w% testPKG
#> 16 %q% testPKG
#> modifiers :
#>        mod side expect     pkg
#> 1      efa    l   char  lavaan
#> 2    fixed    r    num  lavaan
#> 3    label    r   char  lavaan
#> 4    start    r    num  lavaan
#> 5    lower    r    num  lavaan
#> 6    upper    r    num  lavaan
#> 7       rv    r   char  lavaan
#> 8    equal    r   char  lavaan
#> 9   linksc    l   char testPKG
#> 10  linksn    l    num testPKG
#> 11 rechtsn    r    raw testPKG
#> groupings :
#>    group     pkg
#> 1  group  lavaan
#> 2  level  lavaan
#> 3  class  lavaan
#> 4  block  lavaan
#> 5  color testPKG
#> 6 colour testPKG
model <- '
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + a*y2 + start(0.3)*b*y3 + upper(0.99)*c*y4
     dem65 =~ y5 + a*y6 + b*y7 + rechtsn(2 * pi / 3)*c*y8
     dem60 %w% aa *0.2*y4

    dem60 ~ ind60
    dem65 ~ ind60 + dem60
    linksc("klopt") * dem60 =~ aaa * y8
    linksn(sqrt(23.5)) * dem60 =~ bbb * y9
    efa("efatje") * dem65 =~ xyz * x3

    y1 ~~ y5
    y2 ~~ y4 + y6
    y3 ~~ y7
    y4 ~~ y8
    y6 ~~ y8
'
print(result <- lavParseModelString(model, TRUE, parser = "open"))
#>      lhs  op   rhs block    efa fixed label start upper linksc           linksn
#> 1  ind60  =~    x1     1                                                       
#> 2  ind60  =~    x2     1                                                       
#> 3  ind60  =~    x3     1                                                       
#> 4  dem60  =~    y1     1                                                       
#> 5  dem60  =~    y2     1                  a                                    
#> 6  dem60  =~    y3     1                  b   0.3                              
#> 7  dem60  =~    y4     1                  c        0.99                        
#> 8  dem65  =~    y5     1                                                       
#> 9  dem65  =~    y6     1                  a                                    
#> 10 dem65  =~    y7     1                  b                                    
#> 11 dem65  =~    y8     1                  c                                    
#> 12 dem60 %w%    y4     1          0.2    aa                                    
#> 13 dem60   ~ ind60     1                                                       
#> 14 dem65   ~ ind60     1                                                       
#> 15 dem65   ~ dem60     1                                                       
#> 16 dem60  =~    y8     1                aaa              klopt                 
#> 17 dem60  =~    y9     1                bbb                    4.84767985741633
#> 18 dem65  =~    x3     1 efatje         xyz                                    
#> 19    y1  ~~    y5     1                                                       
#> 20    y2  ~~    y4     1                                                       
#> 21    y2  ~~    y6     1                                                       
#> 22    y3  ~~    y7     1                                                       
#> 23    y4  ~~    y8     1                                                       
#> 24    y6  ~~    y8     1                                                       
#>     rechtsn mod.idx
#> 1                 0
#> 2                 0
#> 3                 0
#> 4                 0
#> 5                 1
#> 6                 2
#> 7                 3
#> 8                 0
#> 9                 4
#> 10                5
#> 11 2 * pi/3       6
#> 12                7
#> 13                0
#> 14                0
#> 15                0
#> 16                8
#> 17                9
#> 18               10
#> 19                0
#> 20                0
#> 21                0
#> 22                0
#> 23                0
#> 24                0
print(attributes(result))
#> $names
#>  [1] "lhs"     "op"      "rhs"     "block"   "efa"     "fixed"   "label"  
#>  [8] "start"   "upper"   "linksc"  "linksn"  "rechtsn" "mod.idx"
#> 
#> $class
#> [1] "data.frame"
#> 
#> $row.names
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#> 
#> $modifiers
#> $modifiers[[1]]
#> $modifiers[[1]]$label
#> [1] "a"
#> 
#> 
#> $modifiers[[2]]
#> $modifiers[[2]]$start
#> [1] 0.3
#> 
#> $modifiers[[2]]$label
#> [1] "b"
#> 
#> 
#> $modifiers[[3]]
#> $modifiers[[3]]$upper
#> [1] 0.99
#> 
#> $modifiers[[3]]$label
#> [1] "c"
#> 
#> 
#> $modifiers[[4]]
#> $modifiers[[4]]$label
#> [1] "a"
#> 
#> 
#> $modifiers[[5]]
#> $modifiers[[5]]$label
#> [1] "b"
#> 
#> 
#> $modifiers[[6]]
#> $modifiers[[6]]$rechtsn
#> [1] "2 * pi/3"
#> 
#> $modifiers[[6]]$label
#> [1] "c"
#> 
#> 
#> $modifiers[[7]]
#> $modifiers[[7]]$label
#> [1] "aa"
#> 
#> $modifiers[[7]]$fixed
#> [1] 0.2
#> 
#> 
#> $modifiers[[8]]
#> $modifiers[[8]]$linksc
#> [1] "klopt"
#> 
#> $modifiers[[8]]$label
#> [1] "aaa"
#> 
#> 
#> $modifiers[[9]]
#> $modifiers[[9]]$linksn
#> [1] 4.84768
#> 
#> $modifiers[[9]]$label
#> [1] "bbb"
#> 
#> 
#> $modifiers[[10]]
#> $modifiers[[10]]$efa
#> [1] "efatje"
#> 
#> $modifiers[[10]]$label
#> [1] "xyz"
#> 
#> 
#> 
#> $constraints
#> list()
#> 

model2 <- '
  color: green
    level: 1
        fw =~ y1 + y2 + y3
        fw ~ x1 + x2 + x3
    level: 2
        fb =~ y1 + y2 + y3
        fb ~ w1 + w2
  color: blue
    level: 1
        fw =~ y1 + y2 + y3
        fw ~ x1 + x2 + x3
    level: 2
        fb =~ y1 + y2 + y3
        fb ~ w1 + w2
'
print(result2 <- lavParseModelString(model2, TRUE, parser = "open"))
#>      lhs op   rhs block mod.idx
#> 1  color  : green     1       0
#> 2  level  :     1     2       0
#> 3     fw =~    y1     2       0
#> 4     fw =~    y2     2       0
#> 5     fw =~    y3     2       0
#> 6     fw  ~    x1     2       0
#> 7     fw  ~    x2     2       0
#> 8     fw  ~    x3     2       0
#> 9  level  :     2     3       0
#> 10    fb =~    y1     3       0
#> 11    fb =~    y2     3       0
#> 12    fb =~    y3     3       0
#> 13    fb  ~    w1     3       0
#> 14    fb  ~    w2     3       0
#> 15 color  :  blue     4       0
#> 16 level  :     1     5       0
#> 17    fw =~    y1     5       0
#> 18    fw =~    y2     5       0
#> 19    fw =~    y3     5       0
#> 20    fw  ~    x1     5       0
#> 21    fw  ~    x2     5       0
#> 22    fw  ~    x3     5       0
#> 23 level  :     2     6       0
#> 24    fb =~    y1     6       0
#> 25    fb =~    y2     6       0
#> 26    fb =~    y3     6       0
#> 27    fb  ~    w1     6       0
#> 28    fb  ~    w2     6       0
print(attributes(result2))
#> $names
#> [1] "lhs"     "op"      "rhs"     "block"   "mod.idx"
#> 
#> $class
#> [1] "data.frame"
#> 
#> $row.names
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#> [26] 26 27 28
#> 
#> $modifiers
#> list()
#> 
#> $constraints
#> list()
#>