Skip to content

Commit

Permalink
Fix_gamlss (#828)
Browse files Browse the repository at this point in the history
* fix issue with find_formula.gamlss with namespace

* news, version

* lintr
  • Loading branch information
strengejacke authored Nov 2, 2023
1 parent 7822d45 commit 8e81cc4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
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: 0.19.6.4
Version: 0.19.6.5
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* Fixed issue in `model_info()`, where in some cases logistic regression models
were erroneously considered as `"bernoulli"` models.

* Fixed issue in `find_formula()` for models of class `gamlss` when the `random()`
function was used with namespace in the formula (i.e. `... + gamlss::random()`).

# insight 0.19.6

## General
Expand Down
20 changes: 16 additions & 4 deletions R/find_formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,21 @@ find_formula.gamlss <- function(x, verbose = TRUE, ...) {
if (length(f.random) == 1L) {
f.random <- f.random[[1]]
} else if (grepl("random\\((.*)\\)", safe_deparse(f.cond))) {
re <- gsub("(.*)random\\((.*)\\)", "\\2", safe_deparse(f.cond))
f.cond <- safe_deparse(f.cond)
# remove namespace prefixes
if (grepl("::", f.cond, fixed = TRUE)) {
# Here's a regular expression pattern in R that removes any word
# followed by two colons from a string: This pattern matches a word
# boundary (\\b), followed by one or more word characters (\\w+),
# followed by two colons (::)
f.cond <- gsub("\\b\\w+::", "\\2", f.cond)
}
re <- gsub("(.*)random\\((.*)\\)", "\\2", f.cond)
f.random <- stats::as.formula(paste0("~1|", re))
f.cond <- stats::update.formula(f.cond, stats::as.formula(paste0(". ~ . - random(", re, ")")))
f.cond <- stats::update.formula(
stats::as.formula(f.cond),
stats::as.formula(paste0(". ~ . - random(", re, ")"))
)
}

compact_list(list(
Expand Down Expand Up @@ -1787,7 +1799,7 @@ find_formula.model_fit <- function(x, verbose = TRUE, ...) {
if (grepl("(.*)poly\\((.*),\\s*raw\\s*=\\s*T\\)", f)) {
if (verbose) {
format_warning(
"Looks like you are using `poly()` with \"raw = T\". This results in unexpected behaviour, because `all.vars()` considers `T` as variable.",
"Looks like you are using `poly()` with \"raw = T\". This results in unexpected behaviour, because `all.vars()` considers `T` as variable.", # nolint
"Please use \"raw = TRUE\"."
)
}
Expand All @@ -1813,7 +1825,7 @@ find_formula.model_fit <- function(x, verbose = TRUE, ...) {
} else {
if (verbose) {
format_warning(paste0(
"Using `$` in model formulas can produce unexpected results. Specify your model using the `data` argument instead.",
"Using `$` in model formulas can produce unexpected results. Specify your model using the `data` argument instead.", # nolint
"\n Try: ", fc$formula, ", data = ", fc$data
))
}
Expand Down
26 changes: 22 additions & 4 deletions tests/testthat/test-gamlss.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ test_that("find_response", {
})

test_that("get_response", {
expect_equal(get_response(m_gamlss1), abdom$y)
expect_identical(get_response(m_gamlss1), abdom$y)
})

test_that("get_predictors", {
expect_identical(colnames(get_predictors(m_gamlss1)), "x")
})

test_that("get_data", {
expect_equal(nrow(get_data(m_gamlss1)), 610)
expect_identical(nrow(get_data(m_gamlss1)), 610L)
expect_identical(colnames(get_data(m_gamlss1)), c("y", "x"))
})

Expand Down Expand Up @@ -101,7 +101,7 @@ test_that("find_terms", {
})

test_that("n_obs", {
expect_equal(n_obs(m_gamlss1), 610)
expect_identical(n_obs(m_gamlss1), 610L)
})

test_that("link_function", {
Expand All @@ -122,7 +122,7 @@ test_that("find_parameters", {
tau = "(Intercept)"
)
)
expect_equal(nrow(get_parameters(m_gamlss1)), 6)
expect_identical(nrow(get_parameters(m_gamlss1)), 6L)
})

test_that("is_multivariate", {
Expand All @@ -136,3 +136,21 @@ test_that("find_algorithm", {
test_that("find_statistic", {
expect_identical(find_statistic(m_gamlss1), "t-statistic")
})

test_that("find_formula works with namespace colons", {
data(iris)
m <- gamlss::gamlss(
Sepal.Length ~ Sepal.Width + gamlss::random(Species),
sigma.formula = ~Sepal.Width,
data = iris
)
expect_equal(
find_formula(m),
list(
conditional = Sepal.Length ~ Sepal.Width,
random = ~ 1 | Species,
sigma = ~Sepal.Width
),
ignore_attr = TRUE
)
})

0 comments on commit 8e81cc4

Please sign in to comment.