diff --git a/tests/testthat/test-backticks.R b/tests/testthat/test-backticks.R index 955c882ec..261d21c5b 100644 --- a/tests/testthat/test-backticks.R +++ b/tests/testthat/test-backticks.R @@ -1,122 +1,110 @@ -iris$`a m` <- iris$Species -iris$`Sepal Width` <- iris$Sepal.Width -dat <<- iris -m <- lm(`Sepal Width` ~ Petal.Length + `a m` * log(Sepal.Length), data = dat) -m2 <- lm(`Sepal Width` ~ Petal.Length + `a m`, data = dat) - -test_that("text_remove_backticks", { - d <- data.frame(Parameter = names(coef(m2)), Estimate = unname(coef(m2)), stringsAsFactors = FALSE) - expect_equal( - text_remove_backticks(d)$Parameter, - c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica") - ) - - d <- data.frame( - Parameter = names(coef(m2)), - Term = names(coef(m2)), - Estimate = unname(coef(m2)), - stringsAsFactors = FALSE - ) - x <- text_remove_backticks(d, c("Parameter", "Term")) - expect_equal(x$Parameter, c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica")) - expect_equal(x$Term, c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica")) - - d <- list(Parameter = names(coef(m2)), Estimate = unname(coef(m2))) - expect_warning(text_remove_backticks(d, verbose = TRUE)) - expect_equal( - text_remove_backticks(d), - list( - Parameter = c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica"), - Estimate = c(2.99186937324135, 0.298310962215218, -1.49267407227818, -1.67409183546024) - ), - tolerance = 1e-3 - ) -}) - -test_that("backticks", { - expect_equal( - find_parameters(m), - list(conditional = c( - "(Intercept)", "Petal.Length", "a mversicolor", - "a mvirginica", "log(Sepal.Length)", "a mversicolor:log(Sepal.Length)", - "a mvirginica:log(Sepal.Length)" - )) - ) - - expect_equal( - get_parameters(m)$Parameter, - c( - "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", "log(Sepal.Length)", - "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" +skip_if_not_installed("withr") + +withr::with_environment( + new.env(), + test_that("text_remove_backticks", { + iris$`a m` <- iris$Species + iris$`Sepal Width` <- iris$Sepal.Width + dat <- iris + m <- lm(`Sepal Width` ~ Petal.Length + `a m` * log(Sepal.Length), data = dat) + m2 <- lm(`Sepal Width` ~ Petal.Length + `a m`, data = dat) + + d <- data.frame(Parameter = names(coef(m2)), Estimate = unname(coef(m2)), stringsAsFactors = FALSE) + expect_identical( + text_remove_backticks(d)$Parameter, + c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica") ) - ) - expect_equal( - get_statistic(m)$Parameter, - c( - "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", "log(Sepal.Length)", - "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" + d <- data.frame( + Parameter = names(coef(m2)), + Term = names(coef(m2)), + Estimate = unname(coef(m2)), + stringsAsFactors = FALSE ) - ) - - expect_equal( - clean_parameters(m)$Parameter, - c( - "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", "log(Sepal.Length)", - "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" + x <- text_remove_backticks(d, c("Parameter", "Term")) + expect_identical(x$Parameter, c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica")) + expect_identical(x$Term, c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica")) + + d <- list(Parameter = names(coef(m2)), Estimate = unname(coef(m2))) + expect_warning(text_remove_backticks(d, verbose = TRUE)) + expect_equal( + text_remove_backticks(d), + list( + Parameter = c("(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica"), + Estimate = c(2.99186937324135, 0.298310962215218, -1.49267407227818, -1.67409183546024) + ), + tolerance = 1e-3 ) - ) - - expect_equal( - find_predictors(m), - list(conditional = c("Petal.Length", "a m", "Sepal.Length")) - ) - - expect_equal( - colnames(get_predictors(m)), - c("Petal.Length", "a m", "Sepal.Length") - ) - - expect_equal( - find_variables(m), - list( - response = "Sepal Width", - conditional = c("Petal.Length", "a m", "Sepal.Length") + expect_identical( + find_parameters(m), + list(conditional = c( + "(Intercept)", "Petal.Length", "a mversicolor", + "a mvirginica", "log(Sepal.Length)", "a mversicolor:log(Sepal.Length)", + "a mvirginica:log(Sepal.Length)" + )) ) - ) - - expect_equal( - find_terms(m), - list( - response = "Sepal Width", - conditional = c("Petal.Length", "a m", "log(Sepal.Length)") + expect_identical( + get_parameters(m)$Parameter, + c( + "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", "log(Sepal.Length)", + "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" + ) ) - ) - - expect_equal( - rownames(get_varcov(m)), - c( - "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", - "log(Sepal.Length)", "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" + expect_identical( + get_statistic(m)$Parameter, + c( + "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", "log(Sepal.Length)", + "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" + ) ) - ) - - expect_equal( - clean_names(m), - c("Sepal Width", "Petal.Length", "a m", "Sepal.Length") - ) - - expect_equal(find_response(m), "Sepal Width") - - expect_equal(get_response(m), iris[["Sepal Width"]]) -}) + expect_identical( + clean_parameters(m)$Parameter, + c( + "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", "log(Sepal.Length)", + "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" + ) + ) + expect_identical( + find_predictors(m), + list(conditional = c("Petal.Length", "a m", "Sepal.Length")) + ) + expect_identical( + find_variables(m), + list( + response = "Sepal Width", + conditional = c("Petal.Length", "a m", "Sepal.Length") + ) + ) + expect_identical( + find_terms(m), + list( + response = "Sepal Width", + conditional = c("Petal.Length", "a m", "log(Sepal.Length)") + ) + ) + expect_identical( + rownames(get_varcov(m)), + c( + "(Intercept)", "Petal.Length", "a mversicolor", "a mvirginica", + "log(Sepal.Length)", "a mversicolor:log(Sepal.Length)", "a mvirginica:log(Sepal.Length)" + ) + ) + expect_identical( + clean_names(m), + c("Sepal Width", "Petal.Length", "a m", "Sepal.Length") + ) + expect_identical(find_response(m), "Sepal Width") + expect_identical(get_response(m), iris[["Sepal Width"]]) + expect_named(get_predictors(m), c("Petal.Length", "a m", "Sepal.Length")) + }) +) test_that("text_remove_backticks, character", { x <- "`test`" - expect_equal(text_remove_backticks(x), "test") + expect_identical(text_remove_backticks(x), "test") x <- "test" - expect_equal(text_remove_backticks(x), "test") + expect_identical(text_remove_backticks(x), "test") x <- NULL expect_null(text_remove_backticks(x)) }) @@ -125,13 +113,13 @@ test_that("text_remove_backticks, character", { test_that("text_remove_backticks, matrix", { x <- matrix(1:9, nrow = 3) out <- text_remove_backticks(x) - expect_equal(dimnames(x), dimnames(out)) + expect_identical(dimnames(x), dimnames(out)) colnames(x) <- rownames(x) <- 1:3 out <- text_remove_backticks(x) - expect_equal(dimnames(x), dimnames(out)) + expect_identical(dimnames(x), dimnames(out)) colnames(x) <- rownames(x) <- paste0("`", 1:3, "`") out <- text_remove_backticks(x) - expect_equal(list(as.character(1:3), as.character(1:3)), dimnames(out)) + expect_identical(list(as.character(1:3), as.character(1:3)), dimnames(out)) }) diff --git a/tests/testthat/test-coxme.R b/tests/testthat/test-coxme.R index 5d71b6a7a..e211a94e0 100644 --- a/tests/testthat/test-coxme.R +++ b/tests/testthat/test-coxme.R @@ -3,198 +3,186 @@ skip_if_not_installed("lme4") skip_if_not_installed("nlme") skip_if_not_installed("bdsmatrix") skip_if_not_installed("coxme") - -lung <- survival::lung -Surv <- survival::Surv - -set.seed(1234) -lung$inst2 <- sample(1:10, size = nrow(lung), replace = TRUE) -lung <- subset(lung, subset = ph.ecog %in% 0:2) -lung$ph.ecog <- factor(lung$ph.ecog, labels = c("good", "ok", "limited")) - -d <<- lung - -m1 <- coxme::coxme(Surv(time, status) ~ ph.ecog + age + (1 | inst), d) -m2 <- coxme::coxme(Surv(time, status) ~ ph.ecog + age + (1 | inst) + (1 | inst2), d) - -test_that("model_info", { - expect_true(model_info(m1)$is_logit) - expect_false(model_info(m1)$is_linear) -}) - -test_that("find_predictors", { - expect_identical(find_predictors(m1), list(conditional = c("ph.ecog", "age"))) - expect_identical( - find_predictors(m1, effects = "random"), - list(random = "inst") - ) - expect_identical(find_predictors(m2), list(conditional = c("ph.ecog", "age"))) - expect_identical(find_predictors(m2, effects = "random"), list(random = c("inst", "inst2"))) -}) - -test_that("find_response", { - expect_identical(find_response(m1), "Surv(time, status)") - expect_identical(find_response(m1, combine = FALSE), c("time", "status")) -}) - -test_that("link_inverse", { - expect_equal(link_inverse(m1)(0.2), plogis(0.2), tolerance = 1e-5) - expect_equal(link_inverse(m2)(0.2), plogis(0.2), tolerance = 1e-5) -}) - -test_that("get_data", { - expect_equal(nrow(get_data(m1)), 225) - expect_equal( - colnames(get_data(m1)), - c( - "time", - "status", - "ph.ecog", - "age", - "inst" - ) - ) - expect_equal( - colnames(get_data(m2)), - c( - "time", - "status", - "ph.ecog", - "age", - "inst", - "inst2" - ) - ) -}) - -test_that("find_formula", { - expect_length(find_formula(m1), 2) - expect_equal( - find_formula(m1), - list( - conditional = as.formula("Surv(time, status) ~ ph.ecog + age"), - random = as.formula("~1 | inst") - ), - ignore_attr = TRUE - ) - - expect_length(find_formula(m2), 2) - expect_equal( - find_formula(m2), - list( - conditional = as.formula("Surv(time, status) ~ ph.ecog + age"), - random = list(as.formula("~1 | inst"), as.formula("~1 | inst2")) - ), - ignore_attr = TRUE - ) -}) - -test_that("find_terms", { - expect_equal( - find_terms(m1), - list( - response = "Surv(time, status)", - conditional = c("ph.ecog", "age"), - random = "inst" - ) - ) - expect_equal( - find_terms(m1, flatten = TRUE), - c("Surv(time, status)", "ph.ecog", "age", "inst") - ) - expect_equal( - find_terms(m2), - list( - response = "Surv(time, status)", - conditional = c("ph.ecog", "age"), - random = c("inst", "inst2") - ) - ) - expect_equal( - find_terms(m2, flatten = TRUE), - c("Surv(time, status)", "ph.ecog", "age", "inst", "inst2") - ) -}) - -test_that("find_variables", { - expect_equal( - find_variables(m1), - list( - response = c("time", "status"), - conditional = c("ph.ecog", "age"), - random = "inst" - ) - ) - expect_equal( - find_variables(m1, flatten = TRUE), - c("time", "status", "ph.ecog", "age", "inst") - ) - expect_equal( - find_variables(m2), - list( - response = c("time", "status"), - conditional = c("ph.ecog", "age"), - random = c("inst", "inst2") - ) - ) - expect_equal( - find_variables(m2, flatten = TRUE), - c("time", "status", "ph.ecog", "age", "inst", "inst2") - ) -}) - -test_that("n_obs", { - expect_equal(n_obs(m1), 225) - expect_equal(n_obs(m2), 225) -}) - -test_that("get_response", { - expect_equal(colnames(get_response(m1)), c("time", "status")) - expect_equal(nrow(get_response(m1)), 225) - expect_equal(colnames(get_response(m1)), c("time", "status")) - expect_equal(nrow(get_response(m2)), 225) -}) - -test_that("linkfun", { - expect_false(is.null(link_function(m1))) - expect_false(is.null(link_function(m2))) -}) - -test_that("is_multivariate", { - expect_false(is_multivariate(m1)) -}) - -test_that("find_parameters", { - expect_equal( - find_parameters(m1), - list( - conditional = c("ph.ecogok", "ph.ecoglimited", "age"), - random = "inst" - ) - ) - expect_equal( - find_parameters(m2), - list( - conditional = c("ph.ecogok", "ph.ecoglimited", "age"), - random = c("inst", "inst2") - ) - ) - expect_equal(nrow(get_parameters(m1)), 3) - expect_equal( - get_parameters(m1)$Parameter, - c("ph.ecogok", "ph.ecoglimited", "age") - ) - - expect_equal(nrow(get_parameters(m2)), 3) - expect_equal( - get_parameters(m2)$Parameter, - c("ph.ecogok", "ph.ecoglimited", "age") - ) - - expect_length(get_parameters(m2, effects = "random"), 2) -}) - -test_that("find_statistic", { - expect_identical(find_statistic(m1), "z-statistic") - expect_identical(find_statistic(m2), "z-statistic") -}) +skip_if_not_installed("withr") + +withr::with_environment( + new.env(), + { + lung <- survival::lung + Surv <- survival::Surv + + set.seed(1234) + lung$inst2 <- sample(1:10, size = nrow(lung), replace = TRUE) + lung <- subset(lung, subset = ph.ecog %in% 0:2) + lung$ph.ecog <- factor(lung$ph.ecog, labels = c("good", "ok", "limited")) + + d <<- lung + + m1 <- coxme::coxme(Surv(time, status) ~ ph.ecog + age + (1 | inst), d) + m2 <- coxme::coxme(Surv(time, status) ~ ph.ecog + age + (1 | inst) + (1 | inst2), d) + + test_that("model_info", { + expect_true(model_info(m1)$is_logit) + expect_false(model_info(m1)$is_linear) + }) + + test_that("find_predictors", { + expect_identical(find_predictors(m1), list(conditional = c("ph.ecog", "age"))) + expect_identical( + find_predictors(m1, effects = "random"), + list(random = "inst") + ) + expect_identical(find_predictors(m2), list(conditional = c("ph.ecog", "age"))) + expect_identical(find_predictors(m2, effects = "random"), list(random = c("inst", "inst2"))) + }) + + test_that("find_response", { + expect_identical(find_response(m1), "Surv(time, status)") + expect_identical(find_response(m1, combine = FALSE), c("time", "status")) + }) + + test_that("link_inverse", { + expect_equal(link_inverse(m1)(0.2), plogis(0.2), tolerance = 1e-5) + expect_equal(link_inverse(m2)(0.2), plogis(0.2), tolerance = 1e-5) + }) + + test_that("get_data", { + expect_identical(nrow(get_data(m1)), 225L) + expect_named(get_data(m1), c("time", "status", "ph.ecog", "age", "inst")) + expect_named(get_data(m2), c("time", "status", "ph.ecog", "age", "inst", "inst2")) + }) + + test_that("find_formula", { + expect_length(find_formula(m1), 2) + expect_equal( + find_formula(m1), + list( + conditional = as.formula("Surv(time, status) ~ ph.ecog + age"), + random = as.formula("~1 | inst") + ), + ignore_attr = TRUE + ) + + expect_length(find_formula(m2), 2) + expect_equal( + find_formula(m2), + list( + conditional = as.formula("Surv(time, status) ~ ph.ecog + age"), + random = list(as.formula("~1 | inst"), as.formula("~1 | inst2")) + ), + ignore_attr = TRUE + ) + }) + + test_that("find_terms", { + expect_identical( + find_terms(m1), + list( + response = "Surv(time, status)", + conditional = c("ph.ecog", "age"), + random = "inst" + ) + ) + expect_identical( + find_terms(m1, flatten = TRUE), + c("Surv(time, status)", "ph.ecog", "age", "inst") + ) + expect_identical( + find_terms(m2), + list( + response = "Surv(time, status)", + conditional = c("ph.ecog", "age"), + random = c("inst", "inst2") + ) + ) + expect_identical( + find_terms(m2, flatten = TRUE), + c("Surv(time, status)", "ph.ecog", "age", "inst", "inst2") + ) + }) + + test_that("find_variables", { + expect_identical( + find_variables(m1), + list( + response = c("time", "status"), + conditional = c("ph.ecog", "age"), + random = "inst" + ) + ) + expect_identical( + find_variables(m1, flatten = TRUE), + c("time", "status", "ph.ecog", "age", "inst") + ) + expect_identical( + find_variables(m2), + list( + response = c("time", "status"), + conditional = c("ph.ecog", "age"), + random = c("inst", "inst2") + ) + ) + expect_identical( + find_variables(m2, flatten = TRUE), + c("time", "status", "ph.ecog", "age", "inst", "inst2") + ) + }) + + test_that("n_obs", { + expect_identical(n_obs(m1), 225) + expect_identical(n_obs(m2), 225) + }) + + test_that("get_response", { + expect_named(get_response(m1), c("time", "status")) + expect_identical(nrow(get_response(m1)), 225L) + expect_named(get_response(m1), c("time", "status")) + expect_identical(nrow(get_response(m2)), 225L) + }) + + test_that("linkfun", { + expect_false(is.null(link_function(m1))) + expect_false(is.null(link_function(m2))) + }) + + test_that("is_multivariate", { + expect_false(is_multivariate(m1)) + }) + + test_that("find_parameters", { + expect_identical( + find_parameters(m1), + list( + conditional = c("ph.ecogok", "ph.ecoglimited", "age"), + random = "inst" + ) + ) + expect_identical( + find_parameters(m2), + list( + conditional = c("ph.ecogok", "ph.ecoglimited", "age"), + random = c("inst", "inst2") + ) + ) + expect_identical(nrow(get_parameters(m1)), 3L) + expect_identical( + get_parameters(m1)$Parameter, + c("ph.ecogok", "ph.ecoglimited", "age") + ) + + expect_identical(nrow(get_parameters(m2)), 3L) + expect_identical( + get_parameters(m2)$Parameter, + c("ph.ecogok", "ph.ecoglimited", "age") + ) + + expect_length(get_parameters(m2, effects = "random"), 2) + }) + + test_that("find_statistic", { + expect_identical(find_statistic(m1), "z-statistic") + expect_identical(find_statistic(m2), "z-statistic") + }) + + } +) diff --git a/tests/testthat/test-coxph.R b/tests/testthat/test-coxph.R index 2539e195b..e2af4da30 100644 --- a/tests/testthat/test-coxph.R +++ b/tests/testthat/test-coxph.R @@ -1,6 +1,7 @@ skip_if_not_installed("survival") skip_if_not_installed("insight") skip_if_not_installed("JM") +skip_if_not_installed("withr") lung <- subset(survival::lung, subset = ph.ecog %in% 0:2) lung$sex <- factor(lung$sex, labels = c("male", "female")) @@ -36,41 +37,44 @@ test_that("get_data", { ## NOTE check back every now and then and see if tests still work skip("works interactively") expect_s3_class(get_data(m1), "data.frame") - expect_equal(dim(get_data(m1)), c(166, 10)) + expect_identical(dim(get_data(m1)), c(166L, 10L)) }) -test_that("get_data: regression test for previous bug", { - ## NOTE check back every now and then and see if tests still work - skip("works interactively") - dat_regression_test <- data.frame( - time = c(4, 3, 1, 1, 2, 2, 3), - status = c(1, 1, 1, 0, 1, 1, 0), - x = c(0, 2, 1, 1, 1, 0, 0), - sex = c(0, 0, 0, 0, 1, 1, 1) - ) - mod <- survival::coxph(Surv(time, status) ~ x + strata(sex), - data = dat_regression_test, - ties = "breslow" - ) - expect_equal(get_data(mod), dat_regression_test, ignore_attr = TRUE) -}) - -test_that("get_data: regression test for data stored as list", { - ## NOTE check back every now and then and see if tests still work - skip("works interactively") - dat_regression_test <- list( - time = c(4, 3, 1, 1, 2, 2, 3), - status = c(1, 1, 1, 0, 1, 1, 0), - x = c(0, 2, 1, 1, 1, 0, 0), - sex = c(0, 0, 0, 0, 1, 1, 1) - ) - mod <- coxph(Surv(time, status) ~ x + strata(sex), - data = dat_regression_test, - ties = "breslow" - ) - expect_equal(get_data(mod), as.data.frame(dat_regression_test), ignore_attr = TRUE) -}) +withr::with_environment( + new.env(), + test_that("get_data: regression test for previous bug", { + dat_regression_test <- data.frame( + time = c(4, 3, 1, 1, 2, 2, 3), + status = c(1, 1, 1, 0, 1, 1, 0), + x = c(0, 2, 1, 1, 1, 0, 0), + sex = c(0, 0, 0, 0, 1, 1, 1) + ) + mod <- survival::coxph(Surv(time, status) ~ x + strata(sex), + data = dat_regression_test, + ties = "breslow" + ) + expect_equal(get_data(mod), dat_regression_test, ignore_attr = TRUE) + }) +) + + +withr::with_environment( + new.env(), + test_that("get_data: regression test for data stored as list", { + dat_regression_test <- list( + time = c(4, 3, 1, 1, 2, 2, 3), + status = c(1, 1, 1, 0, 1, 1, 0), + x = c(0, 2, 1, 1, 1, 0, 0), + sex = c(0, 0, 0, 0, 1, 1, 1) + ) + mod <- survival::coxph(Surv(time, status) ~ x + strata(sex), + data = dat_regression_test, + ties = "breslow" + ) + expect_equal(get_data(mod), as.data.frame(dat_regression_test), ignore_attr = TRUE) + }) +) test_that("find_formula", { @@ -85,18 +89,18 @@ test_that("find_formula", { }) test_that("find_variables", { - expect_equal(find_variables(m1), list( + expect_identical(find_variables(m1), list( response = c("time", "status"), conditional = c("sex", "age", "ph.ecog") )) - expect_equal( + expect_identical( find_variables(m1, flatten = TRUE), c("time", "status", "sex", "age", "ph.ecog") ) }) test_that("n_obs", { - expect_equal(n_obs(m1), 226) + expect_identical(n_obs(m1), 226L) }) test_that("linkfun", { @@ -108,21 +112,21 @@ test_that("is_multivariate", { }) test_that("find_parameters", { - expect_equal( + expect_identical( find_parameters(m1), list( conditional = c("sexfemale", "age", "ph.ecogok", "ph.ecoglimited") ) ) - expect_equal(nrow(get_parameters(m1)), 4) - expect_equal( + expect_identical(nrow(get_parameters(m1)), 4L) + expect_identical( get_parameters(m1)$Parameter, c("sexfemale", "age", "ph.ecogok", "ph.ecoglimited") ) }) test_that("find_terms", { - expect_equal( + expect_identical( find_terms(m1), list( response = "Surv(time, status)", @@ -139,9 +143,9 @@ test_that("JM", { data("aids", package = "JM") m <- survival::coxph(Surv(start, stop, event) ~ CD4, data = aids) d <- get_data(m) - expect_equal(dim(d), c(1405, 4)) - expect_equal(colnames(d), c("start", "stop", "event", "CD4")) - expect_equal(find_variables(m), list(response = c("start", "stop", "event"), conditional = "CD4")) + expect_identical(dim(d), c(1405L, 4L)) + expect_named(d, c("start", "stop", "event", "CD4")) + expect_identical(find_variables(m), list(response = c("start", "stop", "event"), conditional = "CD4")) }) test_that("get_statistic", { diff --git a/tests/testthat/test-fixest.R b/tests/testthat/test-fixest.R index 576f60db9..8048063eb 100644 --- a/tests/testthat/test-fixest.R +++ b/tests/testthat/test-fixest.R @@ -2,6 +2,7 @@ skip_on_os("mac") skip_if(getRversion() < "3.6.0") skip_if_not_installed("fixest") skip_if_not_installed("carData") +skip_if_not_installed("withr") # avoid warnings fixest::setFixest_nthreads(1) @@ -298,32 +299,33 @@ test_that("get_predicted", { expect_warning(get_predicted(m1, predict = NULL, type = "link"), NA) }) -test_that("get_data works when model data has name of reserved words", { - ## NOTE check back every now and then and see if tests still work - skip("works interactively") - rep <- data.frame(Y = runif(100) > 0.5, X = rnorm(100)) - m <- fixest::feglm(Y ~ X, data = rep, family = binomial) - out <- get_data(m) - expect_s3_class(out, "data.frame") - expect_equal( - head(out), - structure( - list( - Y = c(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE), + +withr::with_environment( + new.env(), + test_that("get_data works when model data has name of reserved words", { + set.seed(1234) + rep <- data.frame(Y = runif(100) > 0.5, X = rnorm(100)) + m <- fixest::feglm(Y ~ X, data = rep, family = binomial) + out <- get_data(m) + expect_s3_class(out, "data.frame") + expect_equal( + head(out), + data.frame( + Y = c(FALSE, TRUE, TRUE, TRUE, TRUE, TRUE), X = c( - -1.37601434046896, -0.0340090992175856, 0.418083058388383, - -0.51688491498936, -1.30634551903768, -0.858343109785566 + -1.80603125680195, -0.582075924689333, -1.10888962442678, + -1.01496200949201, -0.162309523556819, 0.563055818994517 ) ), - is_subset = FALSE, row.names = c(NA, 6L), class = "data.frame" - ), - ignore_attr = TRUE, - tolerance = 1e-3 - ) -}) + ignore_attr = TRUE, + tolerance = 1e-3 + ) + }) +) test_that("find_variables with interaction", { + data(mtcars) mod <- suppressMessages(fixest::feols(mpg ~ 0 | carb | vs:cyl ~ am:cyl, data = mtcars)) expect_equal( find_variables(mod), @@ -341,15 +343,14 @@ test_that("find_variables with interaction", { test_that("find_predictors with i(f1, i.f2) interaction", { + data(airquality) aq <- airquality aq$week <- aq$Day %/% 7 + 1 mod <- fixest::feols(Ozone ~ i(Month, i.week), aq, notes = FALSE) expect_equal( find_predictors(mod), - list( - conditional = c("Month", "week") - ), + list(conditional = c("Month", "week")), ignore_attr = TRUE ) })