Skip to content

Commit

Permalink
Error in exp(spurious_coefficients) : non-numeric argument to mathema…
Browse files Browse the repository at this point in the history
…tical function

Fixes #999
  • Loading branch information
strengejacke committed Jul 24, 2024
1 parent e949952 commit 91f9f75
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
14 changes: 9 additions & 5 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
27 changes: 14 additions & 13 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 91f9f75

Please sign in to comment.