@@ -342,24 +342,21 @@ estimate_grouplevel.stanreg <- function(
342342
343343 # Analyze random effect structure
344344 randomgroups <- insight :: find_random(model )$ random
345-
346- # formulas for random effects
347- f_random <- insight :: find_formula( model ) $ random
345+ # randomslopes <- insight::find_random_slopes(model)
346+ # TODO: currently insight::find_random_slopes() doesn't tell us to which random factor does the slope belong to
347+ # So we need to find it manually by callin ranef()
348348 randomslopes <- list ()
349- # iterate random effects
350- for (s in f_random ) {
351- # extract slope and group
352- p_slope <- gsub(" (.*)\\ |(.*)" , " \\ 1" , insight :: safe_deparse(s ))
353- p_group <- gsub(" (.*)\\ |(.*)" , " \\ 2" , insight :: safe_deparse(s ))
354- # clean
355- re <- all.vars(s )
356- # extract slopes
357- slopes <- insight :: trim_ws(re [sapply(re , grepl , p_slope , fixed = TRUE )])
358- # if slopes exist, add them to list
359- if (length(slopes ) > 0 ) {
360- # make sure we use related group name as element name
361- gr <- insight :: trim_ws(re [sapply(re , grepl , p_group , fixed = TRUE )])
362- randomslopes [[gr ]] <- slopes
349+ p <- lme4 :: ranef(model )
350+ for (g in names(p )) {
351+ s <- names(p [[g ]])[names(p [[g ]]) != " (Intercept)" ]
352+
353+ # Only pick non-factor random slopes for now
354+ # TODO: correctly deal with the case when the random slope is a factor
355+ pred <- insight :: find_predictors(model , effects = " all" , flatten = TRUE )
356+ s <- s [s %in% pred ]
357+
358+ if (length(s ) > 0 ) {
359+ randomslopes [[g ]] <- s
363360 }
364361 }
365362
0 commit comments