Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect multivariate vgams/vglms #843

Merged
merged 8 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion R/is_multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ is_multivariate <- function(x) {
return(isTRUE(ncol(x$coefficients) > 1L))
}

return(FALSE)
vgam_classes <- c("vglm", "vgam")
if (inherits(x, vgam_classes)) {
return(isTRUE(x@extra$multiple.responses))
}

FALSE
}
3 changes: 2 additions & 1 deletion R/model_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' * `is_hurdle`: model has zero-inflation component and is a hurdle-model (truncated family distribution)
#' * `is_dispersion`: model has dispersion component (not only dispersion _parameter_)
#' * `is_mixed`: model is a mixed effects model (with random effects)
#' * `is_multivariate`: model is a multivariate response model (currently only works for _brmsfit_ objects)
#' * `is_multivariate`: model is a multivariate response model (currently only works for _brmsfit_ and _vglm/vgam_ objects)

Check warning on line 42 in R/model_info.R

View workflow job for this annotation

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

file=R/model_info.R,line=42,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 123 characters.
#' * `is_trial`: model response contains additional information about the trials
#' * `is_bayesian`: model is a Bayesian model
#' * `is_gam`: model is a generalized additive model
Expand Down Expand Up @@ -1092,6 +1092,7 @@
fitfam = faminfo@vfamily[1],
logit.link = any(.string_contains("logit", faminfo@blurb)),
link.fun = link.fun,
multi.var = is_multivariate(x),
...
)
}
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ unstandardizing
variates
vectorized
vgam
vglm
visualisation
warmup
warmups
Expand Down
2 changes: 1 addition & 1 deletion man/model_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

test_that("n_parameters", {
expect_identical(n_parameters(m1), 5L)
expect_identical(n_parameters(m1, component = "conditional"), 1)
expect_identical(n_parameters(m1, component = "conditional"), 1L)
})

test_that("clean_names", {
Expand Down Expand Up @@ -80,7 +80,7 @@
expect_identical(find_predictors(m2, flatten = TRUE), c("x2", "x3", "x0", "x1"))
expect_null(find_predictors(m2, effects = "random"))

expect_identical(find_predictors(m3), list(y0 = list(conditional = c("x0", "x1")), y1 = list(conditional = c("x2", "x3"))))

Check warning on line 83 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=83,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
expect_identical(find_predictors(m3, flatten = TRUE), c("x0", "x1", "x2", "x3"))
expect_null(find_predictors(m3, effects = "random"))
})
Expand Down Expand Up @@ -159,7 +159,7 @@
expect_identical(find_variables(m1, flatten = TRUE), c("y", "x0", "x1", "x2", "x3"))
expect_identical(find_variables(m2), list(response = "y", conditional = c("x2", "x3"), zero_inflated = c("x0", "x1")))
expect_identical(find_variables(m2, flatten = TRUE), c("y", "x2", "x3", "x0", "x1"))
expect_identical(find_variables(m3), list(response = c(y0 = "y0", y1 = "y1"), y0 = list(conditional = c("x0", "x1")), y1 = list(conditional = c("x2", "x3"))))

Check warning on line 162 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=162,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 160 characters.
expect_identical(find_variables(m3, flatten = TRUE), c("y0", "y1", "x0", "x1", "x2", "x3"))
})

Expand Down Expand Up @@ -278,12 +278,12 @@
withr::with_environment(
new.env(),
test_that("get_predicted, gam-1", {
# dat3 <- head(dat, 30)

Check warning on line 281 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=281,col=7,[commented_code_linter] Remove commented code.
# tmp <- mgcv::gam(y ~ s(x0) + s(x1), data = dat3)

Check warning on line 282 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=282,col=7,[commented_code_linter] Remove commented code.
# pred <- get_predicted(tmp, verbose = FALSE, ci = 0.95)

Check warning on line 283 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=283,col=7,[commented_code_linter] Remove commented code.
# expect_s3_class(pred, "get_predicted")

Check warning on line 284 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=284,col=7,[commented_code_linter] Remove commented code.
# expect_equal(
# as.vector(pred),

Check warning on line 286 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=286,col=9,[commented_code_linter] Remove commented code.
# c(
# 11.99341, 5.58098, 10.89252, 7.10335, 5.94836, 6.5724, 8.5054,
# 5.47147, 5.9343, 8.27001, 5.71199, 9.94999, 5.69979, 6.63532,
Expand All @@ -291,10 +291,10 @@
# 7.80726, 7.38088, 5.70664, 10.60654, 7.62847, 5.8596, 6.06744,
# 5.81571, 10.4606
# ),
# tolerance = 1e-3

Check warning on line 294 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=294,col=9,[commented_code_linter] Remove commented code.
# )

# x <- get_predicted(tmp, predict = NULL, type = "link", ci = 0.95)

Check warning on line 297 in tests/testthat/test-gam.R

View workflow job for this annotation

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

file=tests/testthat/test-gam.R,line=297,col=7,[commented_code_linter] Remove commented code.
# y <- get_predicted(tmp, predict = "link", ci = 0.95)
# z <- predict(tmp, type = "link", se.fit = TRUE)
# expect_equal(x, y)
Expand Down
25 changes: 22 additions & 3 deletions tests/testthat/test-glmmTMB.R
Original file line number Diff line number Diff line change
Expand Up @@ -968,9 +968,6 @@ test_that("model_info, ordered beta", {
out <- model_info(m)
expect_true(out$is_orderedbeta)
expect_identical(out$family, "ordbeta")
skip_on_cran()
out <- get_variance(m)
expect_equal(out$var.distribution, 1.44250604187634, tolerance = 1e-4)
})


Expand All @@ -987,3 +984,25 @@ test_that("model_info, recognize ZI even without ziformula", {
expect_true(out$is_zero_inflated)
expect_true(out$is_hurdle)
})


skip_if_not_installed("withr")

withr::with_environment(
new.env(),
test_that("get_variance, ordered beta", {
skip_if_not_installed("glmmTMB", minimum_version = "1.1.8")
skip_if_not_installed("datawizard")
skip_if_not_installed("lme4")
skip_on_cran()
data(sleepstudy, package = "lme4")
sleepstudy$y <- datawizard::normalize(sleepstudy$Reaction)
m <- glmmTMB::glmmTMB(
y ~ Days + (Days | Subject),
data = sleepstudy,
family = glmmTMB::ordbeta()
)
out <- get_variance(m)
expect_equal(out$var.distribution, 1.44250604187634, tolerance = 1e-4)
})
)
7 changes: 4 additions & 3 deletions tests/testthat/test-vgam.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ test_that("find_response", {
})

test_that("get_response", {
expect_equal(get_response(m1), hunua$agaaus)
expect_identical(get_response(m1), hunua$agaaus)
expect_equal(
get_response(m2),
data.frame(agaaus = hunua$agaaus, kniexc = hunua$kniexc)
data.frame(agaaus = hunua$agaaus, kniexc = hunua$kniexc),
ignore_attr = TRUE
)
})

Expand Down Expand Up @@ -195,7 +196,7 @@ test_that("find_parameters", {

test_that("is_multivariate", {
expect_false(is_multivariate(m1))
expect_false(is_multivariate(m2))
expect_true(is_multivariate(m2))
})

test_that("find_statistic", {
Expand Down
Loading