easystats / insight

:crystal_ball: Easy access to model information for various model objects
https://easystats.github.io/insight/
GNU General Public License v3.0
394 stars 39 forks source link

`clean_parameters` function #106

Closed strengejacke closed 5 years ago

strengejacke commented 5 years ago

In some exported clean_parameters function that we can use elsewhere

Originally posted by @DominiqueMakowski in https://github.com/easystats/bayestestR/issues/134#issuecomment-493749408

strengejacke commented 5 years ago

requires https://github.com/easystats/insight/issues/104 to be finished to work with all models.

strengejacke commented 5 years ago

ok, first example, for glmmTMB:

library(insight)
m <- download_model("glmmTMB_zi_4")
m
#> Formula:          
#> count ~ child + camper + (1 + zg | persons) + (1 | nofish)
#> Zero inflation:         ~child + livebait + (1 | persons)
#> Data: fish
#>       AIC       BIC    logLik  df.resid 
#> 1431.1835 1469.9196 -704.5918       239 
#> Random-effects (co)variances:
#> 
#> Conditional model:
#>  Groups  Name        Std.Dev. Corr  
#>  persons (Intercept) 0.9705         
#>          zg          0.1564   -0.16 
#>  nofish  (Intercept) 0.6822         
#> 
#> Zero-inflation model:
#>  Groups  Name        Std.Dev.
#>  persons (Intercept) 1.138   
#> 
#> Number of obs: 250 / Conditional model: persons, 4; nofish, 2 / Zero-inflation model: persons, 4
#> 
#> Fixed Effects:
#> 
#> Conditional model:
#> (Intercept)        child      camper1  
#>      0.9832      -0.9441       0.5144  
#> 
#> Zero-inflation model:
#> (Intercept)        child    livebait1  
#>    -0.01835      2.06313     -1.10753

clean_parameters(m)
#>      Parameter Effect     Component   Group
#> 1  (Intercept)  fixed   conditional        
#> 2        child  fixed   conditional        
#> 3      camper1  fixed   conditional        
#> 4  (Intercept) random   conditional persons
#> 5           zg random   conditional persons
#> 6  (Intercept) random   conditional  nofish
#> 7  (Intercept)  fixed zero_inflated        
#> 8        child  fixed zero_inflated        
#> 9    livebait1  fixed zero_inflated        
#> 10 (Intercept) random zero_inflated persons

Created on 2019-05-19 by the reprex package (v0.2.1)

strengejacke commented 5 years ago

ok, I chanaged the column names to match the style from get_parameters(), and the function for glmmTMB should work for any (non Bayesian) model:

library(insight)
library(lme4)
#> Loading required package: Matrix

data(sleepstudy)

set.seed(123)
sleepstudy$mygrp <- sample(1:5, size = 180, replace = TRUE)
sleepstudy$mysubgrp <- NA
for (i in 1:5) {
  filter_group <- sleepstudy$mygrp == i
  sleepstudy$mysubgrp[filter_group] <- sample(1:30, size = sum(filter_group), replace = TRUE)
}

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

m2 <- lme4::lmer(
  Reaction ~ Days + (1 | mygrp / mysubgrp) + (1 | Subject),
  data = sleepstudy
)
#> boundary (singular) fit: see ?isSingular

clean_parameters(m1)
#>     parameter effects   component   group
#> 1 (Intercept)   fixed conditional        
#> 2        Days   fixed conditional        
#> 3 (Intercept)  random conditional Subject
#> 4        Days  random conditional Subject

clean_parameters(m2)
#>     parameter effects   component          group
#> 1 (Intercept)   fixed conditional               
#> 2        Days   fixed conditional               
#> 3 (Intercept)  random conditional mysubgrp:mygrp
#> 4 (Intercept)  random conditional        Subject
#> 5 (Intercept)  random conditional          mygrp

Created on 2019-05-19 by the reprex package (v0.2.1)

Now only brms and rstanarm are missing.

strengejacke commented 5 years ago

Here's a current example from brms. Next is stanreg...

library(insight)

m1 <- download_model("brms_mv_2")
clean_parameters(m1)
#>                   parameter effects   component  group response cleaned_parameter
#> 1           b_mpg_Intercept   fixed conditional             mpg       (Intercept)
#> 2                  b_mpg_wt   fixed conditional             mpg                wt
#> 3                b_mpg_disp   fixed conditional             mpg              disp
#> 4                 b_mpg_cyl   fixed conditional             mpg               cyl
#> 5                  b_mpg_hp   fixed conditional             mpg                hp
#> 6            b_wt_Intercept   fixed conditional              wt       (Intercept)
#> 7                 b_wt_disp   fixed conditional              wt              disp
#> 8                  b_wt_cyl   fixed conditional              wt               cyl
#> 9                   b_wt_hp   fixed conditional              wt                hp
#> 10 r_gear__mpg.3.Intercept.  random conditional gear.3      mpg       (Intercept)
#> 11 r_gear__mpg.4.Intercept.  random conditional gear.4      mpg       (Intercept)
#> 12 r_gear__mpg.5.Intercept.  random conditional gear.5      mpg       (Intercept)
#> 13  r_gear__wt.3.Intercept.  random conditional gear.3       wt       (Intercept)
#> 14  r_gear__wt.4.Intercept.  random conditional gear.4       wt       (Intercept)
#> 15  r_gear__wt.5.Intercept.  random conditional gear.5       wt       (Intercept)
#> 16                sigma_mpg   fixed       sigma             mpg             sigma
#> 17                 sigma_wt   fixed       sigma              wt             sigma

m2 <- download_model("brms_mixed_2")
clean_parameters(m2)
#>                       parameter effects   component       group            cleaned_parameter
#> 1                   b_Intercept   fixed conditional                              (Intercept)
#> 2                        b_Days   fixed conditional                                     Days
#> 3  cor_Subject__Intercept__Days  random conditional             cor_Subject__Intercept__Days
#> 4      r_Subject.308.Intercept.  random conditional Subject.308                  (Intercept)
#> 5      r_Subject.309.Intercept.  random conditional Subject.309                  (Intercept)
#> 6      r_Subject.310.Intercept.  random conditional Subject.310                  (Intercept)
#> 7      r_Subject.330.Intercept.  random conditional Subject.330                  (Intercept)
#> 8      r_Subject.331.Intercept.  random conditional Subject.331                  (Intercept)
#> 9      r_Subject.332.Intercept.  random conditional Subject.332                  (Intercept)
#> 10     r_Subject.333.Intercept.  random conditional Subject.333                  (Intercept)
#> 11     r_Subject.334.Intercept.  random conditional Subject.334                  (Intercept)
#> 12     r_Subject.335.Intercept.  random conditional Subject.335                  (Intercept)
#> 13     r_Subject.337.Intercept.  random conditional Subject.337                  (Intercept)
#> 14     r_Subject.349.Intercept.  random conditional Subject.349                  (Intercept)
#> 15     r_Subject.350.Intercept.  random conditional Subject.350                  (Intercept)
#> 16     r_Subject.351.Intercept.  random conditional Subject.351                  (Intercept)
#> 17     r_Subject.352.Intercept.  random conditional Subject.352                  (Intercept)
#> 18     r_Subject.369.Intercept.  random conditional Subject.369                  (Intercept)
#> 19     r_Subject.370.Intercept.  random conditional Subject.370                  (Intercept)
#> 20     r_Subject.371.Intercept.  random conditional Subject.371                  (Intercept)
#> 21     r_Subject.372.Intercept.  random conditional Subject.372                  (Intercept)
#> 22          r_Subject.308.Days.  random conditional Subject.308                         Days
#> 23          r_Subject.309.Days.  random conditional Subject.309                         Days
#> 24          r_Subject.310.Days.  random conditional Subject.310                         Days
#> 25          r_Subject.330.Days.  random conditional Subject.330                         Days
#> 26          r_Subject.331.Days.  random conditional Subject.331                         Days
#> 27          r_Subject.332.Days.  random conditional Subject.332                         Days
#> 28          r_Subject.333.Days.  random conditional Subject.333                         Days
#> 29          r_Subject.334.Days.  random conditional Subject.334                         Days
#> 30          r_Subject.335.Days.  random conditional Subject.335                         Days
#> 31          r_Subject.337.Days.  random conditional Subject.337                         Days
#> 32          r_Subject.349.Days.  random conditional Subject.349                         Days
#> 33          r_Subject.350.Days.  random conditional Subject.350                         Days
#> 34          r_Subject.351.Days.  random conditional Subject.351                         Days
#> 35          r_Subject.352.Days.  random conditional Subject.352                         Days
#> 36          r_Subject.369.Days.  random conditional Subject.369                         Days
#> 37          r_Subject.370.Days.  random conditional Subject.370                         Days
#> 38          r_Subject.371.Days.  random conditional Subject.371                         Days
#> 39          r_Subject.372.Days.  random conditional Subject.372                         Days

m3 <- download_model("brms_zi_2")
clean_parameters(m3)
#>                     parameter effects     component     group cleaned_parameter
#> 1                 b_Intercept   fixed   conditional                 (Intercept)
#> 2                   b_persons   fixed   conditional                     persons
#> 3                     b_child   fixed   conditional                       child
#> 4                    b_camper   fixed   conditional                      camper
#> 5      r_persons.1.Intercept.  random   conditional persons.1       (Intercept)
#> 6      r_persons.2.Intercept.  random   conditional persons.2       (Intercept)
#> 7      r_persons.3.Intercept.  random   conditional persons.3       (Intercept)
#> 8      r_persons.4.Intercept.  random   conditional persons.4       (Intercept)
#> 9              b_zi_Intercept   fixed zero_inflated                 (Intercept)
#> 10                 b_zi_child   fixed zero_inflated                       child
#> 11                b_zi_camper   fixed zero_inflated                      camper
#> 12 r_persons__zi.1.Intercept.  random zero_inflated persons.1       (Intercept)
#> 13 r_persons__zi.2.Intercept.  random zero_inflated persons.2       (Intercept)
#> 14 r_persons__zi.3.Intercept.  random zero_inflated persons.3       (Intercept)
#> 15 r_persons__zi.4.Intercept.  random zero_inflated persons.4       (Intercept)

m4 <- download_model("brms_2")
clean_parameters(m4)
#>     parameter effects   component cleaned_parameter
#> 1 b_Intercept   fixed conditional       (Intercept)
#> 2    b_treat1   fixed conditional            treat1
#> 3        b_c2   fixed conditional                c2
#> 4 b_treat1.c2   fixed conditional         treat1:c2

m5 <- download_model("brms_ordinal_1")
clean_parameters(m5)
#>        parameter effects   component cleaned_parameter
#> 1 b_Intercept.1.   fixed conditional      Intercept.1.
#> 2 b_Intercept.2.   fixed conditional      Intercept.2.
#> 3          b_mpg   fixed conditional               mpg

Created on 2019-05-20 by the reprex package (v0.3.0)

strengejacke commented 5 years ago

See example one above, the parmaters are decomposed into its (five) components: effects, component, group, response and parameter name.

I think we can use this especially for print-methods, were we can "separate" the print-output into some of these components (see example tidy_stan(): https://github.com/easystats/bayestestR/issues/135).

DominiqueMakowski commented 5 years ago

Nice!