Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 5, 2024
1 parent da74b4a commit 4eac08c
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 17 deletions.
35 changes: 19 additions & 16 deletions R/plot.p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ data_plot.p_direction <- function(x, data = NULL, show_intercept = FALSE, ...) {
data <- data[, x$Parameter, drop = FALSE]
dataplot <- data.frame()
for (i in names(data)) {
if (!is.null(params)) {
if (!is.null(params) && all(c("Effects", "Component") %in% colnames(params))) {
dataplot <- rbind(
dataplot,
cbind(
.compute_densities_pd(data[[i]], name = i),
"Effects" = params$Effects[params$Parameter == i],
"Component" = params$Component[params$Parameter == i]
.compute_densities_pd(data[[i]], name = i, null = attr(x, "null")),
Effects = params$Effects[params$Parameter == i],
Component = params$Component[params$Parameter == i]
)
)
} else {
dataplot <- rbind(dataplot, .compute_densities_pd(data[[i]], name = i))
dataplot <- rbind(dataplot, .compute_densities_pd(data[[i]], name = i, null = attr(x, "null")))
}
}

Expand All @@ -60,7 +60,7 @@ data_plot.p_direction <- function(x, data = NULL, show_intercept = FALSE, ...) {
}
} else {
levels_order <- NULL
dataplot <- .compute_densities_pd(data[, 1], name = "Posterior")
dataplot <- .compute_densities_pd(data[, 1], name = "Posterior", null = attr(x, "null"))
}

dataplot <- do.call(
Expand All @@ -70,7 +70,7 @@ data_plot.p_direction <- function(x, data = NULL, show_intercept = FALSE, ...) {
list(dataplot$y, dataplot$fill),
function(df) {
df$n <- nrow(df)
return(df)
df
}
)
)
Expand All @@ -81,7 +81,7 @@ data_plot.p_direction <- function(x, data = NULL, show_intercept = FALSE, ...) {
dataplot$y,
function(df) {
df$prop <- df$n / nrow(df)
return(df)
df
}
)
)
Expand All @@ -108,10 +108,10 @@ data_plot.p_direction <- function(x, data = NULL, show_intercept = FALSE, ...) {
dataplot <- .fix_facet_names(dataplot)

attr(dataplot, "info") <- list(
"xlab" = "Possible parameter values",
"ylab" = ylab,
"legend_fill" = "Effect direction",
"title" = "Probability of Direction"
xlab = "Possible parameter values",
ylab = ylab,
legend_fill = "Effect direction",
title = "Probability of Direction"
)

class(dataplot) <- c("data_plot", "see_p_direction", class(dataplot))
Expand All @@ -121,11 +121,14 @@ data_plot.p_direction <- function(x, data = NULL, show_intercept = FALSE, ...) {


#' @keywords internal
.compute_densities_pd <- function(x, name = "Y") {
.compute_densities_pd <- function(x, name = "Y", null = 0) {
out <- .as.data.frame_density(
stats::density(x)
)
out$fill <- ifelse(out$x < 0, "Negative", "Positive")
if (is.null(null)) {
null <- 0
}
out$fill <- ifelse(out$x < null, "Negative", "Positive")
out$height <- as.vector(
(out$y - min(out$y, na.rm = TRUE)) /
diff(range(out$y, na.rm = TRUE), na.rm = TRUE)
Expand Down Expand Up @@ -182,7 +185,7 @@ plot.see_p_direction <- function(x,
params <- unique(x$y)

# get labels
labels <- .clean_parameter_names(x$y, grid = !is.null(n_columns))
axis_labels <- .clean_parameter_names(x$y, grid = !is.null(n_columns))

insight::check_if_installed("ggridges")

Expand Down Expand Up @@ -216,7 +219,7 @@ plot.see_p_direction <- function(x,
if (length(unique(x$y)) == 1 && is.numeric(x$y)) {
p <- p + scale_y_continuous(breaks = NULL, labels = NULL)
} else {
p <- p + scale_y_discrete(labels = labels)
p <- p + scale_y_discrete(labels = axis_labels)
}


Expand Down
1 change: 0 additions & 1 deletion R/plot.p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ data_plot.p_significance <- function(x,

if (inherits(data, "emmGrid")) {
insight::check_if_installed("emmeans")

data <- as.data.frame(as.matrix(emmeans::as.mcmc.emmGrid(data, names = FALSE)))
} else if (inherits(data, c("stanreg", "brmsfit"))) {
params <- insight::clean_parameters(data)
Expand Down
71 changes: 71 additions & 0 deletions tests/testthat/_snaps/plot.p_direction/plot-p-dir-frequ1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions tests/testthat/_snaps/plot.p_direction/plot-p-dir-frequ2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions tests/testthat/_snaps/plot.p_direction/plot-p-dir-glmmtmb.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions tests/testthat/test-plot.p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,41 @@ test_that("`plot.see_p_direction()` works", {

expect_s3_class(plot(result), "gg")
})

skip_on_cran()
skip_if_not_installed("bayestestR", minimum_version = "0.14.1")
skip_if_not_installed("parameters", minimum_version = "0.22.3")

test_that("`plot.see_p_direction works {parameters}}", {
skip_if_not_installed("vdiffr")
data(qol_cancer, package = "parameters")
model <- lm(QoL ~ time + age + education, data = qol_cancer)
set.seed(123)
out <- parameters::p_direction(model)
vdiffr::expect_doppelganger(
title = "plot.p_dir_frequ1",
fig = plot(out)
)
set.seed(123)
out <- parameters::p_direction(model, null = 2)
vdiffr::expect_doppelganger(
title = "plot.p_dir_frequ2",
fig = plot(out)
)
})

test_that("plot p_direction, glmmTMB", {
skip_if_not_installed("glmmTMB")
data(Salamanders, package = "glmmTMB")
m1 <- glmmTMB::glmmTMB(count ~ mined + cover + (1 | site),
zi = ~mined,
family = poisson,
data = Salamanders
)
set.seed(123)
out <- parameters::p_direction(m1)
vdiffr::expect_doppelganger(
title = "plot.p_dir_glmmTMB",
fig = plot(out)
)
})
1 change: 1 addition & 0 deletions tests/testthat/test-plot.p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test_that("`plot.see_p_significance()` works", {
expect_s3_class(plot(result), "gg")
})

skip_on_cran()
skip_if_not_installed("bayestestR", minimum_version = "0.14.1")
skip_if_not_installed("parameters", minimum_version = "0.22.3")

Expand Down

0 comments on commit 4eac08c

Please sign in to comment.