Skip to content

Commit 42e0e9c

Browse files
authored
pscl crashes with string variable (#1133)
* pscl crashes with string variable Fixes #1132 * move into test()
1 parent 96eb264 commit 42e0e9c

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.26.0.15
4+
Version: 0.26.0.16
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

R/format_parameters.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,21 +425,20 @@ format_parameters.parameters_model <- function(model, ...) {
425425
unlist(preds, use.names = FALSE)
426426
))
427427

428-
# for models from pscl, we have "count_" and "zero_" prefixes, which
429-
# we need to add to the "pretty_labels" names, so that we can match
430-
# them with the parameters
431-
if (inherits(model, c("zeroinfl", "hurdle"))) {
432-
pretty_labels <- c(
433-
stats::setNames(pretty_labels, paste0("count_", names(pretty_labels))),
434-
stats::setNames(pretty_labels, paste0("zero_", names(pretty_labels)))
435-
)
436-
}
437-
438428
# retrieve pretty names attribute
439429
pn <- attributes(params)$pretty_names
440430
# replace former pretty names with labels, if we have any labels
441431
# (else, default pretty names are returned)
442432
if (!is.null(pretty_labels)) {
433+
# for models from pscl, we have "count_" and "zero_" prefixes, which
434+
# we need to add to the "pretty_labels" names, so that we can match
435+
# them with the parameters
436+
if (inherits(model, c("zeroinfl", "hurdle"))) {
437+
pretty_labels <- c(
438+
stats::setNames(pretty_labels, paste0("count_", names(pretty_labels))),
439+
stats::setNames(pretty_labels, paste0("zero_", names(pretty_labels)))
440+
)
441+
}
443442
# check if we have any interactions, and if so, create combined labels
444443
interactions <- pn[grepl(":", names(pn), fixed = TRUE)]
445444
if (length(interactions)) {

tests/testthat/test-pretty_names.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ withr::with_options(
8080

8181
model <- pscl::zeroinfl(art ~ fem + mar, data = mydf)
8282
expect_snapshot(print(model_parameters(model), pretty_names = "labels", zap_small = TRUE))
83+
84+
# doesn't crash if pretty-labels is NULL
85+
mydf <- pscl::bioChemists
86+
mydf$mar <- as.character(mydf$mar)
87+
model <- pscl::zeroinfl(art ~ fem + mar, data = mydf)
88+
out <- model_parameters(model)
89+
expect_identical(dim(out), c(6L, 10L))
8390
})
8491
}
8592
)

0 commit comments

Comments
 (0)