diff --git a/R/format.R b/R/format.R index 86b8868cf..e5a5f6978 100644 --- a/R/format.R +++ b/R/format.R @@ -312,7 +312,7 @@ format.compare_parameters <- function(x, # since we merged all models together, and we only have model-specific # columns for estimates, CI etc. but not for Effects and Component, we # extract "valid" rows via non-NA values in the coefficient column - coef_column <- which(colnames(cols) %in% c(.all_coefficient_types(), "Coefficient")) + coef_column <- which(colnames(cols) %in% c(.all_coefficient_types, "Coefficient")) valid_rows <- which(!is.na(cols[[coef_column]])) # check if we have mixed models with random variance parameters # in such cases, we don't need the group-column, but we rather @@ -919,11 +919,13 @@ format.parameters_sem <- function(x, .print_footer_exp <- function(x) { + # we need this to check whether we have extremely large cofficients if (isTRUE(getOption("parameters_exponentiate", TRUE))) { msg <- NULL - # we need this to check whether we have extremely large cofficients - if (all(c("Coefficient", "Parameter") %in% colnames(x))) { - spurious_coefficients <- abs(x$Coefficient[!.in_intercepts(x$Parameter)]) + # try to find out the name of the coefficient column + coef_column <- intersect(colnames(x), .all_coefficient_names) + if (length(coef_column) && "Parameter" %in% colnames(x)) { + spurious_coefficients <- abs(x[[coef_column[1]]][!.in_intercepts(x$Parameter)]) } else { spurious_coefficients <- NULL } @@ -932,7 +934,9 @@ format.parameters_sem <- function(x, if (isTRUE(.additional_arguments(x, "log_link", FALSE))) { msg <- "The model has a log- or logit-link. Consider using `exponentiate = TRUE` to interpret coefficients as ratios." # nolint # we only check for exp(coef), so exp() here soince coefficients are on logit-scale - spurious_coefficients <- exp(spurious_coefficients) + if (!is.null(spurious_coefficients)) { + spurious_coefficients <- exp(spurious_coefficients) + } } else if (isTRUE(.additional_arguments(x, "log_response", FALSE))) { msg <- "The model has a log-transformed response variable. Consider using `exponentiate = TRUE` to interpret coefficients as ratios." # nolint # don't show warning about complete separation diff --git a/R/utils_format.R b/R/utils_format.R index 7a4956657..667e53895 100644 --- a/R/utils_format.R +++ b/R/utils_format.R @@ -295,15 +295,15 @@ } # fix coefficient column name for random effects - if (!is.null(x$Effects) && all(x$Effects == "random") && any(colnames(x) %in% .all_coefficient_types())) { - colnames(x)[colnames(x) %in% .all_coefficient_types()] <- "Coefficient" + if (!is.null(x$Effects) && all(x$Effects == "random") && any(colnames(x) %in% .all_coefficient_types)) { + colnames(x)[colnames(x) %in% .all_coefficient_types] <- "Coefficient" } # fix coefficient column name for mixed count and zi pars if (!is.null(x$Component) && sum(c("conditional", "zero_inflated", "dispersion") %in% x$Component) >= 2 && - any(colnames(x) %in% .all_coefficient_types())) { - colnames(x)[colnames(x) %in% .all_coefficient_types()] <- "Coefficient" + any(colnames(x) %in% .all_coefficient_types)) { + colnames(x)[colnames(x) %in% .all_coefficient_types] <- "Coefficient" } # random pars with level? combine into parameter column @@ -468,13 +468,14 @@ # components into different tables, we change the column name for those "tables" # that contain the random effects or zero-inflation parameters -.all_coefficient_types <- function() { - c( - "Odds Ratio", "Risk Ratio", "Prevalence Ratio", "IRR", "Log-Odds", - "Log-Mean", "Log-Ratio", "Log-Prevalence", "Probability", "Marginal Means", - "Estimated Counts", "Ratio" - ) -} +.all_coefficient_types <- c( + "Odds Ratio", "Risk Ratio", "Prevalence Ratio", "IRR", "Log-Odds", + "Log-Mean", "Log-Ratio", "Log-Prevalence", "Probability", "Marginal Means", + "Estimated Counts", "Ratio" +) + + +.all_coefficient_names <- c("Coefficient", "Std_Coefficient", "Estimate", "Median", "Mean", "MAP") .format_stan_parameters <- function(out) { @@ -1087,8 +1088,8 @@ } # rename columns for random part - if (grepl("random", type, fixed = TRUE) && any(colnames(tables[[type]]) %in% .all_coefficient_types())) { - colnames(tables[[type]])[colnames(tables[[type]]) %in% .all_coefficient_types()] <- "Coefficient" + if (grepl("random", type, fixed = TRUE) && any(colnames(tables[[type]]) %in% .all_coefficient_types)) { + colnames(tables[[type]])[colnames(tables[[type]]) %in% .all_coefficient_types] <- "Coefficient" } if (grepl("random", type, fixed = TRUE) && isTRUE(ran_pars)) {