Skip to content

Commit 63176fd

Browse files
committed
add test
1 parent 2ad76db commit 63176fd

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

tests/testthat/test-icc.R

+49-2
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ test_that("icc", {
9090
skip_if_not_installed("lme4")
9191
data(sleepstudy, package = "lme4")
9292
set.seed(12345)
93-
sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE)
93+
sleepstudy$grp <- sample.int(5, size = 180, replace = TRUE)
9494
sleepstudy$subgrp <- NA
9595
for (i in 1:5) {
9696
filter_group <- sleepstudy$grp == i
9797
sleepstudy$subgrp[filter_group] <-
98-
sample(1:30, size = sum(filter_group), replace = TRUE)
98+
sample.int(30, size = sum(filter_group), replace = TRUE)
9999
}
100100
model <- lme4::lmer(
101101
Reaction ~ Days + (1 | grp) + (1 | Subject),
@@ -125,3 +125,50 @@ test_that("icc", {
125125
expect_equal(out$ICC_adjusted, 0.9104331, tolerance = 0.01)
126126
expect_equal(out$ICC_unadjusted, 0.3109478, tolerance = 0.01)
127127
})
128+
129+
130+
test_that("icc, glmmTMB 1.1.9+", {
131+
skip_on_cran()
132+
skip_if_not_installed("glmmTMB", min_version = "1.1.9")
133+
set.seed(101)
134+
dd <- data.frame(
135+
z = rnorm(1000),
136+
x1 = 1:1000,
137+
x2 = runif(1000, 0, 10),
138+
re = rep(1:20, each = 50)
139+
)
140+
dd <- transform(dd, x3 = as.factor(ifelse(
141+
x1 <= 500, "Low", sample(c("Middle", "High"), 1000, replace = TRUE)
142+
)))
143+
dd <- transform(dd, x4 = as.factor(ifelse(
144+
x1 > 500, "High", sample(c("Absent", "Low"), 1000, replace = TRUE)
145+
)))
146+
dd <- transform(dd, z = z + re * 5)
147+
expect_message({
148+
mod_TMB <- glmmTMB::glmmTMB(
149+
z ~ x1 + x2 + x3 + x4 + (1 | re),
150+
data = dd,
151+
start = list(theta = 3),
152+
control = glmmTMB::glmmTMBControl(rank_check = "adjust")
153+
)
154+
})
155+
expect_equal(
156+
icc(mod_TMB),
157+
data.frame(
158+
ICC_adjusted = 0.995480998331767,
159+
ICC_conditional = 0.244468078371849,
160+
ICC_unadjusted = 0.244468078371849
161+
),
162+
ignore_attr = TRUE,
163+
tolerance = 1e-4
164+
)
165+
expect_equal(
166+
r2(mod_TMB),
167+
list(
168+
R2_conditional = c(`Conditional R2` = 0.998890233308478),
169+
R2_marginal = c(`Marginal R2` = 0.754422154936629)
170+
),
171+
ignore_attr = TRUE,
172+
tolerance = 1e-4
173+
)
174+
})

0 commit comments

Comments
 (0)