Skip to content

feature request: Tidy random effects from nested mixed effect models #152

Description

@phargarten2

Thank you for making the broom.mixed package and be able to tidy model results from mixed effect modeling packages.

Recently, I fitted a nested random intercept model and realized that the random parameters are not tidied. All i really want is to have the variance estimates in a nice tibble.

Example:

    library(tibble)
    library(magrittr)
    library(lme4)
#> Loading required package: Matrix
    library(nlme)
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:lme4':
#> 
#>     lmList
    library(broom.mixed)
    
    data("sleepstudy", package="lme4")
    
## Random Intercept Model
lmm1 <- nlme::lme(Reaction ~ Days, random=~ Days|Subject, sleepstudy)

tidy(lmm1, "ran_pars")
#> # A tibble: 4 × 4
#>   effect   group    term                 estimate
#>  <chr>    <chr>    <chr>                   <dbl>
#> 1 ran_pars Subject  sd_(Intercept)        24.7   
#> 2 ran_pars Subject  cor_Days.(Intercept)   0.0656
#> 3 ran_pars Subject  sd_Days                5.92  
#> 4 ran_pars Residual sd_Observation        25.6  

## Crossed effect model
    sleepstudy2 <- sleepstudy %>%
        tibble::add_column(Group = rep(LETTERS[1:10], each = 18))
    
    lmm2 <- nlme::lme(Reaction ~ Days,
                      random = list(Subject = ~ Days, Group = ~ 1),
                      data = sleepstudy2
    )
    
    lmm2
#> Linear mixed-effects model fit by REML
#>   Data: sleepstudy2 
#>   Log-restricted-likelihood: -871.0085
#>   Fixed: Reaction ~ Days 
#> (Intercept)        Days 
#>   252.21968    10.42173 
#> 
#> Random effects:
#>  Formula: ~Days | Subject
#>  Structure: General positive-definite, Log-Cholesky parametrization
#>             StdDev   Corr  
#> (Intercept) 18.30302 (Intr)
#> Days         6.12880 0.048 
#> 
#>  Formula: ~1 | Group %in% Subject
#>         (Intercept) Residual
#> StdDev:    15.84422 25.09344
#> 
#> Number of Observations: 180
#> Number of Groups: 
#>            Subject Group %in% Subject 
#>                 18                 26
    
    #Fixed effects tidy great!
    tidy(lmm2, "fixed")
#> # A tibble: 2 × 7
#>   effect term        estimate std.error    df statistic  p.value
#>   <chr>  <chr>          <dbl>     <dbl> <dbl>     <dbl>    <dbl>
#> 1 fixed  (Intercept)    252.       6.64   153     38.0  7.74e-80
#> 2 fixed  Days            10.4      1.62   153      6.44 1.43e- 9
    
    #Random effects can't be evaluated?
    tidy(lmm2, "ran_pars")
#> Warning in tidy.lme(lmm2, "ran_pars"): ran_pars not yet implemented for
#> multiple levels of nesting
#> # A tibble: 0 × 1
#> # ℹ 1 variable: effect <chr>

Created on 2024-08-30 with reprex v2.1.1

Using VarCorr() it should be possible to extract the relevant components

vc <- VarCorr(lmm2)
vc
#>            Variance        StdDev   Corr  
#> Subject =   pdLogChol(Days)                
(#> Intercept) 335.00060       18.30302 (Intr)
#> Days         37.56219        6.12880 0.048 
#> Group =     pdLogChol(1)                   
#> (Intercept) 251.03921       15.84422       
#> Residual    629.68083       25.09344    

I get an error trying to create this into a tibble.

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