From 00ba19f9a10ae0a9830471f6c45e7d06b9b8d019 Mon Sep 17 00:00:00 2001 From: Dominique Makowski Date: Thu, 30 Nov 2023 16:49:43 +0000 Subject: [PATCH 01/16] Add % of variance to n_factors plot --- R/data_plot.R | 3 +++ R/plot.n_factors.R | 58 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/R/data_plot.R b/R/data_plot.R index 8a54b9270..c4fef63f3 100644 --- a/R/data_plot.R +++ b/R/data_plot.R @@ -132,6 +132,9 @@ add_plot_attributes <- function(x) { if (!is.null(info$title)) { out[[length(out) + 1L]] <- ggplot2::labs(title = info$title) } + if (!is.null(info$subtitle)) { + out[[length(out) + 1L]] <- ggplot2::labs(subtitle = info$subtitle) + } out } diff --git a/R/plot.n_factors.R b/R/plot.n_factors.R index 71a967037..35655eb1f 100644 --- a/R/plot.n_factors.R +++ b/R/plot.n_factors.R @@ -19,8 +19,18 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { s2[[var]] <- 1:max(x[[var]]) } + if("Variance_Cumulative" %in% names(s1)){ + s2$Variance_Cumulative <- NA + } + dataplot <- rbind(s1, s2[!s2[[var]] %in% s1[[var]], ]) + # Add Variance explained + if("Variance_Explained" %in% names(attributes(x))){ + dataplot$Variance_Cumulative <- NULL # Remove column and re add + dataplot <- merge(dataplot, attributes(x)$Variance_Explained[, c("n_Factors", "Variance_Cumulative")], by = "n_Factors") + } + if (type == "line") { dataplot$x <- factor(dataplot[[var]], levels = rev(sort(levels(dataplot[[var]])))) dataplot$group <- "0" @@ -37,19 +47,31 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { dataplot$y <- dataplot$n_Methods / sum(dataplot$n_Methods) rownames(dataplot) <- NULL + # Labels and titles ----------------------------------------------------- + n_max <- sum(dataplot$n_Methods) + axis_lab <- paste0("% of methods (out of ", n_max, ")") + + # Inverse xlab and ylab for line plot if (type == "line") { attr(dataplot, "info") <- list( "ylab" = paste("Number of", lab), - "xlab" = "Consensus between methods", - "title" = paste("How many", lab, "to retain") + "xlab" = axis_lab ) } else { attr(dataplot, "info") <- list( "xlab" = paste("Number of", lab), - "ylab" = "Consensus between methods", - "title" = paste("How many", lab, "to retain") + "ylab" = axis_lab ) } + # Title + + attr(dataplot, "info")$title <- paste("How many", lab, "to retain") + attr(dataplot, "info")$subtitle <- paste0("Number of ", lab, " considered optimal by various algorithm") + if("Variance_Cumulative" %in% names(dataplot) && type != "line"){ + attr(dataplot, "info")$subtitle <- paste0( + attr(dataplot, "info")$subtitle, + ". The dashed line represent the cumulative percentage of variance explained") + } class(dataplot) <- unique(c("data_plot", "see_n_factors", class(dataplot))) dataplot @@ -83,7 +105,10 @@ data_plot.n_clusters <- data_plot.n_factors #' data(mtcars) #' result <- n_factors(mtcars, type = "PCA") #' result +#' +#' plot(result) # type = "bar" by default #' plot(result, type = "line") +#' plot(result, type = "area") #' #' @export plot.see_n_factors <- function(x, @@ -105,8 +130,9 @@ plot.see_n_factors <- function(x, ) } + # Base plot if (type == "area") { - ggplot(x, aes(x = .data$x, y = .data$y)) + + p <- ggplot(x, aes(x = .data$x, y = .data$y)) + geom_area(fill = flat_colors("grey")) + geom_segment( aes( @@ -115,32 +141,42 @@ plot.see_n_factors <- function(x, y = 0, yend = max(.data$y) ), - color = flat_colors("red"), - linetype = "dashed" + color = flat_colors("red") ) + geom_point(aes(x = .data$x[which.max(.data$y)], y = max(.data$y)), color = flat_colors("red") ) + - scale_y_continuous(labels = .percents) + scale_x_continuous(breaks = 1:max(x$x)) + add_plot_attributes(x) } else if (type == "line") { - ggplot(x, aes(y = .data$x, x = .data$y, colour = .data$group)) + + p <- ggplot(x, aes(y = .data$x, x = .data$y, colour = .data$group)) + geom_segment(aes(x = 0, yend = .data$x, xend = .data$y), linewidth = size) + geom_point(size = 2 * size) + guides(colour = "none") + scale_x_continuous(labels = .percents) + scale_color_manual(values = unname(flat_colors(c("grey", "red")))) + add_plot_attributes(x) + # If line, return plot as variance explained cannot be added due to the horizontal orientation of the plot + return(p) } else { - ggplot(x, aes(x = .data$x, y = .data$y, fill = .data$fill)) + + p <- ggplot(x, aes(x = .data$x, y = .data$y, fill = .data$fill)) + geom_bar(stat = "identity", width = size) + guides(fill = "none") + - scale_y_continuous(labels = .percents) + add_plot_attributes(x) + scale_x_continuous(breaks = 1:max(x$x)) + scale_fill_manual(values = unname(flat_colors(c("grey", "red")))) } + + # Add variance explained + if("Variance_Cumulative" %in% names(x)) { + x$Varex_scaled <- x$Variance_Cumulative * max(x$y) + p <- p + + geom_line(data=x, aes(x = .data$x, y = .data$Varex_scaled, group=1), linetype="dashed") + + scale_y_continuous(labels = .percents, sec.axis = sec_axis(~ . / max(x$y), name = "% of variance explained", labels = .percents)) + } else { + p <- p + scale_y_continuous(labels = .percents) + } + p } #' @export From f444f767376af407a1c3ff83611f5345a1eae47a Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 1 Dec 2023 09:16:17 +0100 Subject: [PATCH 02/16] lintrs, news, desc --- DESCRIPTION | 2 +- NEWS.md | 7 ++++ R/geom_binomdensity.R | 2 +- R/geom_from_list.R | 50 +++++++++++++++++----------- R/plot.n_factors.R | 62 +++++++++++++++++++++-------------- R/plot.parameters_brms_meta.R | 18 +++++----- 6 files changed, 85 insertions(+), 56 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d18611b18..052aed07a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: see Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2' -Version: 0.8.1 +Version: 0.8.1.1 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NEWS.md b/NEWS.md index 3971c6217..caaa42dc4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# see 0.8.2 + +## Minor Changes + +* `plot.n_factors()` now shows a dashed line over the bars, indicating the + cumulate explained variance by the number of factors. + # see 0.8.1 ## Major Changes diff --git a/R/geom_binomdensity.R b/R/geom_binomdensity.R index e430baecb..ab951c513 100644 --- a/R/geom_binomdensity.R +++ b/R/geom_binomdensity.R @@ -62,7 +62,7 @@ geom_binomdensity <- function(data, # Other parameters data$.side <- ifelse(data[[y]] == y_levels[1], "top", "bottom") - data$.justification <- as.numeric(!(data[[y]] == y_levels[1])) + data$.justification <- as.numeric(data[[y]] != y_levels[1]) data$.scale <- .geom_binomdensity_scale(data, x, y, scale) # ggdist geom diff --git a/R/geom_from_list.R b/R/geom_from_list.R index 52a9186a4..65a78a0f2 100644 --- a/R/geom_from_list.R +++ b/R/geom_from_list.R @@ -114,58 +114,68 @@ #' @export geom_from_list <- function(x, ...) { # Additional parameters ------------------------------------------------------ - args <- x[!names(x) %in% c("geom", "aes", "data", "width", "height", "position", "show.legend")] + arguments <- x[!names(x) %in% c("geom", "aes", "data", "width", "height", "position", "show.legend")] if (is.null(x$geom)) { return(NULL) } if (inherits(x$geom, "function")) { - return(do.call(x$geom, args)) + return(do.call(x$geom, args = arguments)) } if (x$geom %in% c("density_2d", "density_2d_filled", "density_2d_polygon")) { - if (!"contour" %in% names(args)) args$contour <- TRUE - if (!"contour_var" %in% names(args)) args$contour_var <- "density" + if (!"contour" %in% names(arguments)) arguments$contour <- TRUE + if (!"contour_var" %in% names(arguments)) arguments$contour_var <- "density" } # If they are not geoms, return immediately if (x$geom == "labs") { - return(do.call(ggplot2::labs, args)) + return(do.call(ggplot2::labs, args = arguments)) } if (x$geom == "guides") { - return(do.call(ggplot2::guides, args)) + return(do.call(ggplot2::guides, args = arguments)) } if (x$geom == "coord_flip") { - return(do.call(ggplot2::coord_flip, args)) + return(do.call(ggplot2::coord_flip, args = arguments)) } if (x$geom == "facet_wrap") { - return(do.call(ggplot2::facet_wrap, args)) + return(do.call(ggplot2::facet_wrap, args = arguments)) } if (x$geom == "facet_grid") { - return(do.call(ggplot2::facet_grid, args)) + return(do.call(ggplot2::facet_grid, args = arguments)) } if (x$geom == "smooth") { - if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) - if (!"method" %in% names(args)) args$method <- "loess" - if (!"formula" %in% names(args)) args$formula <- "y ~ x" - return(do.call(ggplot2::geom_smooth, args)) + if (!is.null(x$aes)) { + arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) + } + if (!"method" %in% names(arguments)) { + arguments$method <- "loess" + } + if (!"formula" %in% names(arguments)) { + arguments$formula <- "y ~ x" + } + return(do.call(ggplot2::geom_smooth, args = arguments)) } if (startsWith(x$geom, "scale_") || startsWith(x$geom, "theme") || startsWith(x$geom, "see_")) { - return(do.call(x$geom, args)) + return(do.call(x$geom, args = arguments)) } if (startsWith(x$geom, "ggside::")) { insight::check_if_installed("ggside") - if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) - return(do.call(eval(parse(text = x$geom)), args)) + if (!is.null(x$aes)) { + arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) + } + return(do.call(eval(parse(text = x$geom)), args = arguments)) } if (startsWith(x$geom, "ggraph::")) { insight::check_if_installed("ggraph") - if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) - return(do.call(eval(parse(text = x$geom)), args)) + if (!is.null(x$aes)) { + arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) + } + return(do.call(eval(parse(text = x$geom)), args = arguments)) } # Default parameters @@ -212,7 +222,7 @@ geom_from_list <- function(x, ...) { # Aesthetics if ("aes" %in% names(x)) { - aes_list <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) + aes_list <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) } else { aes_list <- NULL } @@ -231,7 +241,7 @@ geom_from_list <- function(x, ...) { geom = x$geom, mapping = aes_list, data = x$data, - params = args, + params = arguments, show.legend = show.legend, ... ) diff --git a/R/plot.n_factors.R b/R/plot.n_factors.R index 35655eb1f..a58af6888 100644 --- a/R/plot.n_factors.R +++ b/R/plot.n_factors.R @@ -3,43 +3,47 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { s1 <- summary(x) if ("n_Factors" %in% names(x)) { - var <- "n_Factors" + variable <- "n_Factors" lab <- "factors" } else { - var <- "n_Clusters" + variable <- "n_Clusters" lab <- "clusters" } - s2 <- data.frame(n_Methods = rep(0, max(x[[var]]))) + s2 <- data.frame(n_Methods = rep(0, max(x[[variable]]))) if (type == "line") { - s1[[var]] <- as.factor(s1[[var]]) - s2[[var]] <- factor(1:max(x[[var]])) + s1[[variable]] <- as.factor(s1[[variable]]) + s2[[variable]] <- factor(1:max(x[[variable]])) } else { - s2[[var]] <- 1:max(x[[var]]) + s2[[variable]] <- 1:max(x[[variable]]) } - if("Variance_Cumulative" %in% names(s1)){ + if ("Variance_Cumulative" %in% names(s1)) { s2$Variance_Cumulative <- NA } - dataplot <- rbind(s1, s2[!s2[[var]] %in% s1[[var]], ]) + dataplot <- rbind(s1, s2[!s2[[variable]] %in% s1[[variable]], ]) # Add Variance explained - if("Variance_Explained" %in% names(attributes(x))){ + if ("Variance_Explained" %in% names(attributes(x))) { dataplot$Variance_Cumulative <- NULL # Remove column and re add - dataplot <- merge(dataplot, attributes(x)$Variance_Explained[, c("n_Factors", "Variance_Cumulative")], by = "n_Factors") + dataplot <- merge( + dataplot, + attributes(x)$Variance_Explained[, c("n_Factors", "Variance_Cumulative")], + by = "n_Factors" + ) } if (type == "line") { - dataplot$x <- factor(dataplot[[var]], levels = rev(sort(levels(dataplot[[var]])))) + dataplot$x <- factor(dataplot[[variable]], levels = rev(sort(levels(dataplot[[variable]])))) dataplot$group <- "0" dataplot$group[which.max(dataplot$n_Methods)] <- "1" } else if (type == "area") { - dataplot$x <- dataplot[[var]] + dataplot$x <- dataplot[[variable]] } else { - dataplot <- dataplot[order(dataplot[[var]]), ] - dataplot$x <- dataplot[[var]] + dataplot <- dataplot[order(dataplot[[variable]]), ] + dataplot$x <- dataplot[[variable]] dataplot$fill <- "Not-optimal" dataplot$fill[which.max(dataplot$n_Methods)] <- "Optimal" } @@ -54,23 +58,24 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { # Inverse xlab and ylab for line plot if (type == "line") { attr(dataplot, "info") <- list( - "ylab" = paste("Number of", lab), - "xlab" = axis_lab + ylab = paste("Number of", lab), + xlab = axis_lab ) } else { attr(dataplot, "info") <- list( - "xlab" = paste("Number of", lab), - "ylab" = axis_lab + xlab = paste("Number of", lab), + ylab = axis_lab ) } # Title attr(dataplot, "info")$title <- paste("How many", lab, "to retain") attr(dataplot, "info")$subtitle <- paste0("Number of ", lab, " considered optimal by various algorithm") - if("Variance_Cumulative" %in% names(dataplot) && type != "line"){ + if ("Variance_Cumulative" %in% names(dataplot) && type != "line") { attr(dataplot, "info")$subtitle <- paste0( attr(dataplot, "info")$subtitle, - ". The dashed line represent the cumulative percentage of variance explained") + ". The dashed line represent the cumulative percentage of variance explained" + ) } class(dataplot) <- unique(c("data_plot", "see_n_factors", class(dataplot))) @@ -124,8 +129,8 @@ plot.see_n_factors <- function(x, if (missing(size)) { size <- switch(type, - "bar" = 0.7, - "line" = 1, + bar = 0.7, + line = 1, 1 ) } @@ -168,11 +173,18 @@ plot.see_n_factors <- function(x, } # Add variance explained - if("Variance_Cumulative" %in% names(x)) { + if ("Variance_Cumulative" %in% names(x)) { x$Varex_scaled <- x$Variance_Cumulative * max(x$y) p <- p + - geom_line(data=x, aes(x = .data$x, y = .data$Varex_scaled, group=1), linetype="dashed") + - scale_y_continuous(labels = .percents, sec.axis = sec_axis(~ . / max(x$y), name = "% of variance explained", labels = .percents)) + geom_line( + data = x, + aes(x = .data$x, y = .data$Varex_scaled, group = 1), + linetype = "dashed" + ) + + scale_y_continuous( + labels = .percents, + sec.axis = sec_axis(~ . / max(x$y), name = "% of variance explained", labels = .percents) + ) } else { p <- p + scale_y_continuous(labels = .percents) } diff --git a/R/plot.parameters_brms_meta.R b/R/plot.parameters_brms_meta.R index 7558d83ad..f97e20d44 100644 --- a/R/plot.parameters_brms_meta.R +++ b/R/plot.parameters_brms_meta.R @@ -59,11 +59,11 @@ data_plot.parameters_brms_meta <- function(x, data = NULL, normalize_height = TR attr(dataplot, "summary") <- summary attr(dataplot, "info") <- list( - "xlab" = "Standardized Mean Difference", - "ylab" = NULL, - "legend_fill" = NULL, - "legend_color" = NULL, - "title" = "Bayesian Meta-Analysis" + xlab = "Standardized Mean Difference", + ylab = NULL, + legend_fill = NULL, + legend_color = NULL, + title = "Bayesian Meta-Analysis" ) class(dataplot) <- unique(c("data_plot", "see_parameters_brms_meta", class(dataplot))) @@ -212,12 +212,12 @@ plot.see_parameters_brms_meta <- function(x, theme_lucid() + ggplot2::scale_y_discrete() + ggplot2::scale_fill_manual(values = c( - "Study" = unname(metro_colors("light blue")), - "Overall" = unname(metro_colors("amber")) + Study = unname(metro_colors("light blue")), + Overall = unname(metro_colors("amber")) )) + ggplot2::scale_colour_manual(values = c( - "Study" = unname(metro_colors("light blue")), - "Overall" = unname(metro_colors("amber")) + Study = unname(metro_colors("light blue")), + Overall = unname(metro_colors("amber")) )) + ggplot2::guides(fill = "none", colour = "none") + add_plot_attributes(x) From 905cde5d4142683e81c31f5d6ca727d779d5194b Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 1 Dec 2023 09:19:48 +0100 Subject: [PATCH 03/16] lintr --- R/plot.check_collinearity.R | 50 +++++++++++++++---------------- R/plot.check_heteroscedasticity.R | 24 +++++++-------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/R/plot.check_collinearity.R b/R/plot.check_collinearity.R index 594c556ec..cf84d66e2 100644 --- a/R/plot.check_collinearity.R +++ b/R/plot.check_collinearity.R @@ -69,11 +69,11 @@ plot.see_check_collinearity <- function(x, xlim <- nrow(x) if (ylim < 10) ylim <- 10 - if (!is.null(ci_data)) { - x <- cbind(x, ci_data) - } else { + if (is.null(ci_data)) { x$VIF_CI_low <- NA_real_ x$VIF_CI_high <- NA_real_ + } else { + x <- cbind(x, ci_data) } # make sure legend is properly sorted @@ -119,31 +119,29 @@ plot.see_check_collinearity <- function(x, color = NA, alpha = 0.15 ) + - { - if (!is.null(ci_data)) { - list( - ggplot2::geom_linerange( - linewidth = size_line, - na.rm = TRUE + if (!is.null(ci_data)) { + list( + ggplot2::geom_linerange( + linewidth = size_line, + na.rm = TRUE + ), + ggplot2::geom_segment( + data = x[x$VIF_CI_high > ylim * 1.15, ], + mapping = aes( + x = .data$x, + xend = .data$x, + y = .data$y, + yend = .data$VIF_CI_high + ), + lineend = "round", + linejoin = "round", + arrow = ggplot2::arrow( + ends = "last", type = "closed", + angle = 20, length = ggplot2::unit(0.03, "native") ), - ggplot2::geom_segment( - data = x[x$VIF_CI_high > ylim * 1.15, ], - mapping = aes( - x = .data$x, - xend = .data$x, - y = .data$y, - yend = .data$VIF_CI_high - ), - lineend = "round", - linejoin = "round", - arrow = ggplot2::arrow( - ends = "last", type = "closed", - angle = 20, length = ggplot2::unit(0.03, "native") - ), - show.legend = FALSE - ) + show.legend = FALSE ) - } + ) } + geom_point2( size = size_point, diff --git a/R/plot.check_heteroscedasticity.R b/R/plot.check_heteroscedasticity.R index 53cd814c2..18c9b9260 100644 --- a/R/plot.check_heteroscedasticity.R +++ b/R/plot.check_heteroscedasticity.R @@ -24,21 +24,19 @@ plot.see_check_heteroscedasticity <- function(x, data = NULL, ...) { faminfo <- insight::model_info(model) r <- tryCatch( - { - if (inherits(model, "merMod")) { - stats::residuals(model, scaled = TRUE) - } else if (inherits(model, c("glmmTMB", "MixMod"))) { - sigma <- if (faminfo$is_mixed) { - sqrt(insight::get_variance_residual(model)) - } else { - .sigma_glmmTMB_nonmixed(model, faminfo) - } - stats::residuals(model) / sigma - } else if (inherits(model, "glm")) { - stats::rstandard(model, type = "pearson") + if (inherits(model, "merMod")) { + stats::residuals(model, scaled = TRUE) + } else if (inherits(model, c("glmmTMB", "MixMod"))) { + sig <- if (faminfo$is_mixed) { + sqrt(insight::get_variance_residual(model)) } else { - stats::rstandard(model) + .sigma_glmmTMB_nonmixed(model, faminfo) } + stats::residuals(model) / sig + } else if (inherits(model, "glm")) { + stats::rstandard(model, type = "pearson") + } else { + stats::rstandard(model) }, error = function(e) { NULL From 4c12bbf81bc5e4b179f9904723674d53a9403941 Mon Sep 17 00:00:00 2001 From: Dominique Makowski Date: Fri, 1 Dec 2023 09:34:06 +0000 Subject: [PATCH 04/16] minor x-axis --- R/plot.n_factors.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/plot.n_factors.R b/R/plot.n_factors.R index a58af6888..dd04bcfa9 100644 --- a/R/plot.n_factors.R +++ b/R/plot.n_factors.R @@ -58,12 +58,12 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { # Inverse xlab and ylab for line plot if (type == "line") { attr(dataplot, "info") <- list( - ylab = paste("Number of", lab), + ylab = paste("Optimal number of", lab), xlab = axis_lab ) } else { attr(dataplot, "info") <- list( - xlab = paste("Number of", lab), + xlab = paste("Optimal number of", lab), ylab = axis_lab ) } From bc44abd1b2e255f1d1d0cc2e465c59b0df8b8f12 Mon Sep 17 00:00:00 2001 From: Dominique Makowski Date: Fri, 1 Dec 2023 09:34:24 +0000 Subject: [PATCH 05/16] Revert "minor x-axis" This reverts commit 4c12bbf81bc5e4b179f9904723674d53a9403941. --- R/plot.n_factors.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/plot.n_factors.R b/R/plot.n_factors.R index dd04bcfa9..a58af6888 100644 --- a/R/plot.n_factors.R +++ b/R/plot.n_factors.R @@ -58,12 +58,12 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { # Inverse xlab and ylab for line plot if (type == "line") { attr(dataplot, "info") <- list( - ylab = paste("Optimal number of", lab), + ylab = paste("Number of", lab), xlab = axis_lab ) } else { attr(dataplot, "info") <- list( - xlab = paste("Optimal number of", lab), + xlab = paste("Number of", lab), ylab = axis_lab ) } From 3ebc6dc2d123cc4ffa346494d70123d8bddde8db Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 Dec 2023 13:49:46 +0100 Subject: [PATCH 06/16] remotes --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 052aed07a..713153ef4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -117,3 +117,4 @@ Config/Needs/website: rstudio/bslib, r-lib/pkgdown, easystats/easystatstemplate +Remotes: easystats/parameters From 4745e5b22d7c79a4be0b7543474f39430c289b93 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 Dec 2023 13:52:46 +0100 Subject: [PATCH 07/16] lintr --- R/plot.check_collinearity.R | 48 +++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/R/plot.check_collinearity.R b/R/plot.check_collinearity.R index cf84d66e2..5cf6f8d7c 100644 --- a/R/plot.check_collinearity.R +++ b/R/plot.check_collinearity.R @@ -118,31 +118,33 @@ plot.see_check_collinearity <- function(x, fill = colors[3], color = NA, alpha = 0.15 - ) + - if (!is.null(ci_data)) { - list( - ggplot2::geom_linerange( - linewidth = size_line, - na.rm = TRUE + ) + + if (!is.null(ci_data)) { + p <- p + + ggplot2::geom_linerange( + linewidth = size_line, + na.rm = TRUE + ) + + ggplot2::geom_segment( + data = x[x$VIF_CI_high > ylim * 1.15, ], + mapping = aes( + x = .data$x, + xend = .data$x, + y = .data$y, + yend = .data$VIF_CI_high ), - ggplot2::geom_segment( - data = x[x$VIF_CI_high > ylim * 1.15, ], - mapping = aes( - x = .data$x, - xend = .data$x, - y = .data$y, - yend = .data$VIF_CI_high - ), - lineend = "round", - linejoin = "round", - arrow = ggplot2::arrow( - ends = "last", type = "closed", - angle = 20, length = ggplot2::unit(0.03, "native") - ), - show.legend = FALSE - ) + lineend = "round", + linejoin = "round", + arrow = ggplot2::arrow( + ends = "last", type = "closed", + angle = 20, length = ggplot2::unit(0.03, "native") + ), + show.legend = FALSE ) - } + + } + + p <- p + geom_point2( size = size_point, na.rm = TRUE From 692847a8298c4b0c2c485f4b379c02498bbc5748 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 Dec 2023 13:55:40 +0100 Subject: [PATCH 08/16] lintr --- R/plot.parameters_brms_meta.R | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/R/plot.parameters_brms_meta.R b/R/plot.parameters_brms_meta.R index f97e20d44..b16f052b6 100644 --- a/R/plot.parameters_brms_meta.R +++ b/R/plot.parameters_brms_meta.R @@ -19,45 +19,45 @@ data_plot.parameters_brms_meta <- function(x, data = NULL, normalize_height = TR } # summary - summary <- x[, 1:6] - summary$Parameter <- attributes(x)$cleaned_parameters - colnames(summary)[2] <- "Estimate" - summary$Estimate_CI <- sprintf( + datasummary <- x[, 1:6] + datasummary$Parameter <- attributes(x)$cleaned_parameters + colnames(datasummary)[2] <- "Estimate" + datasummary$Estimate_CI <- sprintf( "%.2f %s", - summary$Estimate, + datasummary$Estimate, insight::format_ci( - summary$CI_low, - summary$CI_high, + datasummary$CI_low, + datasummary$CI_high, ci = NULL, digits = 2, zap_small = TRUE ) ) - summary$Parameter <- factor(summary$Parameter, levels = rev(unique(summary$Parameter))) - colnames(summary)[match("Parameter", colnames(summary))] <- "Study" + datasummary$Parameter <- factor(datasummary$Parameter, levels = rev(unique(datasummary$Parameter))) + colnames(datasummary)[match("Parameter", colnames(datasummary))] <- "Study" - summary$x <- NA_real_ - summary$y <- NA_real_ - summary$Color <- "Study" - summary$Color[summary$Study == "Overall"] <- "Overall" + datasummary$x <- NA_real_ + datasummary$y <- NA_real_ + datasummary$Color <- "Study" + datasummary$Color[datasummary$Study == "Overall"] <- "Overall" if ("ROPE_low" %in% names(x) && "ROPE_high" %in% names(x)) { - attr(summary, "rope") <- c(x$ROPE_low[1], x$ROPE_high[1]) + attr(datasummary, "rope") <- c(x$ROPE_low[1], x$ROPE_high[1]) } dataplot <- dataplot[dataplot$Study != "tau", ] - summary <- summary[summary$Study != "tau", ] + datasummary <- datasummary[datasummary$Study != "tau", ] dataplot$Study <- droplevels(dataplot$Study) - summary$Study <- droplevels(summary$Study) + datasummary$Study <- droplevels(datasummary$Study) dataplot$Group <- "Study" dataplot$Group[dataplot$Study == "Overall"] <- "Overall" dataplot$Color <- "Study" dataplot$Color[dataplot$Study == "Overall"] <- "Overall" - attr(dataplot, "summary") <- summary + attr(dataplot, "summary") <- datasummary attr(dataplot, "info") <- list( xlab = "Standardized Mean Difference", ylab = NULL, @@ -164,7 +164,7 @@ plot.see_parameters_brms_meta <- function(x, x <- data_plot(x, data = model, normalize_height = normalize_height, ...) } - summary <- attributes(x)$summary + datasummary <- attributes(x)$summary rope <- attributes(summary)$rope p <- ggplot2::ggplot(x, mapping = ggplot2::aes(x = .data$x, y = .data$Study, height = .data$y)) @@ -192,7 +192,7 @@ plot.see_parameters_brms_meta <- function(x, alpha = posteriors_alpha ) + ggplot2::geom_errorbarh( - data = summary, + data = datasummary, mapping = ggplot2::aes( xmin = .data$CI_low, xmax = .data$CI_high, @@ -201,7 +201,7 @@ plot.see_parameters_brms_meta <- function(x, linewidth = size_line ) + ggplot2::geom_point( - data = summary, + data = datasummary, mapping = ggplot2::aes(x = .data$Estimate, color = .data$Color), size = size_point, fill = "white", @@ -229,7 +229,7 @@ plot.see_parameters_brms_meta <- function(x, p <- p + ggplot2::geom_text( - data = summary, + data = datasummary, mapping = ggplot2::aes(label = .data$Estimate_CI, x = Inf), hjust = "inward", size = size_text From 34c413def82e0dc6d474e9c264c5d10cd64d0734 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 Dec 2023 13:56:26 +0100 Subject: [PATCH 09/16] lintr --- R/plot.parameters_brms_meta.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/plot.parameters_brms_meta.R b/R/plot.parameters_brms_meta.R index b16f052b6..34504d80e 100644 --- a/R/plot.parameters_brms_meta.R +++ b/R/plot.parameters_brms_meta.R @@ -150,9 +150,7 @@ plot.see_parameters_brms_meta <- function(x, ...) { # save model for later use model <- tryCatch( - { - .retrieve_data(x) - }, + .retrieve_data(x), error = function(e) { priors <- FALSE NULL From 228c715a0ed43043b292a2015aa813a57075c378 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 5 Dec 2023 09:55:50 +0100 Subject: [PATCH 10/16] wordlist --- inst/WORDLIST | 1 + 1 file changed, 1 insertion(+) diff --git a/inst/WORDLIST b/inst/WORDLIST index 76cfea105..7aaeeedee 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -43,6 +43,7 @@ brms characterisation codecov colour +cumulate datawizard de diavola From dab1ee208d05dd3711d88829434259d7400d299c Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 5 Dec 2023 10:01:16 +0100 Subject: [PATCH 11/16] lintr --- R/data_plot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/data_plot.R b/R/data_plot.R index c4fef63f3..e9de62cc2 100644 --- a/R/data_plot.R +++ b/R/data_plot.R @@ -179,7 +179,7 @@ add_plot_attributes <- function(x) { #' @keywords internal .dynGet <- function(x, - ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA), + ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, .call = FALSE), minframe = 1L, inherits = FALSE) { x <- insight::safe_deparse(x) From eb5b5b9b85c88c2c8386fbeb3512b9308d52f7ce Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 5 Dec 2023 10:18:23 +0100 Subject: [PATCH 12/16] use GH to avoid warning --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 713153ef4..597573aa0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -117,4 +117,4 @@ Config/Needs/website: rstudio/bslib, r-lib/pkgdown, easystats/easystatstemplate -Remotes: easystats/parameters +Remotes: easystats/parameters, easystats/bayestestR From 9746fd1aceef8e4ae58b5a714f81e9ff69c74048 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 31 Jan 2024 21:23:55 +0100 Subject: [PATCH 13/16] fix --- R/data_plot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/data_plot.R b/R/data_plot.R index e9de62cc2..c88c21d5a 100644 --- a/R/data_plot.R +++ b/R/data_plot.R @@ -179,7 +179,7 @@ add_plot_attributes <- function(x) { #' @keywords internal .dynGet <- function(x, - ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, .call = FALSE), + ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, call. = FALSE), minframe = 1L, inherits = FALSE) { x <- insight::safe_deparse(x) From 07c2a33f8480e36a9a727ffc5e7957f8a12101f2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 31 Jan 2024 21:27:26 +0100 Subject: [PATCH 14/16] lintr --- R/geom_from_list.R | 2 +- R/plot.check_model.R | 4 ++-- R/plot.check_normality.R | 2 +- R/plot.check_outliers.R | 12 +++++------- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/R/geom_from_list.R b/R/geom_from_list.R index 65a78a0f2..9196e2c90 100644 --- a/R/geom_from_list.R +++ b/R/geom_from_list.R @@ -189,7 +189,7 @@ geom_from_list <- function(x, ...) { } # Default for violin - if (x$geom == "violin") { + if (x$geom == "violin") { # nolint stat <- "ydensity" position <- "dodge" } else if (x$geom == "boxplot") { diff --git a/R/plot.check_model.R b/R/plot.check_model.R index 29c13ff02..14e0a4930 100644 --- a/R/plot.check_model.R +++ b/R/plot.check_model.R @@ -209,9 +209,9 @@ plot.see_check_model <- function(x, pw <- plots(p, n_columns = n_columns) .safe_print_plots(pw, ...) invisible(pw) - } else { - return(p) } + + p } diff --git a/R/plot.check_normality.R b/R/plot.check_normality.R index 01c62233b..8aae47298 100644 --- a/R/plot.check_normality.R +++ b/R/plot.check_normality.R @@ -68,7 +68,7 @@ plot.see_check_normality <- function(x, alpha_level = alpha ) } else { - if (type == "qq") { + if (type == "qq") { # nolint model_info <- attributes(x)$model_info if (inherits(model, c("lme", "lmerMod", "merMod", "glmmTMB", "afex_aov", "BFBayesFactor"))) { res_ <- suppressMessages(sort(stats::residuals(model), na.last = NA)) diff --git a/R/plot.check_outliers.R b/R/plot.check_outliers.R index eae2b173f..8d8d0f334 100644 --- a/R/plot.check_outliers.R +++ b/R/plot.check_outliers.R @@ -41,9 +41,9 @@ plot.see_check_outliers <- function(x, ...) { type <- match.arg(type) influential_obs <- attributes(x)$influential_obs - methods <- attr(x, "methods", exact = TRUE) + outlier_methods <- attr(x, "methods", exact = TRUE) - if (type == "dots" && !is.null(influential_obs) && (is.null(methods) || length(methods) == 1)) { + if (type == "dots" && !is.null(influential_obs) && (is.null(outlier_methods) || length(outlier_methods) == 1)) { .plot_diag_outliers_new( influential_obs, show_labels = show_labels, @@ -52,12 +52,10 @@ plot.see_check_outliers <- function(x, dot_alpha_level = dot_alpha, colors = colors ) + } else if (length(outlier_methods) == 1) { + .plot_diag_outliers(x, show_labels = show_labels, size_text = size_text, rescale_distance = rescale_distance) } else { - if (length(methods == 1)) { - .plot_diag_outliers(x, show_labels = show_labels, size_text = size_text, rescale_distance = rescale_distance) - } else { - .plot_outliers_multimethod(x, rescale_distance) - } + .plot_outliers_multimethod(x, rescale_distance) } } From 6bd6d81628ebcdc0047907b9af09a3c910fc3213 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 31 Jan 2024 21:29:38 +0100 Subject: [PATCH 15/16] lintr --- R/utils.R | 8 ++--- R/utils_add_prior_layer.R | 62 +++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/R/utils.R b/R/utils.R index 788b05868..87cbf49ac 100644 --- a/R/utils.R +++ b/R/utils.R @@ -38,7 +38,7 @@ .clean_parameter_names <- function(params, grid = FALSE) { params <- unique(params) - labels <- params + parameter_labels <- params # clean parameters names params <- gsub("(b_|bs_|bsp_|bcs_)(.*)", "\\2", params, perl = TRUE) @@ -90,7 +90,7 @@ params <- gsub("(Zero-Inflated) (Random)", "(Random, Zero-Inflated)", params, fixed = TRUE) } - stats::setNames(params, labels) + stats::setNames(params, parameter_labels) } @@ -139,8 +139,8 @@ .remove_intercept <- function(x, column = "Parameter", show_intercept = FALSE) { if (!show_intercept) { - remove <- which(.is_intercept(x[[column]])) - if (length(remove)) x <- x[-remove, ] + to_remove <- which(.is_intercept(x[[column]])) + if (length(to_remove)) x <- x[-to_remove, ] } x } diff --git a/R/utils_add_prior_layer.R b/R/utils_add_prior_layer.R index 8c2dbab76..90b0dbbac 100644 --- a/R/utils_add_prior_layer.R +++ b/R/utils_add_prior_layer.R @@ -17,8 +17,8 @@ # limit xrange, to avoid overly wide plots x_range <- stats::median(dens$x) + 7 * stats::mad(dens$x) * c(-1, 1) - remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) - if (length(remove)) dens <- dens[-remove, ] + to_remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) + if (length(to_remove)) dens <- dens[-to_remove, ] # remove intercept from output, if requested .remove_intercept(dens, column = "Parameter", show_intercept) @@ -60,35 +60,33 @@ na.rm = TRUE ) } + } else if (isTRUE(show_ridge_line)) { + ggridges::geom_ridgeline( + data = dat, + mapping = aes( + x = .data$x, + y = as.factor(.data$Parameter), + height = .data$y, + group = as.factor(.data$Parameter), + fill = "Priors" + ), + alpha = priors_alpha, + na.rm = TRUE + ) } else { - if (isTRUE(show_ridge_line)) { - ggridges::geom_ridgeline( - data = dat, - mapping = aes( - x = .data$x, - y = as.factor(.data$Parameter), - height = .data$y, - group = as.factor(.data$Parameter), - fill = "Priors" - ), - alpha = priors_alpha, - na.rm = TRUE - ) - } else { - ggridges::geom_ridgeline( - data = dat, - mapping = aes( - x = .data$x, - y = as.factor(.data$Parameter), - height = .data$y, - group = as.factor(.data$Parameter), - fill = "Priors" - ), - alpha = priors_alpha, - color = NA, - na.rm = TRUE - ) - } + ggridges::geom_ridgeline( + data = dat, + mapping = aes( + x = .data$x, + y = as.factor(.data$Parameter), + height = .data$y, + group = as.factor(.data$Parameter), + fill = "Priors" + ), + alpha = priors_alpha, + color = NA, + na.rm = TRUE + ) } } } @@ -115,8 +113,8 @@ # limit xrange, to avoid overly wide plots x_range <- stats::median(dens$x) + 7 * stats::mad(dens$x) * c(-1, 1) - remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) - if (length(remove)) dens <- dens[-remove, ] + to_remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) + if (length(to_remove)) dens <- dens[-to_remove, ] # remove intercept from output, if requested .remove_intercept(dens, column = "Parameter", show_intercept) From 14edeb657cdb307b97e0c8f2d27c67af7e41c322 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 31 Jan 2024 22:15:00 +0100 Subject: [PATCH 16/16] styler --- R/plot.n_factors.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/plot.n_factors.R b/R/plot.n_factors.R index a58af6888..33ef1bff5 100644 --- a/R/plot.n_factors.R +++ b/R/plot.n_factors.R @@ -27,7 +27,7 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { # Add Variance explained if ("Variance_Explained" %in% names(attributes(x))) { - dataplot$Variance_Cumulative <- NULL # Remove column and re add + dataplot$Variance_Cumulative <- NULL # Remove column and re add dataplot <- merge( dataplot, attributes(x)$Variance_Explained[, c("n_Factors", "Variance_Cumulative")], @@ -69,7 +69,7 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { } # Title - attr(dataplot, "info")$title <- paste("How many", lab, "to retain") + attr(dataplot, "info")$title <- paste("How many", lab, "to retain") attr(dataplot, "info")$subtitle <- paste0("Number of ", lab, " considered optimal by various algorithm") if ("Variance_Cumulative" %in% names(dataplot) && type != "line") { attr(dataplot, "info")$subtitle <- paste0( @@ -111,7 +111,7 @@ data_plot.n_clusters <- data_plot.n_factors #' result <- n_factors(mtcars, type = "PCA") #' result #' -#' plot(result) # type = "bar" by default +#' plot(result) # type = "bar" by default #' plot(result, type = "line") #' plot(result, type = "area") #'