Skip to content
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 1.4.2.13
Version: 1.4.3
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# insight (devel)
# insight 1.4.3

## Changes

Expand Down
31 changes: 22 additions & 9 deletions R/is_converged.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#' @description `is_converged()` provides an alternative convergence
#' test for `merMod`-objects.
#'
#' @param x A model object from class `merMod`, `glmmTMB`, `glm` or `_glm`.
#' @param x A model object from class `merMod`, `glmmTMB`, `glm`, `lavaan` or
#' `_glm`.
#' @param tolerance Indicates up to which value the convergence result is
#' accepted. The smaller `tolerance` is, the stricter the test will be.
#' @param ... Currently not used.
Expand All @@ -26,23 +27,34 @@
#' convergence test for `merMod`-objects.
#'
#' @section Resolving convergence issues:
#' Convergence issues are not easy to diagnose. The help page on `?lme4::convergence`
#' provides most of the current advice about how to resolve convergence issues.
#' Another clue might be large parameter values, e.g. estimates (on the scale of
#' the linear predictor) larger than 10 in (non-identity link) generalized linear
#' model *might* indicate complete separation, which can be addressed by
#' regularization, e.g. penalized regression or Bayesian regression with
#' appropriate priors on the fixed effects.
#' Convergence issues are not easy to diagnose. The help page on
#' `?lme4::convergence` provides most of the current advice about how to resolve
#' convergence issues. In general, convergence issues may be addressed by one or
#' more of the following strategies: 1. Rescale continuous predictors; 2. try a
#' different optimizer; 3. increase the number of iterations; or, if everything
#' else fails, 4. simplify the model. Another clue might be large parameter
#' values, e.g. estimates (on the scale of the linear predictor) larger than 10
#' in (non-identity link) generalized linear model *might* indicate complete
#' separation, which can be addressed by regularization, e.g. penalized
#' regression or Bayesian regression with appropriate priors on the fixed
#' effects.
#'
#' @section Convergence versus Singularity:
#' Note the different meaning between singularity and convergence: singularity
#' indicates an issue with the "true" best estimate, i.e. whether the maximum
#' likelihood estimation for the variance-covariance matrix of the random effects
#' is positive definite or only semi-definite. Convergence is a question of
#' whether we can assume that the numerical optimization has worked correctly
#' or not.
#' or not. A convergence failure means the optimizer (the algorithm) could not
#' find a stable solution (_Bates et. al 2015_).
#'
#' @references
#' Bates, D., Mächler, M., Bolker, B., and Walker, S. (2015). Fitting Linear
#' Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1),
#' 1-48. \doi{10.18637/jss.v067.i01}
#'
#' @examplesIf require("lme4", quietly = TRUE)
#' library(lme4)
#' data(cbpp)
#' set.seed(1)
#' cbpp$x <- rnorm(nrow(cbpp))
Expand All @@ -58,6 +70,7 @@
#'
#' @examplesIf getOption("warn") < 2L && require("glmmTMB")
#' \donttest{
#' library(glmmTMB)
#' model <- glmmTMB(
#' Sepal.Length ~ poly(Petal.Width, 4) * poly(Petal.Length, 4) +
#' (1 + poly(Petal.Width, 4) | Species),
Expand Down Expand Up @@ -108,7 +121,7 @@
is_converged.glm <- function(x, tolerance = 0.001, ...) {
if (!is.null(x$converged)) {
isTRUE(x$converged)
} else if (!is.null(x$fit$converged)) {

Check warning on line 124 in R/is_converged.R

View workflow job for this annotation

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

file=R/is_converged.R,line=124,col=14,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
isTRUE(x$fit$converged)
} else {
NULL
Expand Down
31 changes: 22 additions & 9 deletions man/is_converged.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-averaging.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
skip_on_cran()
skip_if_not_installed("MuMIn")
skip_if_not_installed("withr")
skip_on_cran()

withr::with_options(
list(na.action = "na.fail"),
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-coxme.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
skip_on_cran()
skip_if_not_installed("survival")
skip_if_not_installed("lme4")
skip_if_not_installed("nlme")
skip_if_not_installed("bdsmatrix")
skip_if_not_installed("coxme")
skip_if_not_installed("withr")
skip_on_cran()

withr::with_environment(
new.env(),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-get_priors.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
skip_on_cran()

test_that("get_priors", {
skip_on_os(os = c("mac", "windows"))
skip_on_cran()
skip_if_not_installed("brms")

set.seed(123)
Expand All @@ -9,13 +10,12 @@
priors <- insight::get_priors(model)

expect_equal(priors$Location, c(19.2, NA, 0), tolerance = 1e-3)
expect_equal(priors$Distribution, c("student_t", "uniform", "student_t"))

Check warning on line 13 in tests/testthat/test-get_priors.R

View workflow job for this annotation

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

file=tests/testthat/test-get_priors.R,line=13,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
expect_equal(priors$Parameter, c("b_Intercept", "b_wt", "sigma"))

Check warning on line 14 in tests/testthat/test-get_priors.R

View workflow job for this annotation

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

file=tests/testthat/test-get_priors.R,line=14,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
})

test_that("get_priors, stanmvref", {
skip_on_os(os = "mac")
skip_on_cran()
skip_if_not_installed("curl")
skip_if_offline()
skip_if_not_installed("httr2")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-mlogit.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
skip_if_not_installed("mlogit")
skip_on_cran()
skip_if_not_installed("mlogit")

data("Fishing", package = "mlogit")
Fish <-
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-phylolm.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
skip_on_cran()
skip_if_not_installed("phylolm")
skip_if_not_installed("ape")
skip_if_not_installed("MuMIn")
skip_if_not_installed("withr")
skip_on_cran()

withr::with_options(
list(na.action = "na.fail"),
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-rms.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
skip_on_cran()
skip_if_not_installed("rms")

data(mtcars)
Expand Down
188 changes: 62 additions & 126 deletions tests/testthat/test-tidymodels.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
skip_on_cran()
skip_if_not_installed("parsnip")

data(mtcars)
m <- parsnip::linear_reg()
m <- parsnip::set_engine(m, "lm")
m <- parsnip::set_mode(m, "regression")
Expand All @@ -13,129 +15,63 @@
)
})

# test_that("model_info", {
# expect_true(model_info(m1)$is_poisson)
# expect_true(model_info(m1)$is_count)
# expect_false(model_info(m1)$is_negbin)
# expect_false(model_info(m1)$is_binomial)
# expect_false(model_info(m1)$is_linear)
# })
#
# test_that("loglik", {
# expect_equal(get_loglikelihood(m1), logLik(m1), ignore_attr = TRUE)
# })
#
# test_that("get_df", {
# expect_equal(get_df(m1), df.residual(m1), ignore_attr = TRUE)
# expect_equal(get_df(m1, type = "model"), attr(logLik(m1), "df"), ignore_attr = TRUE)
# })
#
#
# test_that("find_predictors", {
# expect_identical(find_predictors(m1), list(conditional = c("mined", "cover", "sample")))
# expect_identical(
# find_predictors(m1, flatten = TRUE),
# c("mined", "cover", "sample")
# )
# expect_null(find_predictors(m1, effects = "random"))
# })
#
# test_that("find_random", {
# expect_null(find_random(m1))
# })
#
# test_that("get_random", {
# expect_warning(get_random(m1))
# })
#
# test_that("find_response", {
# expect_identical(find_response(m1), "count")
# })
#
# test_that("get_response", {
# expect_equal(get_response(m1), Salamanders$count)
# })
#
# test_that("get_predictors", {
# expect_equal(colnames(get_predictors(m1)), c("mined", "cover", "sample"))
# })
#
# test_that("link_inverse", {
# expect_equal(link_inverse(m1)(0.2), exp(0.2), tolerance = 1e-5)
# })
#
# test_that("linkfun", {
# expect_equal(link_function(m1)(0.2), -1.609438, tolerance = 1e-4)
# })
#
# test_that("get_data", {
# expect_equal(nrow(get_data(m1)), 644)
# expect_equal(
# colnames(get_data(m1)),
# c("count", "mined", "cover", "sample")
# )
# })
#
# test_that("get_call", {
# expect_equal(class(get_call(m1)), "call")
# })
#
#
#
# test_that("find_variables", {
# expect_equal(
# find_variables(m1),
# list(
# response = "count",
# conditional = c("mined", "cover", "sample")
# )
# )
# expect_equal(
# find_variables(m1, flatten = TRUE),
# c("count", "mined", "cover", "sample")
# )
# })
#
# test_that("n_obs", {
# expect_equal(n_obs(m1), 644)
# })
#
# test_that("find_parameters", {
# expect_equal(
# find_parameters(m1),
# list(
# conditional = c("(Intercept)", "minedno", "log(cover)", "sample")
# )
# )
# expect_equal(nrow(get_parameters(m1)), 4)
# expect_equal(
# get_parameters(m1)$Parameter,
# c("(Intercept)", "minedno", "log(cover)", "sample")
# )
# })
#
# test_that("is_multivariate", {
# expect_false(is_multivariate(m1))
# })
#
# test_that("find_terms", {
# expect_equal(
# find_terms(m1),
# list(
# response = "count",
# conditional = c("mined", "log(cover)", "sample")
# )
# )
# })
#
# test_that("find_algorithm", {
# expect_equal(find_algorithm(m1), list(algorithm = "ML"))
# })
#
# test_that("find_statistic", {
# expect_identical(find_statistic(m1), "z-statistic")
# })
#
# test_that("get_statistic", {
# expect_equal(get_statistic(m1)$Statistic, c(-10.7066515607315, 18.1533878215937, -1.68918157150882, 2.23541768590273), tolerance = 1e-4)
# })
test_that("model_info", {
expect_true(model_info(m)$is_linear)
})

test_that("loglik", {
expect_equal(
get_loglikelihood(m),
-83.8397585518224,
tolerance = 1e-4,
ignore_attr = TRUE
)
})

test_that("get_df", {
expect_equal(get_df(m), 29, ignore_attr = TRUE)
expect_equal(get_df(m, type = "model"), 4, ignore_attr = TRUE)
})


test_that("find_predictors", {
expect_identical(find_predictors(m), list(conditional = c("am", "vs")))
expect_identical(
find_predictors(m, flatten = TRUE),
c("am", "vs")
)
expect_null(find_predictors(m, effects = "random"))
})

test_that("find_random", {
expect_null(find_random(m))
})

test_that("get_random", {
expect_warning(get_random(m))
})

test_that("find_response", {
expect_identical(find_response(m), "mpg")
})

test_that("get_response", {
expect_equal(get_response(m), mtcars$mpg)

Check warning on line 59 in tests/testthat/test-tidymodels.R

View workflow job for this annotation

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

file=tests/testthat/test-tidymodels.R,line=59,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
})

test_that("get_predictors", {
expect_equal(colnames(get_predictors(m)), c("am", "vs"))

Check warning on line 63 in tests/testthat/test-tidymodels.R

View workflow job for this annotation

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

file=tests/testthat/test-tidymodels.R,line=63,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
})

test_that("link_inverse", {
expect_equal(link_inverse(m)(0.2), 0.2, tolerance = 1e-5)
})

test_that("linkfun", {
expect_equal(link_function(m)(0.2), -1.609438, tolerance = 1e-4)
})

test_that("get_data", {
expect_equal(nrow(get_data(m)), 32)

Check warning on line 75 in tests/testthat/test-tidymodels.R

View workflow job for this annotation

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

file=tests/testthat/test-tidymodels.R,line=75,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
expect_named(get_data(m), c("mpg", "am", "vs"))
})
Loading