Skip to content

Commit

Permalink
make parameters() show fixed effects restricted to 0
Browse files Browse the repository at this point in the history
Fixes #715
  • Loading branch information
strengejacke committed Sep 11, 2023
1 parent 99a2993 commit 01240fd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
34 changes: 34 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,37 @@
.is_semLme <- function(x) {
all(inherits(x, c("sem", "lme")))
}


.insert_row_at <- function(data, row, index, default_value = NA) {
# add missing columns
new_columns <- setdiff(colnames(data), colnames(row))
if (length(new_columns) > 0) {
row[new_columns] <- default_value
}
# match column order
row <- row[match(colnames(data), colnames(row))]

# insert row
if (index == 1) {
rbind(row, data)
} else if (index == nrow(data)) {
rbind(data, row)
} else {
rbind(data[1:(index - 1), ], row, data[index:nrow(data), ])
}
}


.find_factor_levels <- function(data) {
out <- lapply(colnames(data), function(i) {
v <- data[[i]]
if (is.factor(v)) {
paste0(i, levels(v))
} else {
NULL
}
})
names(out) <- names(data)
insight::compact_list(out)
}
29 changes: 29 additions & 0 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
format = NULL,
coef_name = NULL,
zap_small = FALSE,
add_reference = FALSE,
...) {
# default brackets are parenthesis for HTML / MD
if ((is.null(ci_brackets) || isTRUE(ci_brackets)) && (identical(format, "html") || identical(format, "markdown"))) {
Expand Down Expand Up @@ -308,6 +309,11 @@
x$Level <- NULL
}

# add the coefficient for the base-(reference)-level of factors?
if (add_reference) {
x <- .add_reference_level(x)
}

insight::format_table(
x,
pretty_names = pretty_names,
Expand All @@ -322,6 +328,29 @@
}


.add_reference_level <- function(params) {
# check if we have a model object, else return parameter table
model <- .get_object(params)
if (is.null(model)) {
params
}

# check if we have model data, else return parameter table
model_data <- insight::get_data(model)
if (is.null(model_data)) {
params
}

# find factors and factor levels and check if we have any factors in the data
factors <- .find_factor_levels(model_data)
if (!length(factors)) {
params
}

params
}


# The coefficient column in the printed output is renamed, based on the model.
# But for instance, for random effects, however, which are on a different scale,
# we want a different name for this column. Since print.parameters_model() splits
Expand Down

0 comments on commit 01240fd

Please sign in to comment.