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
22 changes: 21 additions & 1 deletion R/format_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,19 @@ format_parameters.parameters_model <- function(model, ...) {
}
resp <- insight::find_response(model, combine = FALSE)
mf <- mf[, setdiff(colnames(mf), resp), drop = FALSE]
# we need data a 2nd time, to identify on-the-fly-factors
mf_data <- insight::get_data(model, source = "mf", verbose = FALSE)
factors <- attributes(mf_data)$factors

# return variable labels, and for factors, add labels for each level
lbs <- lapply(colnames(mf), function(i) {
vec <- mf[[i]]
# if variable is not a factor, we check whether variable
# was converted to factor on-the-fly.
if (!is.factor(vec) && !is.null(factors) && i %in% factors) {
# if so, we need to convert to factor, to have "factor levels"
vec <- as.factor(vec)
}
if (is.factor(vec)) {
variable_label <- attr(vec, "label", exact = TRUE)
value_labels <- names(attr(vec, "labels", exact = TRUE))
Expand Down Expand Up @@ -507,10 +516,21 @@ format_parameters.parameters_model <- function(model, ...) {
for (i in names(interactions)) {
# extract single coefficient names from interaction term
out <- unlist(strsplit(i, ":", fixed = TRUE))
# for on-the-fly conversion of factors, the names of the factors can
# can also contain "factor()" or "as.factor()" - we need to remove these
if (any(grepl("(as\\.factor|factor|as\\.character)", out))) {
out_clean <- gsub(
"(as\\.factor|factor|as\\.character)\\((.*)\\)(.*)",
"\\2",
out
)
} else {
out_clean <- out
}
# combine labels
labs <- c(
labs,
paste(sapply(out, function(l) pretty_labels[l]), collapse = " * ")
paste(sapply(out_clean, function(l) pretty_labels[l]), collapse = " * ")
)
}
# add interaction terms to labels string
Expand Down
6 changes: 5 additions & 1 deletion R/utils_model_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @keywords internal
#' @noRd
.add_model_parameters_attributes <- function(params,

Check warning on line 7 in R/utils_model_parameters.R

View workflow job for this annotation

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

file=R/utils_model_parameters.R,line=7,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 43 to at most 40. Consider replacing high-complexity sections like loops and branches with helper functions.
model,
ci,
exponentiate = FALSE,
Expand Down Expand Up @@ -215,7 +215,11 @@
}

# add parameters with value and variable
attr(params, "pretty_labels") <- .format_value_labels(params, model)
if (isFALSE(dot.arguments$pretty_names)) {
attr(params, "pretty_labels") <- params$Parameter
} else if (is.null(attr(params, "pretty_labels", exact = TRUE))) {
attr(params, "pretty_labels") <- .format_value_labels(params, model)
}

row.names(params) <- NULL
params
Expand Down Expand Up @@ -254,7 +258,7 @@
#'
#' @keywords internal
#' @noRd
.find_coefficient_type <- function(info, exponentiate, model = NULL) {

Check warning on line 261 in R/utils_model_parameters.R

View workflow job for this annotation

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

file=R/utils_model_parameters.R,line=261,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 57 to at most 40. Consider replacing high-complexity sections like loops and branches with helper functions.
# column name for coefficients
coef_col <- "Coefficient"
if (!is.null(model) && inherits(model, "emmGrid")) {
Expand Down
Loading