Skip to content

Commit 5216fc7

Browse files
committed
Revert "make it work when factors"
This reverts commit ed32638.
1 parent ed32638 commit 5216fc7

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

R/estimate_grouplevel.R

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/testthat/test-estimate_grouplevel.R

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,6 @@ test_that("estimate_grouplevel type='marginal'", {
240240
gl3[gl3$Parameter == "hp", "Coefficient"]
241241
)
242242
expect_gt(r$estimate, 0.99)
243-
244-
# works when grouping variables are factors
245-
data(mtcars)
246-
mtcars$carb <- as.factor(mtcars$carb)
247-
mtcars$gear <- as.factor(mtcars$gear)
248-
249-
model <- lme4::lmer(mpg ~ hp + (1 + hp | carb) + (1 | gear), data = mtcars)
250-
gl1 <- estimate_grouplevel(model, type = "random")
251-
gl3 <- estimate_grouplevel(model, type = "marginal")
252-
253-
r <- cor.test(
254-
gl1[gl1$Parameter == "(Intercept)" & gl1$Group == "carb", "Coefficient"],
255-
gl3[gl3$Parameter == "(Intercept)" & gl3$Group == "carb", "Coefficient"]
256-
)
257-
expect_equal(r$estimate, 0.6626235, tolerance = 1e-4, ignore_attr = TRUE)
258243
})
259244

260245
test_that("estimate_grouplevel type='marginal' correlations", {

0 commit comments

Comments
 (0)