Skip to content

Commit

Permalink
fix format issue
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jan 26, 2024
1 parent 830ca88 commit 0dd407b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 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: parameters
Title: Processing of Model Parameters
Version: 0.21.3.8
Version: 0.21.3.9
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 @@ -24,6 +24,9 @@
* `print(include_reference = TRUE)` for `model_parameters()` did not work when
run inside a pipe-chain.

* Fixed issues with `format()` for objects returned by `compare_parameters()`
that included mixed models.

# parameters 0.21.3

## Changes
Expand Down
8 changes: 4 additions & 4 deletions R/compare_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ compare_parameters <- function(...,
names(models) <- model_names
}
} else {
model_names <- match.call(expand.dots = FALSE)$`...`
model_names <- match.call(expand.dots = FALSE)[["..."]]
if (length(names(model_names)) > 0) {
model_names <- names(model_names)
} else if (any(vapply(model_names, is.call, TRUE))) {
Expand Down Expand Up @@ -275,14 +275,14 @@ compare_models <- compare_parameters
}
} else {
match_pretty_names <- att$pretty_names[x$Parameter]
if (!anyNA(match_pretty_names)) {
x$Parameter <- att$pretty_names[x$Parameter]
} else {
if (anyNA(match_pretty_names)) {
match_pretty_names <- match(names(att$pretty_names), x$Parameter)
match_pretty_names <- match_pretty_names[!is.na(match_pretty_names)]
if (length(match_pretty_names)) {
x$Parameter[match_pretty_names] <- att$pretty_names[x$Parameter[match_pretty_names]]
}
} else {
x$Parameter <- att$pretty_names[x$Parameter]
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ format.compare_parameters <- function(x,
formatted_table <- split(out, f = split_by)
formatted_table <- lapply(names(formatted_table), function(tab) {
i <- formatted_table[[tab]]
# check if data frame is empty - this may happen if not all combinations
# of split_by factors are present in the data (e.g., zero-inflated mixed
# models, that have random effects for the count, but not for the zero-
# inflation component)
if (nrow(i) == 0L) {
return(NULL)
}
# remove unique columns
if (insight::n_unique(i$Component) == 1L) i$Component <- NULL
if (insight::n_unique(i$Effects) == 1L) i$Effects <- NULL
Expand All @@ -447,6 +454,9 @@ format.compare_parameters <- function(x,
i
})

# remove empty tables
formatted_table <- insight::compact_list(formatted_table)

# for HTML, bind data frames
if (identical(format, "html")) {
# fix non-equal length of columns and bind data frames
Expand Down
9 changes: 6 additions & 3 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,12 @@
}


.add_reference_level <- function(params) {
# check if we have a model object, else return parameter table
model <- .get_object(params)
.add_reference_level <- function(params, model = NULL) {
if (is.null(model)) {
# check if we have a model object, if not provided by user
model <- .get_object(params)
}
# no model object provided? Try to get data from model call
if (is.null(model)) {
# get data from model call
model_data <- .safe(eval(attributes(params)$model_call$data))
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-format_model_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,32 @@ withr::with_options(
})
}
)

skip_if_not_installed("lme4")
skip_if_not_installed("glmmTMB")

test_that("format, compare_parameters, mixed models", {
data(mtcars)
data(Salamanders, package = "glmmTMB")
model1 <- lme4::lmer(mpg ~ wt + (1 | gear), data = mtcars)
model2 <- glmmTMB::glmmTMB(
count ~ spp + mined + (1 | site),
ziformula = ~mined,
family = poisson(),
data = Salamanders
)
out <- compare_parameters(model1, model2, effects = "all", component = "all")
f <- format(out)
expect_length(f, 3)
f <- format(out, format = "html")
expect_identical(
f$Component,
c(
"Fixed Effects", "Fixed Effects", "Fixed Effects", "Fixed Effects",
"Fixed Effects", "Fixed Effects", "Fixed Effects", "Fixed Effects",
"Fixed Effects", "Fixed Effects (Zero-Inflation Component)",
"Fixed Effects (Zero-Inflation Component)", "Random Effects",
"Random Effects", "Random Effects"
)
)
})

0 comments on commit 0dd407b

Please sign in to comment.