Skip to content
Open
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

## Bug fixes

* `model_parameters()` uses variable labels for coefficients in *multcomp*
hypothesis labels (#1139).
Comment on lines +38 to +39

* `model_parameters()` for *BayesFactor* objects with more than one model, for
example from `regressionBF()` or `anovaBF()`, now shows the Bayes factor of
the first model in every row. The parameters also come from that model. Before
Expand Down
54 changes: 54 additions & 0 deletions R/format_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@
if (is.null(model)) {
model <- .get_object(params)
}
if (inherits(model, "glht")) {
return(.format_glht_labels(params, model))
}

# validation check
if (!is.null(model) && insight::is_regression_model(model) && !is.data.frame(model)) {
Expand Down Expand Up @@ -606,6 +609,57 @@
}


.format_glht_labels <- function(params, model) {
out <- stats::setNames(params$Parameter, params$Parameter)
terms <- names(model$coef)

Check warning on line 614 in R/format_parameters.R

View workflow job for this annotation

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

file=R/format_parameters.R,line=614,col=3,[object_overwrite_linter] 'terms' is an exported object from package 'stats'. Avoid re-using such symbols.
if (!length(terms)) {
return(out)
}
term_table <- .data_frame(Parameter = terms)
attr(term_table, "pretty_names") <- stats::setNames(terms, terms)
labels <- .format_value_labels(term_table, model$model)

Check warning on line 620 in R/format_parameters.R

View workflow job for this annotation

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

file=R/format_parameters.R,line=620,col=3,[object_overwrite_linter] 'labels' is an exported object from package 'base'. Avoid re-using such symbols.

keys <- paste(rownames(model$linfct), model$rhs)
keys[duplicated(keys) | duplicated(keys, fromLast = TRUE)] <- NA_character_
rows <- match(sub(" (==|<=|>=) ([^ ]+)$", " \\2", out), keys)
out[] <- vapply(
seq_along(out),
function(i) {
original <- out[[i]]
if (is.na(rows[i])) {
return(original)
}
used <- terms[which(model$linfct[rows[i], ] != 0)]
if (!length(used) || identical(unname(labels[used]), used)) {
return(original)
}

# Match whole coefficient names, with longer expressions before their parts.
used <- used[order(nchar(used), decreasing = TRUE)]
escaped <- gsub("([][{}()+*^$|\\\\?.])", "\\\\\\1", used)
pattern <- paste0(
"(?<![[:alnum:]_.])(?:",
paste(escaped, collapse = "|"),
")(?![[:alnum:]_.])"
)
locations <- gregexpr(pattern, original, perl = TRUE)
matches <- regmatches(original, locations)[[1]]

# Matrix contrasts can carry arbitrary names. Relabel complete expressions.
masked <- original
regmatches(masked, locations) <- list(rep("0", length(matches)))
if (!setequal(matches, used) || !grepl("^[[:space:]0-9.eE+*/():<>=-]+$", masked)) {
return(original)
}
regmatches(original, locations) <- list(unname(labels[matches]))
original
},
character(1)
)
out
}


# helper -------------------

.unicode_symbols <- function() {
Expand Down
54 changes: 54 additions & 0 deletions tests/testthat/test-model_parameters.glht.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,62 @@
params <- model_parameters(model)
expect_equal(params$Coefficient, c(-0.1721, -0.258, -0.8709, 0.1041, 1.077), tolerance = 1e-2)
expect_equal(params$SE, c(0.0703, 0.2539, 0.183, 0.0353, 0.3817), tolerance = 1e-2)
expect_equal(

Check warning on line 18 in tests/testthat/test-model_parameters.glht.R

View workflow job for this annotation

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

file=tests/testthat/test-model_parameters.glht.R,line=18,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=.
params$Parameter,
c("Agriculture == 0", "Examination == 0", "Education == 0", "Catholic == 0", "Infant.Mortality == 0")
)
})

test_that("glht labels use variable labels for coefficient expressions", {
skip_if_not_installed("multcomp")
skip_if_not_installed("glmmTMB")

data <- transform(mtcars, cylinder_power = cyl^2)
attr(data$cyl, "label") <- "Cylinder gear"
attr(data$gear, "label") <- "Forward gears"
attr(data$cylinder_power, "label") <- "Cylinder square"
fit <- glmmTMB::glmmTMB(mpg ~ cyl * gear + cylinder_power, data = data)
hypothesis <- multcomp::glht(
fit,
linfct = c("cyl + gear = 0", "cyl:gear + cylinder_power = 0")
)
params <- model_parameters(hypothesis, pretty_names = "labels")

expect_identical(
params$Parameter,
c("cyl + gear == 0", "cyl:gear + cylinder_power == 0")
)
expect_identical(
attr(params, "pretty_labels"),
stats::setNames(
c(
"Cylinder gear + Forward gears == 0",
"Cylinder gear * Forward gears + Cylinder square == 0"
),
params$Parameter
)
)
expect_null(attr(model_parameters(hypothesis, pretty_names = FALSE), "pretty_labels"))
})

test_that("glht retains descriptive matrix contrast names", {
skip_if_not_installed("multcomp")

data <- mtcars
attr(data$cyl, "label") <- "Cylinders"
attr(data$gear, "label") <- "Gears"
fit <- lm(mpg ~ cyl + gear, data = data)
contrast <- matrix(
rep(c(0, 1, 0), 3),
nrow = 3,
byrow = TRUE,
dimnames = list(
c("cyl versus gear", "gear", "cyl - gear"),
c("(Intercept)", "gear", "cyl")
)
)
hypothesis <- multcomp::glht(fit, linfct = contrast)
params <- model_parameters(hypothesis, pretty_names = "labels")

expect_identical(unname(attr(params, "pretty_labels")), params$Parameter)
})
Loading