Skip to content

Commit

Permalink
check for NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 22, 2023
1 parent 344c02c commit dd3dfac
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 48 deletions.
12 changes: 7 additions & 5 deletions tests/testthat/test-GLMMadaptive.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ skip_if_not_installed("lme4")
m <- download_model("GLMMadaptive_zi_2")
m2 <- download_model("GLMMadaptive_zi_1")

skip_if(is.null(m))
skip_if(is.null(m2))

data(cbpp, package = "lme4")
tmp <<- cbpp
m3 <- GLMMadaptive::mixed_model(
Expand Down Expand Up @@ -68,7 +71,7 @@ test_that("find_predictors", {
)
expect_identical(
find_predictors(m, effects = "all")$zero_inflated_random,
c("persons")
"persons"
)
expect_identical(find_predictors(m, effects = "random")$random, "persons")
expect_identical(
Expand Down Expand Up @@ -167,15 +170,14 @@ test_that("clean_names", {

test_that("find_formula", {
expect_length(find_formula(m), 4)
expect_identical(
names(find_formula(m)),
expect_named(
find_formula(m),
c(
"conditional",
"random",
"zero_inflated",
"zero_inflated_random"
),
ignore_attr = TRUE
)
)
})

Expand Down
91 changes: 48 additions & 43 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
skip_on_cran()
skip_if_offline()
skip_if_not_installed("brms")

Expand All @@ -12,34 +13,38 @@ m6 <- insight::download_model("brms_corr_re1")
m7 <- suppressWarnings(insight::download_model("brms_mixed_8"))
m8 <- insight::download_model("brms_ordinal_1")

all_loaded <- !vapply(list(m1, m2, m3, m4, m5, m6, m7, m8), is.null, TRUE)
skip_if(!all(all_loaded))

# Tests -------------------------------------------------------------------
test_that("get_predicted.brmsfit: ordinal dv", {
skip_if_not_installed("bayestestR")
skip_if_not_installed("rstantools")

pred1 <- get_predicted(m8, ci = 0.95)
pred2 <- get_predicted(m8, ci_method = "hdi", ci = 0.95)
expect_true(inherits(pred1, "get_predicted"))
expect_true(inherits(pred1, "data.frame"))
expect_s3_class(pred1, "get_predicted")
expect_s3_class(pred1, "data.frame")
expect_true(all(c("Row", "Response") %in% colnames(pred1)))

# ci_method changes intervals but not se or predicted
pred1 <- data.frame(pred1)
pred2 <- data.frame(pred2)
expect_equal(pred1$Row, pred2$Row)
expect_equal(pred1$Response, pred2$Response)
expect_equal(pred1$Predicted, pred2$Predicted)
expect_equal(pred1$SE, pred2$SE)
expect_equal(pred1$Row, pred2$Row, ignore_attr = TRUE)
expect_equal(pred1$Response, pred2$Response, ignore_attr = TRUE)
expect_equal(pred1$Predicted, pred2$Predicted, ignore_attr = TRUE)
expect_equal(pred1$SE, pred2$SE, ignore_attr = TRUE)
expect_false(mean(pred1$CI_low == pred2$CI_low) > 0.1) # most CI bounds are different
expect_false(mean(pred1$CI_high == pred2$CI_high) > 0.1) # most CI bounds are different

# compare to manual predictions
pred3 <- get_predicted(m8, centrality_function = stats::median, ci = 0.95)
manual <- rstantools::posterior_epred(m8)
manual <- apply(manual[, , 1], 2, median)
expect_equal(pred3$Predicted[1:32], manual)
expect_equal(pred3$Predicted[1:32], manual, ignore_attr = TRUE)
manual <- rstantools::posterior_epred(m8)
manual <- apply(manual[, , 1], 2, mean)

Check warning on line 46 in tests/testthat/test-brms.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-brms.R,line=46,col=13,[matrix_apply_linter] Use rowMeans(colMeans(manual[,,1])) or colMeans(manual[,,1]) if manual[,,1] has 2 dimensions rather than apply(manual[, , 1], 2, mean)
expect_equal(pred1$Predicted[1:32], manual)
expect_equal(pred1$Predicted[1:32], manual, ignore_attr = TRUE)
})

test_that("find_statistic", {
Expand All @@ -51,8 +56,8 @@ test_that("find_statistic", {
})

test_that("n_parameters", {
expect_equal(n_parameters(m1), 65)
expect_equal(n_parameters(m1, effects = "fixed"), 5)
expect_identical(n_parameters(m1), 65L)
expect_identical(n_parameters(m1, effects = "fixed"), 5L)
})

test_that("model_info", {
Expand Down Expand Up @@ -161,36 +166,36 @@ test_that("find_predictors", {
})

test_that("find_response", {
expect_equal(find_response(m1, combine = TRUE), "count")
expect_equal(
expect_identical(find_response(m1, combine = TRUE), "count")
expect_identical(
find_response(m2, combine = TRUE),
c(SepalLength = "Sepal.Length", SepalWidth = "Sepal.Width")
)
expect_equal(find_response(m3, combine = TRUE), c("r", "n"))
expect_equal(find_response(m1, combine = FALSE), "count")
expect_equal(
expect_identical(find_response(m3, combine = TRUE), c("r", "n"))
expect_identical(find_response(m1, combine = FALSE), "count")
expect_identical(
find_response(m2, combine = FALSE),
c(SepalLength = "Sepal.Length", SepalWidth = "Sepal.Width")
)
expect_equal(find_response(m3, combine = FALSE), c("r", "n"))
expect_equal(find_response(m4, combine = FALSE), "count")
expect_equal(
expect_identical(find_response(m3, combine = FALSE), c("r", "n"))
expect_identical(find_response(m4, combine = FALSE), "count")
expect_identical(
find_response(m5, combine = TRUE),
c(count = "count", count2 = "count2")
)
})

test_that("get_response", {
expect_length(get_response(m1), 236)
expect_equal(ncol(get_response(m2)), 2)
expect_equal(
expect_identical(ncol(get_response(m2)), 2L)
expect_identical(
colnames(get_response(m2)),
c("Sepal.Length", "Sepal.Width")
)
expect_equal(ncol(get_response(m3)), 2)
expect_equal(colnames(get_response(m3)), c("r", "n"))
expect_identical(ncol(get_response(m3)), 2L)
expect_identical(colnames(get_response(m3)), c("r", "n"))
expect_length(get_response(m4), 250)
expect_equal(colnames(get_response(m5)), c("count", "count2"))
expect_identical(colnames(get_response(m5)), c("count", "count2"))
})

test_that("find_variables", {
Expand Down Expand Up @@ -264,16 +269,16 @@ test_that("find_variables", {
})

test_that("n_obs", {
expect_equal(n_obs(m1), 236)
expect_equal(n_obs(m2), 150)
expect_equal(n_obs(m3), 10)
expect_equal(n_obs(m4), 250)
expect_equal(n_obs(m5), 250)
expect_identical(n_obs(m1), 236L)
expect_identical(n_obs(m2), 150L)
expect_identical(n_obs(m3), 10L)
expect_identical(n_obs(m4), 250L)
expect_identical(n_obs(m5), 250L)
})


test_that("find_random", {
expect_equal(find_random(m5), list(
expect_identical(find_random(m5), list(
count = list(
random = "persons",
zero_inflated_random = "persons"
Expand All @@ -283,8 +288,8 @@ test_that("find_random", {
zero_inflated_random = "persons"
)
))
expect_equal(find_random(m5, flatten = TRUE), "persons")
expect_equal(find_random(m6, flatten = TRUE), "id")
expect_identical(find_random(m5, flatten = TRUE), "persons")
expect_identical(find_random(m6, flatten = TRUE), "id")
})


Expand All @@ -302,7 +307,7 @@ test_that("get_data", {


test_that("find_paramaters", {
expect_equal(
expect_identical(
find_parameters(m1),
list(
conditional = c(
Expand All @@ -316,7 +321,7 @@ test_that("find_paramaters", {
)
)

expect_equal(
expect_identical(
find_parameters(m2),
structure(
list(
Expand All @@ -343,7 +348,7 @@ test_that("find_paramaters", {
)
)

expect_equal(
expect_identical(
find_parameters(m4),
list(
conditional = c("b_Intercept", "b_child", "b_camper"),
Expand All @@ -353,7 +358,7 @@ test_that("find_paramaters", {
)
)

expect_equal(
expect_identical(
find_parameters(m5, effects = "all"),
structure(
list(
Expand All @@ -380,7 +385,7 @@ test_that("find_paramaters", {
})

test_that("find_paramaters", {
expect_equal(
expect_identical(
colnames(get_parameters(m4)),
c(
"b_Intercept",
Expand All @@ -391,11 +396,11 @@ test_that("find_paramaters", {
"b_zi_camper"
)
)
expect_equal(
expect_identical(
colnames(get_parameters(m4, component = "zi")),
c("b_zi_Intercept", "b_zi_child", "b_zi_camper")
)
expect_equal(
expect_identical(
colnames(get_parameters(m4, effects = "all")),
c(
"b_Intercept", "b_child", "b_camper", "r_persons[1,Intercept]",
Expand All @@ -405,14 +410,14 @@ test_that("find_paramaters", {
"r_persons__zi[4,Intercept]", "sd_persons__zi_Intercept"
)
)
expect_equal(
expect_identical(
colnames(get_parameters(m4, effects = "random", component = "conditional")),
c(
"r_persons[1,Intercept]", "r_persons[2,Intercept]", "r_persons[3,Intercept]",
"r_persons[4,Intercept]", "sd_persons__Intercept"
)
)
expect_equal(
expect_identical(
colnames(get_parameters(m5, effects = "random", component = "conditional")),
c(
"r_persons__count[1,Intercept]", "r_persons__count[2,Intercept]",
Expand All @@ -423,7 +428,7 @@ test_that("find_paramaters", {
)
)

expect_equal(
expect_identical(
colnames(get_parameters(m5, effects = "all", component = "all")),
c(
"b_count_Intercept", "b_count_child", "b_count_camper", "r_persons__count[1,Intercept]",
Expand Down Expand Up @@ -468,7 +473,7 @@ test_that("is_multivariate", {
})

test_that("find_terms", {
expect_equal(
expect_identical(
find_terms(m2),
list(
SepalLength = list(
Expand All @@ -484,7 +489,7 @@ test_that("find_terms", {
})

test_that("find_algorithm", {
expect_equal(
expect_identical(
find_algorithm(m1),
list(
algorithm = "sampling",
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-format_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ skip_if_not_installed("bayestestR")

# test for bayesian models -----------------
m1 <- insight::download_model("stanreg_glm_1")
skip_if(is.null(m1))

set.seed(123)
x <- suppressWarnings(as.data.frame(bayestestR::describe_posterior(m1, test = c("pd", "bf"))))

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ m1 <- mgcv::gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat2)
m2 <- download_model("gam_zi_1")
m3 <- download_model("gam_mv_1")

skip_if(is.null(m2))
skip_if(is.null(m3))

test_that("model_info", {
expect_true(model_info(m1)$is_linear)
expect_true(model_info(m2)$is_count)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-get_loglikelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ test_that("get_loglikelihood - (g)lmer", {
expect_equal(as.numeric(ll), as.numeric(ll2))

model <- download_model("lmerMod_1")
skip_if(is.null(model))
expect_equal(get_loglikelihood(model, estimator = "REML"), logLik(model, REML = TRUE), tolerance = 0.01, ignore_attr = TRUE)
expect_equal(get_loglikelihood(model, estimator = "ML"), logLik(model, REML = FALSE), tolerance = 0.01, ignore_attr = TRUE)

model <- download_model("merMod_1")
skip_if(is.null(model))
expect_equal(get_loglikelihood(model, estimator = "REML"), logLik(model, REML = FALSE), tolerance = 0.01, ignore_attr = TRUE)
expect_equal(get_loglikelihood(model, estimator = "ML"), logLik(model, REML = FALSE), tolerance = 0.01, ignore_attr = TRUE)
})
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-mvrstanarm.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ skip_if_not_installed("rstanarm")

data("pbcLong", package = "rstanarm")
m1 <- download_model("stanmvreg_1")
skip_if(is.null(m1))

test_that("clean_names", {
expect_identical(
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-rstanarm.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ m1 <- insight::download_model("stanreg_merMod_5")
m2 <- insight::download_model("stanreg_glm_6")
m3 <- insight::download_model("stanreg_glm_1")

skip_if(is.null(m1))
skip_if(is.null(m2))
skip_if(is.null(m3))

data("puzzles", package = "BayesFactor")
m4 <- suppressWarnings(
stan_glm(
Expand All @@ -38,6 +42,7 @@ m5 <- suppressWarnings(
)
)
m6 <- insight::download_model("stanreg_gamm4_1")
skip_if(is.null(m6))

m7 <- suppressWarnings(
stan_lm(mpg ~ wt + qsec + am,
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ d <- data.frame(
dat <<- d

m1 <- download_model("glmmTMB_spatial_1")
skip_if(is.null(m1))

test_that("find_weights", {
expect_null(find_weights(m1))
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-vgam.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ data("hunua", package = "VGAM")
m1 <- download_model("vgam_1")
m2 <- download_model("vgam_2")

skip_if(is.null(m1))
skip_if(is.null(m2))

test_that("model_info", {
expect_true(model_info(m1)$is_binomial)
expect_true(model_info(m2)$is_binomial)
Expand Down

0 comments on commit dd3dfac

Please sign in to comment.