Skip to content

[Feature Request] tidy and glance methods? #101

@emstruong

Description

@emstruong

Hi Ed,

The tidy() method for blavaan doesn't seem to return confidence intervals. The glance() method also seems to be broken.

I was thinking that it might be a good idea to extend the method, such that it is a cross of the tidy method for lavaan and also the tidy method for brms.

Would you be open to a PR and are there any specific requests you have for the methods?

library(lavaan)
#> This is lavaan 0.6-20
#> lavaan is FREE software! Please report any bugs.
library(broom)
library(blavaan)
#> Loading required package: Rcpp
#> This is blavaan 0.5-9.1372
#> On multicore systems, we suggest use of future::plan("multicore") or
#>   future::plan("multisession") for faster post-MCMC computations.


HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '
     
fit <- blavaan::bcfa(HS.model, data=HolzingerSwineford1939, seed=959)
#> 
#> SAMPLING FOR MODEL 'stanmarg' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.000275 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 2.75 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:    1 / 1500 [  0%]  (Warmup)
#> Chain 1: Iteration:  150 / 1500 [ 10%]  (Warmup)
#> Chain 1: Iteration:  300 / 1500 [ 20%]  (Warmup)
#> Chain 1: Iteration:  450 / 1500 [ 30%]  (Warmup)
#> Chain 1: Iteration:  501 / 1500 [ 33%]  (Sampling)
#> Chain 1: Iteration:  650 / 1500 [ 43%]  (Sampling)
#> Chain 1: Iteration:  800 / 1500 [ 53%]  (Sampling)
#> Chain 1: Iteration:  950 / 1500 [ 63%]  (Sampling)
#> Chain 1: Iteration: 1100 / 1500 [ 73%]  (Sampling)
#> Chain 1: Iteration: 1250 / 1500 [ 83%]  (Sampling)
#> Chain 1: Iteration: 1400 / 1500 [ 93%]  (Sampling)
#> Chain 1: Iteration: 1500 / 1500 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 1.676 seconds (Warm-up)
#> Chain 1:                3.302 seconds (Sampling)
#> Chain 1:                4.978 seconds (Total)
#> Chain 1: 
#> 
#> SAMPLING FOR MODEL 'stanmarg' NOW (CHAIN 2).
#> Chain 2: 
#> Chain 2: Gradient evaluation took 0.000186 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 1.86 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2: 
#> Chain 2: 
#> Chain 2: Iteration:    1 / 1500 [  0%]  (Warmup)
#> Chain 2: Iteration:  150 / 1500 [ 10%]  (Warmup)
#> Chain 2: Iteration:  300 / 1500 [ 20%]  (Warmup)
#> Chain 2: Iteration:  450 / 1500 [ 30%]  (Warmup)
#> Chain 2: Iteration:  501 / 1500 [ 33%]  (Sampling)
#> Chain 2: Iteration:  650 / 1500 [ 43%]  (Sampling)
#> Chain 2: Iteration:  800 / 1500 [ 53%]  (Sampling)
#> Chain 2: Iteration:  950 / 1500 [ 63%]  (Sampling)
#> Chain 2: Iteration: 1100 / 1500 [ 73%]  (Sampling)
#> Chain 2: Iteration: 1250 / 1500 [ 83%]  (Sampling)
#> Chain 2: Iteration: 1400 / 1500 [ 93%]  (Sampling)
#> Chain 2: Iteration: 1500 / 1500 [100%]  (Sampling)
#> Chain 2: 
#> Chain 2:  Elapsed Time: 1.712 seconds (Warm-up)
#> Chain 2:                3.041 seconds (Sampling)
#> Chain 2:                4.753 seconds (Total)
#> Chain 2: 
#> 
#> SAMPLING FOR MODEL 'stanmarg' NOW (CHAIN 3).
#> Chain 3: 
#> Chain 3: Gradient evaluation took 0.000194 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 1.94 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3: 
#> Chain 3: 
#> Chain 3: Iteration:    1 / 1500 [  0%]  (Warmup)
#> Chain 3: Iteration:  150 / 1500 [ 10%]  (Warmup)
#> Chain 3: Iteration:  300 / 1500 [ 20%]  (Warmup)
#> Chain 3: Iteration:  450 / 1500 [ 30%]  (Warmup)
#> Chain 3: Iteration:  501 / 1500 [ 33%]  (Sampling)
#> Chain 3: Iteration:  650 / 1500 [ 43%]  (Sampling)
#> Chain 3: Iteration:  800 / 1500 [ 53%]  (Sampling)
#> Chain 3: Iteration:  950 / 1500 [ 63%]  (Sampling)
#> Chain 3: Iteration: 1100 / 1500 [ 73%]  (Sampling)
#> Chain 3: Iteration: 1250 / 1500 [ 83%]  (Sampling)
#> Chain 3: Iteration: 1400 / 1500 [ 93%]  (Sampling)
#> Chain 3: Iteration: 1500 / 1500 [100%]  (Sampling)
#> Chain 3: 
#> Chain 3:  Elapsed Time: 1.971 seconds (Warm-up)
#> Chain 3:                3.447 seconds (Sampling)
#> Chain 3:                5.418 seconds (Total)
#> Chain 3: 
#> Computing post-estimation metrics (including lvs if requested)...

tidy(fit)
#> # A tibble: 24 × 5
#>    term          op    estimate std.lv std.all
#>    <chr>         <chr>    <dbl>  <dbl>   <dbl>
#>  1 visual =~ x1  =~       1      0.888   0.760
#>  2 visual =~ x2  =~       0.575  0.511   0.431
#>  3 visual =~ x3  =~       0.759  0.674   0.591
#>  4 textual =~ x4 =~       1      0.991   0.849
#>  5 textual =~ x5 =~       1.12   1.11    0.855
#>  6 textual =~ x6 =~       0.932  0.924   0.838
#>  7 speed =~ x7   =~       1      0.595   0.546
#>  8 speed =~ x8   =~       1.24   0.738   0.718
#>  9 speed =~ x9   =~       1.19   0.711   0.692
#> 10 x1 ~~ x1      ~~       0.578  0.578   0.423
#> # ℹ 14 more rows
tidy(fit, conf.int = TRUE)
#> # A tibble: 24 × 5
#>    term          op    estimate std.lv std.all
#>    <chr>         <chr>    <dbl>  <dbl>   <dbl>
#>  1 visual =~ x1  =~       1      0.888   0.760
#>  2 visual =~ x2  =~       0.575  0.511   0.431
#>  3 visual =~ x3  =~       0.759  0.674   0.591
#>  4 textual =~ x4 =~       1      0.991   0.849
#>  5 textual =~ x5 =~       1.12   1.11    0.855
#>  6 textual =~ x6 =~       0.932  0.924   0.838
#>  7 speed =~ x7   =~       1      0.595   0.546
#>  8 speed =~ x8   =~       1.24   0.738   0.718
#>  9 speed =~ x9   =~       1.19   0.711   0.692
#> 10 x1 ~~ x1      ~~       0.578  0.578   0.423
#> # ℹ 14 more rows
glance(fit)
#> Error in `rename()`:
#> ! Can't rename columns that don't exist.
#> ✖ Column `rmsea.ci.upper` doesn't exist.

Created on 2025-12-14 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions