From 63176fd0811b07ec7f58166e37901ba12ca3bc39 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 20 Mar 2024 15:42:51 +0100 Subject: [PATCH] add test --- tests/testthat/test-icc.R | 51 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-icc.R b/tests/testthat/test-icc.R index ad4e4b033..1fc72e9a8 100644 --- a/tests/testthat/test-icc.R +++ b/tests/testthat/test-icc.R @@ -90,12 +90,12 @@ test_that("icc", { skip_if_not_installed("lme4") data(sleepstudy, package = "lme4") set.seed(12345) - sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE) + sleepstudy$grp <- sample.int(5, size = 180, replace = TRUE) sleepstudy$subgrp <- NA for (i in 1:5) { filter_group <- sleepstudy$grp == i sleepstudy$subgrp[filter_group] <- - sample(1:30, size = sum(filter_group), replace = TRUE) + sample.int(30, size = sum(filter_group), replace = TRUE) } model <- lme4::lmer( Reaction ~ Days + (1 | grp) + (1 | Subject), @@ -125,3 +125,50 @@ test_that("icc", { expect_equal(out$ICC_adjusted, 0.9104331, tolerance = 0.01) expect_equal(out$ICC_unadjusted, 0.3109478, tolerance = 0.01) }) + + +test_that("icc, glmmTMB 1.1.9+", { + skip_on_cran() + skip_if_not_installed("glmmTMB", min_version = "1.1.9") + set.seed(101) + dd <- data.frame( + z = rnorm(1000), + x1 = 1:1000, + x2 = runif(1000, 0, 10), + re = rep(1:20, each = 50) + ) + dd <- transform(dd, x3 = as.factor(ifelse( + x1 <= 500, "Low", sample(c("Middle", "High"), 1000, replace = TRUE) + ))) + dd <- transform(dd, x4 = as.factor(ifelse( + x1 > 500, "High", sample(c("Absent", "Low"), 1000, replace = TRUE) + ))) + dd <- transform(dd, z = z + re * 5) + expect_message({ + mod_TMB <- glmmTMB::glmmTMB( + z ~ x1 + x2 + x3 + x4 + (1 | re), + data = dd, + start = list(theta = 3), + control = glmmTMB::glmmTMBControl(rank_check = "adjust") + ) + }) + expect_equal( + icc(mod_TMB), + data.frame( + ICC_adjusted = 0.995480998331767, + ICC_conditional = 0.244468078371849, + ICC_unadjusted = 0.244468078371849 + ), + ignore_attr = TRUE, + tolerance = 1e-4 + ) + expect_equal( + r2(mod_TMB), + list( + R2_conditional = c(`Conditional R2` = 0.998890233308478), + R2_marginal = c(`Marginal R2` = 0.754422154936629) + ), + ignore_attr = TRUE, + tolerance = 1e-4 + ) +})