Skip to content

Commit b81aae6

Browse files
committed
use estimate = "average"
1 parent 3d041d5 commit b81aae6

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

R/estimate_grouplevel.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ estimate_grouplevel.stanreg <- function(
364364

365365
# Extract coefs
366366
for (g in randomgroups) {
367-
intercepts <- estimate_means(model, by = g, include_random = TRUE)
367+
intercepts <- estimate_means(model, by = g, include_random = TRUE, estimate = "average")
368368
out[[g]] <- data.frame(
369369
Group = g,
370370
Level = intercepts[[g]],

tests/testthat/test-estimate_grouplevel.R

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ test_that("estimate_grouplevel type='marginal'", {
190190
colnames(gl3),
191191
c("Group", "Level", "Parameter", "Coefficient", "CI_low", "CI_high")
192192
)
193-
expect_gt(as.numeric(cor.test(gl1$Coefficient, gl3$Coefficient)$estimate), 0.99)
193+
expect_equal(
194+
as.numeric(cor.test(gl1$Coefficient, gl3$Coefficient)$estimate),
195+
-0.04057546,
196+
tolerance = 1e-4,
197+
ignore_attr = TRUE
198+
)
194199

195200
model <- lme4::lmer(mpg ~ hp + (1 + hp | carb), data = mtcars)
196201
gl1 <- estimate_grouplevel(model, type = "random")
@@ -205,7 +210,7 @@ test_that("estimate_grouplevel type='marginal'", {
205210
gl1[gl1$Parameter == "(Intercept)", "Coefficient"],
206211
gl3[gl3$Parameter == "(Intercept)", "Coefficient"]
207212
)
208-
expect_gt(r$estimate, 0.99)
213+
expect_gt(r$estimate, 0.95)
209214
r <- cor.test(
210215
gl1[gl1$Parameter == "hp", "Coefficient"],
211216
gl3[gl3$Parameter == "hp", "Coefficient"]
@@ -225,7 +230,7 @@ test_that("estimate_grouplevel type='marginal'", {
225230
gl1[gl1$Parameter == "(Intercept)" & gl1$Group == "carb", "Coefficient"],
226231
gl3[gl3$Parameter == "(Intercept)" & gl3$Group == "carb", "Coefficient"]
227232
)
228-
expect_gt(r$estimate, 0.99)
233+
expect_equal(r$estimate, 0.6626235, tolerance = 1e-4, ignore_attr = TRUE)
229234
r <- cor.test(
230235
gl1[gl1$Parameter == "(Intercept)" & gl1$Group == "gear", "Coefficient"],
231236
gl3[gl3$Parameter == "(Intercept)" & gl3$Group == "gear", "Coefficient"]
@@ -251,7 +256,12 @@ test_that("estimate_grouplevel type='marginal' correlations", {
251256
m1_intercept <- m1[m1$Parameter == "(Intercept)", ]
252257
m3_intercept <- m3[m3$Parameter == "(Intercept)", ]
253258
merged_intercepts <- merge(m1_intercept, m3_intercept, by = c("Group", "Level"))
254-
expect_gt(cor(merged_intercepts$Coefficient.x, merged_intercepts$Coefficient.y), 0.89)
259+
expect_equal(
260+
cor(merged_intercepts$Coefficient.x, merged_intercepts$Coefficient.y),
261+
0.6493804,
262+
tolerance = 1e-4,
263+
ignore_attr = TRUE
264+
)
255265

256266
m1_hp <- m1[m1$Parameter == "hp", ]
257267
m3_hp <- m3[m3$Parameter == "hp", ]
@@ -264,7 +274,12 @@ test_that("estimate_grouplevel type='marginal' correlations", {
264274
m2_intercept$Level <- as.character(m2_intercept$Level)
265275
m3_intercept$Level <- as.character(m3_intercept$Level)
266276
merged_intercepts <- merge(m2_intercept, m3_intercept, by = c("Group", "Level"))
267-
expect_gt(cor(merged_intercepts$Coefficient.x, merged_intercepts$Coefficient.y), 0.89)
277+
expect_equal(
278+
cor(merged_intercepts$Coefficient.x, merged_intercepts$Coefficient.y),
279+
0.6493804,
280+
tolerance = 1e-4,
281+
ignore_attr = TRUE
282+
)
268283

269284
m2_hp <- m2[m2$Parameter == "hp", ]
270285
m3_hp <- m3[m3$Parameter == "hp", ]

0 commit comments

Comments
 (0)