From b69b18c602122cae67c7fd1f022b27ea13544034 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 24 Dec 2024 10:18:55 +0100 Subject: [PATCH] scoping issue --- tests/testthat/test-r2_mcfadden.R | 68 +++++++++++++++++-------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/tests/testthat/test-r2_mcfadden.R b/tests/testthat/test-r2_mcfadden.R index cb6ee9d3a..68cbe6339 100644 --- a/tests/testthat/test-r2_mcfadden.R +++ b/tests/testthat/test-r2_mcfadden.R @@ -27,37 +27,43 @@ test_that("r2_mcfadden", { ) }) +skip_if_not_installed("withr") -test_that("r2_mcfadden, glmmTMB-beta-binomial", { - skip_if_not_installed("glmmTMB") - set.seed(101) - dd <- data.frame(x = rnorm(200)) - dd$y <- glmmTMB::simulate_new( - ~ 1 + x, - newdata = dd, - newparams = list(beta = c(0,1), betadisp = -1), - weights = rep(10, nrow(dd)), - family = glmmTMB::betabinomial() - )[[1]] - dd$success <- round(runif(nrow(dd), 0, dd$y)) +withr::with_environment( + new.env(), + { + test_that("r2_mcfadden, glmmTMB-beta-binomial", { + skip_if_not_installed("glmmTMB") + set.seed(101) + dd <- data.frame(x = rnorm(200)) + dd$y <- glmmTMB::simulate_new( + ~ 1 + x, + newdata = dd, + newparams = list(beta = c(0,1), betadisp = -1), + weights = rep(10, nrow(dd)), + family = glmmTMB::betabinomial() + )[[1]] + dd$success <- round(runif(nrow(dd), 0, dd$y)) - m <- glmmTMB::glmmTMB( - y/10 ~ 1 + x, - data = dd, - weights = rep(10, nrow(dd)), - family = glmmTMB::betabinomial() - ) - out1 <- r2(m) - out2 <- r2_mcfadden(m) - expect_equal(out1$R2, out2$R2, tolerance = 1e-4, ignore_attr = TRUE) - expect_equal(out1$R2, 0.06892733, tolerance = 1e-4, ignore_attr = TRUE) + m <- glmmTMB::glmmTMB( + y/10 ~ 1 + x, + data = dd, + weights = rep(10, nrow(dd)), + family = glmmTMB::betabinomial() + ) + out1 <- r2(m) + out2 <- r2_mcfadden(m) + expect_equal(out1$R2, out2$R2, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out1$R2, 0.06892733, tolerance = 1e-4, ignore_attr = TRUE) - m <- glmmTMB::glmmTMB( - cbind(y, success) ~ 1 + x, - data = dd, - weights = rep(10, nrow(dd)), - family = glmmTMB::betabinomial() - ) - expect_warning(r2(m), regex = "calculate accurate") - expect_warning(r2_mcfadden(m), regex = "calculate accurate") -}) + m <- glmmTMB::glmmTMB( + cbind(y, success) ~ 1 + x, + data = dd, + weights = rep(10, nrow(dd)), + family = glmmTMB::betabinomial() + ) + expect_warning(r2(m), regex = "calculate accurate") + expect_warning(r2_mcfadden(m), regex = "calculate accurate") + }) + } +)