Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 4, 2023
1 parent b8918a3 commit daa1a37
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 486 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: see
Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2'
Version: 0.7.5.9
Version: 0.7.5.10
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
gains a `type` argument, to either create density plots, or discrete dots resp.
interval plots for posterior predictive checks.

* `plot()` for `performance::check_model()` gains an `n_column` argument, to
define the number of columns for the diagnostic plots (by default, two columns).

* `plot()` for `performance::check_model()` sometimes failed to create the plot
under certain conditions, e.g. when the screen or app windows was zoomed-in.
If an error occurs, a much more informative error message is shown, providing
Expand Down
129 changes: 129 additions & 0 deletions R/plot.check_collinearity.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,132 @@ plot.see_check_collinearity <- function(x,
is_check_model = FALSE
)
}


.plot_diag_vif <- function(x,
size_point,
size_line,
theme_style = theme_lucid,
colors = unname(social_colors(c("green", "blue", "red"))),
ci_data = NULL,
is_check_model = FALSE) {
ylim <- ceiling(max(x$y, na.rm = TRUE))
xlim <- nrow(x)
if (ylim < 10) ylim <- 10

if (!is.null(ci_data)) {
x <- cbind(x, ci_data)
} else {
x$VIF_CI_low <- NA_real_
x$VIF_CI_high <- NA_real_
}

# make sure legend is properly sorted
x$group <- factor(x$group, levels = c("low", "moderate", "high"))
levels(x$group) <- c("Low (< 5)", "Moderate (< 10)", "High (\u2265 10)")
names(colors) <- c("Low (< 5)", "Moderate (< 10)", "High (\u2265 10)")

p <- ggplot2::ggplot(x) +
ggplot2::aes(
x = .data$x,
y = .data$y,
color = .data$group,
ymin = .data$VIF_CI_low,
ymax = .data$VIF_CI_high
) +
ggplot2::annotate(
geom = "rect",
xmin = -Inf,
xmax = Inf,
ymin = 1,
ymax = 5,
fill = colors[1],
color = NA,
alpha = 0.15
) +
ggplot2::annotate(
geom = "rect",
xmin = -Inf,
xmax = Inf,
ymin = 5,
ymax = 10,
fill = colors[2],
color = NA,
alpha = 0.15
) +
ggplot2::annotate(
geom = "rect",
xmin = -Inf,
xmax = Inf,
ymin = 10,
ymax = Inf,
fill = colors[3],
color = NA,
alpha = 0.15
) +
{
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")
),
show.legend = FALSE
)
)
}
} +
geom_point2(
size = size_point,
na.rm = TRUE
) +
ggplot2::labs(
title = "Collinearity",
subtitle = "High collinearity (VIF) may inflate parameter uncertainty",
x = NULL,
y = paste("Variance Inflation", "Factor (VIF, log-scaled)", sep = ifelse(is_check_model, "\n", " "))
) +
ggplot2::scale_color_manual(
values = colors,
aesthetics = c("color", "fill"),
guide = ggplot2::guide_legend(title = NULL)
) +
theme_style(
base_size = 10,
plot.title.space = 3,
axis.title.space = 5
) +
ggplot2::scale_y_continuous(
limits = c(1, ylim * 1.15),
oob = scales::oob_squish,
trans = "log10",
expand = c(0, 0),
breaks = scales::log_breaks(n = 7, base = 10)
) +
ggplot2::scale_x_discrete() +
ggplot2::theme(
legend.position = "bottom",
legend.margin = ggplot2::margin(0, 0, 0, 0),
legend.box.margin = ggplot2::margin(-5, -5, -5, -5)
)

if ("facet" %in% colnames(x)) {
p <- p + ggplot2::facet_wrap(~facet, nrow = 1, scales = "free")
}

p
}
42 changes: 42 additions & 0 deletions R/plot.check_homogeneity.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,45 @@ plot.see_check_homogeneity <- function(x, data = NULL, ...) {
) +
theme(plot.title.position = "plot")
}


.plot_diag_homogeneity <- function(x,
size_point,
size_line,
alpha_level = 0.2,
theme_style = theme_lucid,
colors = unname(social_colors(c("green", "blue", "red"))),
dot_alpha_level = 0.8,
show_dots = TRUE) {
p <- ggplot2::ggplot(x, ggplot2::aes(x = .data$x, .data$y))

if (isTRUE(show_dots)) {
p <- p +
geom_point2(
colour = colors[2],
size = size_point,
alpha = dot_alpha_level
)
}

p +
ggplot2::stat_smooth(
method = "loess",
se = TRUE,
alpha = alpha_level,
formula = y ~ x,
linewidth = size_line,
colour = colors[1]
) +
ggplot2::labs(
title = "Homogeneity of Variance",
subtitle = "Reference line should be flat and horizontal",
y = expression(sqrt("|Std. residuals|")),
x = "Fitted values"
) +
theme_style(
base_size = 10,
plot.title.space = 3,
axis.title.space = 5
)
}
Loading

0 comments on commit daa1a37

Please sign in to comment.