mattansb / Structural-Equation-Modeling-foR-Psychologists

Lesson files used in the Structural Equation Modeling for Psychologists.
Other
137 stars 30 forks source link

Improving model fit (MI and Score Test) #19

Open mattansb opened 2 years ago

mattansb commented 2 years ago
lavaan::lavTestScore()
lavaan::modificationIndices()
mattansb commented 1 year ago
library(lavaan)
#> This is lavaan 0.6-14
#> lavaan is FREE software! Please report any bugs.

HS.model <- '
    visual  =~ x1 + b1*x2 + b1*x3
    textual =~ x4 + b2*x5 + b2*x6
    speed   =~ x7 + x8 + x9
'
fit <- cfa(HS.model, data=HolzingerSwineford1939)

## Modification Indices ------------------

modificationIndices(fit, sort. = TRUE, minimum.value = 10)
#>       lhs op rhs     mi    epc sepc.lv sepc.all sepc.nox
#> 32 visual =~  x9 36.229  0.569   0.511    0.507    0.507
#> 78     x7 ~~  x8 33.738  0.547   0.547    0.883    0.883
#> 30 visual =~  x7 18.679 -0.420  -0.377   -0.347   -0.347
#> 80     x8 ~~  x9 14.953 -0.429  -0.429   -0.817   -0.817
#> 67     x4 ~~  x6 14.566 -0.212  -0.212   -0.607   -0.607
#> 66     x4 ~~  x5 11.398  0.185   0.185    0.419    0.419

## Lagrange Multiplier test --------------

lavTestScore(fit)
#> $test
#> 
#> total score test:
#> 
#>    test     X2 df p.value
#> 1 score 12.013  2   0.002
#> 
#> $uni
#> 
#> univariate score tests:
#> 
#>    lhs op  rhs    X2 df p.value
#> 1 .p2. == .p3. 2.877  1   0.090
#> 2 .p5. == .p6. 9.136  1   0.003

parTable(fit)[c("lhs", "op", "rhs", "label", "plabel")]
#>        lhs op     rhs label plabel
#> 1   visual =~      x1         .p1.
#> 2   visual =~      x2    b1   .p2.
#> 3   visual =~      x3    b1   .p3.
#> 4  textual =~      x4         .p4.
#> 5  textual =~      x5    b2   .p5.
#> 6  textual =~      x6    b2   .p6.
#> 7    speed =~      x7         .p7.
#> 8    speed =~      x8         .p8.
#> 9    speed =~      x9         .p9.
#> 10      x1 ~~      x1        .p10.
#> 11      x2 ~~      x2        .p11.
#> 12      x3 ~~      x3        .p12.
#> 13      x4 ~~      x4        .p13.
#> 14      x5 ~~      x5        .p14.
#> 15      x6 ~~      x6        .p15.
#> 16      x7 ~~      x7        .p16.
#> 17      x8 ~~      x8        .p17.
#> 18      x9 ~~      x9        .p18.
#> 19  visual ~~  visual        .p19.
#> 20 textual ~~ textual        .p20.
#> 21   speed ~~   speed        .p21.
#> 22  visual ~~ textual        .p22.
#> 23  visual ~~   speed        .p23.
#> 24 textual ~~   speed        .p24.
#> 25    .p2. ==    .p3.             
#> 26    .p5. ==    .p6.

Created on 2023-04-03 with reprex v2.0.2