Skip to content

Commit

Permalink
format_alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 28, 2023
1 parent eba59ec commit 7ed4e4d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion R/bayesfactor_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ bayesfactor_pointnull <- function(posterior,
verbose = TRUE,
...) {
if (length(null) > 1 && verbose) {
message("'null' is a range - computing a ROPE based Bayes factor.")
insight::format_alert("`null` is a range - computing a ROPE based Bayes factor.")
}

bayesfactor_parameters(
Expand Down
2 changes: 0 additions & 2 deletions R/describe_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,6 @@ describe_posterior.default <- function(posteriors, ...) {
row.names(out) <- NULL
}



# Prepare output
attr(out, "ci_method") <- ci_method
out
Expand Down
4 changes: 2 additions & 2 deletions R/mediation.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mediation.stanmvreg <- function(model, treatment, mediator, response = NULL, cen
# check for binary response. In this case, user should rescale variables
modelinfo <- insight::model_info(model)
if (any(sapply(modelinfo, function(i) i$is_binomial, simplify = TRUE))) {
message("One of moderator or outcome is binary, so direct and indirect effects may be on different scales. Consider rescaling model predictors, e.g. with `effectsize::standardize()`.")
insight::format_alert("One of moderator or outcome is binary, so direct and indirect effects may be on different scales. Consider rescaling model predictors, e.g. with `effectsize::standardize()`.")
}

# model responses
Expand Down Expand Up @@ -355,7 +355,7 @@ print.bayestestR_mediation <- function(x, digits = 3, ...) {
)

if (any(prop_mediated_ori$Estimate < 0)) {
message("\nDirect and indirect effects have opposite directions. The proportion mediated is not meaningful.")
insight::format_alert("\nDirect and indirect effects have opposite directions. The proportion mediated is not meaningful.")
}
}

Expand Down
9 changes: 8 additions & 1 deletion R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,49 @@ plot.bayestestR_eti <- function(x, ...) {
NextMethod()
}


#' @export
plot.bayestestR_si <- function(x, ...) {
insight::check_if_installed("see", "to plot support intervals")
NextMethod()
}


#' @export
plot.bayesfactor_parameters <- function(x, ...) {
insight::check_if_installed("see", "to plot Savage-Dickey Bayes factor")
NextMethod()
}


#' @export
plot.bayesfactor_models <- function(x, ...) {
insight::check_if_installed("see", "to plot models' Bayes factors")
NextMethod()
}


#' @export
plot.estimate_density <- function(x, ...) {
insight::check_if_installed("see", "to plot densities")
NextMethod()
}


#' @export
plot.estimate_density_df <- function(x, ...) {
insight::check_if_installed("see", "to plot models' densities")
NextMethod()
}


#' @export
plot.p_significance <- function(x, ...) {
insight::check_if_installed("see", "to plot practical significance")
NextMethod()
}


#' @export
plot.describe_posterior <- function(x, stack = FALSE, ...) {
insight::check_if_installed("see", "to plot posterior samples")
Expand All @@ -91,6 +98,6 @@ plot.describe_posterior <- function(x, stack = FALSE, ...) {
graphics::plot(estimate_density(model), stack = stack, ...) +
ggplot2::labs(title = "Posterior Samples", x = NULL, y = NULL)
} else {
warning(insight::format_message("Could not find model-object. Try ' plot(estimate_density(model))' instead."), call. = FALSE)
insight::format_warning("Could not find model-object. Try `plot(estimate_density(model))` instead.")
}
}
2 changes: 1 addition & 1 deletion R/simulate_priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ simulate_prior.bcplm <- function(model, n = 1000, verbose = TRUE, ...) {
}

if (sim_error_msg && verbose) {
warning(paste0("Can't simulate priors from a ", prior$Distribution, " distribution."), call. = FALSE)
insight::format_warning(paste0("Can't simulate priors from a ", prior$Distribution, " distribution."))
}

simulated$.bamboozled <- NULL
Expand Down
8 changes: 4 additions & 4 deletions R/unupdate.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ unupdate.stanreg <- function(model, verbose = TRUE, ...) {
}

if (verbose) {
message("Sampling priors, please wait...")
insight::format_alert("Sampling priors, please wait...")
}

prior_dists <- sapply(rstanarm::prior_summary(model), `[[`, "dist")
Expand Down Expand Up @@ -63,7 +63,7 @@ unupdate.brmsfit <- function(model, verbose = TRUE, ...) {
}

if (verbose) {
message("Sampling priors, please wait...")
insight::format_alert("Sampling priors, please wait...")
}

utils::capture.output({
Expand Down Expand Up @@ -99,7 +99,7 @@ unupdate.brmsfit_multiple <- function(model,
}

if (verbose) {
message("Sampling priors, please wait...")
insight::format_alert("Sampling priors, please wait...")
}

utils::capture.output({model_prior <-
Expand Down Expand Up @@ -138,7 +138,7 @@ unupdate.blavaan <- function(model, verbose = TRUE, ...) {
}

if (verbose) {
message("Sampling priors, please wait...")
insight::format_alert("Sampling priors, please wait...")
}

cl$prisamp <- TRUE
Expand Down
12 changes: 11 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,34 @@
return(temp)
}
if (isTRUE(is_stan_mv)) {
# for models with multiple responses, we create a separate response column
temp$Response <- gsub("(b\\[)*(.*)\\|(.*)", "\\2", temp$Parameter)
# from the parameter names, we can now remove the name of the respone variables
for (i in unique(temp$Response)) {
temp$Parameter <- gsub(sprintf("%s|", i), "", temp$Parameter, fixed = TRUE)
}
merge_by <- c("Parameter", "Effects", "Component", "Response")
remove_cols <- c("Group", "Cleaned_Parameter", "Function", ".roworder")
} else if (isTRUE(is_brms_mv)) {
# for models with multiple responses, we create a separate response column
temp$Response <- gsub("(.*)_(.*)_(.*)", "\\2", temp$Parameter)
# temp$Parameter <- gsub("(.*)_(.*)_(.*)", "\\1_\\3", temp$Parameter)
merge_by <- c("Parameter", "Effects", "Component", "Response")
remove_cols <- c("Group", "Cleaned_Parameter", "Function", ".roworder")
} else {
# By default, we only merge by these three columns
merge_by <- c("Parameter", "Effects", "Component")
remove_cols <- c("Group", "Cleaned_Parameter", "Response", "Function", ".roworder")
}

# in "temp", we have the data frame from the related functions (like
# `point_estimate()`, `ci()` etc.). "cleaned_parameters" is a data frame
# only with original parameter names, model components and "cleaned"
# parameter names (retrieved from `insight::clean_parameters()`).

merge_by <- intersect(merge_by, colnames(temp))
temp$.roworder <- seq_len(nrow(temp))
out <- merge(x = temp, y = cleaned_parameters, by = merge_by, all.x = TRUE)

# hope this works for stanmvreg...
if ((isTRUE(is_stan_mv) || isTRUE(is_brms_mv)) && all(is.na(out$Effects)) && all(is.na(out$Component))) {
out$Effects <- cleaned_parameters$Effects[seq_len(nrow(out))]
Expand Down
10 changes: 2 additions & 8 deletions R/utils_bayesfactor.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@

if (is.null(prior)) {
prior <- posterior
warning(
"Prior not specified! ",
"Please provide the original model to get meaningful results."
)
insight::format_warning("Prior not specified! Please provide the original model to get meaningful results.")
}


Expand Down Expand Up @@ -132,10 +129,7 @@
verbose = TRUE) {
if (is.null(prior)) {
prior <- posterior
warning(
"Prior not specified! ",
"Please provide the original model to get meaningful results."
)
insight::format_warning("Prior not specified! Please provide the original model to get meaningful results.")
}

if (!inherits(prior, "emm_list")) {
Expand Down
16 changes: 14 additions & 2 deletions R/utils_check_collinearity.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,26 @@
results <- results[results$corr > threshold & results$corr <= 0.9, ]
if (nrow(results) > 0) {
where <- paste0("between ", toString(paste0(results$where, " (r = ", round(results$corr, 2), ")")), "")
message("Possible multicollinearity ", where, ". This might lead to inappropriate results. See 'Details' in '?", method, "'.")
insight::format_alert(paste0(
"Possible multicollinearity ",
where,
". This might lead to inappropriate results. See 'Details' in '?",
method,
"'."
))
}

# Filter by second threshold
results <- results[results$corr > 0.9, ]
if (nrow(results) > 0) {
where <- paste0("between ", toString(paste0(results$where, " (r = ", round(results$corr, 2), ")")), "")
warning("Probable multicollinearity ", where, ". This might lead to inappropriate results. See 'Details' in '?", method, "'.", call. = FALSE)
insight::format_warning(paste0(
"Probable multicollinearity ",
where,
". This might lead to inappropriate results. See 'Details' in '?",
method,
"'."
))
}
}
}
Expand Down

0 comments on commit 7ed4e4d

Please sign in to comment.