Factor scores for an endogenous latent factor

43 views
Skip to first unread message

Hok Chio (Mark) Lai

unread,
Jun 13, 2023, 12:13:22 PM6/13/23
to lavaan
Does `lavPredict()` support computing factor scores for endogenous latent factors? I tried fitting a simple MIMIC model and got the following error:

``` r
library(lavaan)
#> This is lavaan 0.6-13
#> lavaan is FREE software! Please report any bugs.
HS.model <- ' visual  =~ x1 + x2 + x3
              visual ~ school
              x3 ~ school '
fit <- cfa(HS.model, data = HolzingerSwineford1939)
lavPredict(fit)
#> Error in `[<-`(`*tmp*`, , ov.x.dummy.lv.idx, value = eXo): subscript out of bounds
```

<sup>Created on 2023-06-13 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>

I think I got the same error in version 0.6-15 as well. Thanks!

Mark

Hok Chio (Mark) Lai

unread,
Jun 13, 2023, 12:27:24 PM6/13/23
to lavaan
Looks like I can get around this by creating a latent variable for `school` so that the latent factor is regressed on a latent variable instead of an observed variable:

``` r
library(lavaan)
#> This is lavaan 0.6-13
#> lavaan is FREE software! Please report any bugs.
HS_dat <- within(HolzingerSwineford1939, {
    sch <- as.integer(school) - 1
})

HS.model <- ' visual  =~ x1 + x2 + x3
              lschool =~ sch
              visual ~ lschool
              x3 ~ lschool '
fit <- cfa(HS.model, data = HS_dat)
lavPredict(fit) |> head()
#>          visual   lschool
#> [1,] -1.5934664 0.4817276
#> [2,] -0.9576667 0.4817276
#> [3,] -1.1873101 0.4817276
#> [4,] -0.3507596 0.4817276
#> [5,] -1.5327331 0.4817276
#> [6,] -0.9457860 0.4817276

```

<sup>Created on 2023-06-13 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>
Reply all
Reply to author
Forward
0 new messages