Intro

Miscellaneous functions are included to fill a need.

Examples

Find typical values

In many cases, predictions are made by holding the random effects to zero, which can be seen as the typical case. When the clusters carry substantive meaning, for example, school, country, or hospital, it might be of interest to note which are most ‘typical’, or at any other value relative to their peers. This will work for any model to which extract_random_effects applies.

library(lme4)

lmer_model <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)

library(mixedup)

find_typical(lmer_model)  # closest RE to zero
# A tibble: 2 x 7
  group_var effect    group  value    se lower_2.5 upper_97.5
  <chr>     <chr>     <fct>  <dbl> <dbl>     <dbl>      <dbl>
1 Subject   Days      333   -0.224  2.31     -4.74       4.29
2 Subject   Intercept 335   -0.334 12.1     -24.0       23.3 


find_typical(lmer_model, probs = c(.1, .9))
# A tibble: 4 x 8
  group_var effect    group  value    se lower_2.5 upper_97.5 probs
  <chr>     <chr>     <fct>  <dbl> <dbl>     <dbl>      <dbl> <chr>
1 Subject   Days      310    -5.45  2.31     -9.97     -0.931 10%  
2 Subject   Days      350     6.61  2.31      2.10     11.1   90%  
3 Subject   Intercept 370   -25.6  12.1     -49.3      -1.96  10%  
4 Subject   Intercept 331    22.3  12.1      -1.40     45.9   90%  

Extract heterogeneous variances

For nlme, only models with varIdent have been tested. For glmmTMB, this applies to models that use the diag function in the formula.

library(nlme)

lme_1 <- lme(
  distance ~ scale(age) + Sex,
  data = Orthodont,
  random = ~ 1 | Subject,
  weights = varIdent(form = ~ 1 | Sex)
)

extract_het_var(lme_1)
   Male Female
1 3.104   0.64

May include brms at some point, but for these, you can extract any parameters associated with sigma model for distributional models using the component argument of the other standard functions.

Extract correlation structure

With some packages one can estimate the residual correlation structure. Right now this works for nlme, glmmTMB, and brms objects.

base_model <-
  lme(Reaction ~ Days,
      random = ~ 1 + Days | Subject,
      data = sleepstudy)

lme_corSymm <-
  update(
    base_model,
    corr = corSymm(form = ~ 1 | Subject),
    data = dplyr::filter(sleepstudy, Days < 5)
  )

lme_corAR <-
  update(
    base_model,
    corr = corAR1(form = ~ Days),
    data = dplyr::filter(sleepstudy, Days < 5)
  )


extract_cor_structure(lme_corSymm)
       V1    V2     V3     V4     V5
V1  1.000 0.263 -0.460 -0.222 -0.092
V2  0.263 1.000  0.272  0.356  0.081
V3 -0.460 0.272  1.000  0.377 -0.417
V4 -0.222 0.356  0.377  1.000  0.572
V5 -0.092 0.081 -0.417  0.572  1.000


extract_cor_structure(lme_corAR)
# A tibble: 1 x 1
    Phi
  <dbl>
1 0.662

Extract model data

Just a wrapper for model.frame.

extract_model_data(lmer_model)
# A tibble: 180 x 3
   Reaction  Days Subject
      <dbl> <dbl> <fct>  
 1     250.     0 308    
 2     259.     1 308    
 3     251.     2 308    
 4     321.     3 308    
 5     357.     4 308    
 6     415.     5 308    
 7     382.     6 308    
 8     290.     7 308    
 9     431.     8 308    
10     466.     9 308    
# … with 170 more rows