Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up some lints #303

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/geom_binomdensity.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
insight::check_if_installed(c("ggplot2", "ggdist"))

# Sanitize y (e.g., if levels with no values, etc.)
if (is.factor(data[[y]]) && length(levels(data[[y]])) > 2L) {
if (is.factor(data[[y]]) && nlevels(data[[y]]) > 2L) {
data[[y]] <- droplevels(data[[y]])
}

Expand Down Expand Up @@ -91,7 +91,7 @@
if (length(scale) == 1 && is.character(scale) && scale %in% c("density", "proportion", "auto")) {
# Density instead of proportion
if (scale == "density") {
prop <- sapply(split(data, data[[y]]), function(df) {

Check warning on line 94 in R/geom_binomdensity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/geom_binomdensity.R,line=94,col=15,[undesirable_function_linter] Function "sapply" is undesirable.

Check warning on line 94 in R/geom_binomdensity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/geom_binomdensity.R,line=94,col=15,[undesirable_function_linter] Function "sapply" is undesirable.
max(stats::density(df[[x]], na.rm = TRUE)$y) * nrow(df)
})
prop <- prop / sum(prop)
Expand Down
7 changes: 1 addition & 6 deletions R/plot.equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@
tmp <- merge(tmp, cp, by = "predictor")
tmp$predictor <- factor(tmp$predictor, levels = rev(unique(tmp$predictor)))

has_multiple_panels <-
(!"Effects" %in% names(tmp) || length(unique(tmp$Effects)) <= 1L) &&
(!"Component" %in% names(tmp) || length(unique(tmp$Component)) <= 1L)

# check if we have multiple panels
if (has_multiple_panels) {
if (.has_multiple_panels(tmp)) {
n_columns <- NULL
}

Expand All @@ -131,7 +126,7 @@

fill.color <- fill.color[sort(unique(match(x$ROPE_Equivalence, c("Accepted", "Rejected", "Undecided"))))]

add.args <- lapply(match.call(expand.dots = FALSE)$`...`, function(x) x)

Check warning on line 129 in R/plot.equivalence_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.equivalence_test.R,line=129,col=54,[keyword_quote_linter] Only quote targets of extraction with $ if necessary, i.e., if the name is not a valid R symbol (see ?make.names). Use backticks to create non-syntactic names, or use [[ to extract by string.
if ("colors" %in% names(add.args)) fill.color <- eval(add.args[["colors"]])
if ("x.title" %in% names(add.args)) x.title <- eval(add.args[["x.title"]])
if ("legend.title" %in% names(add.args)) legend.title <- eval(add.args[["legend.title"]])
Expand Down Expand Up @@ -278,7 +273,7 @@

fill.color <- fill.color[sort(unique(match(x$ROPE_Equivalence, c("Accepted", "Rejected", "Undecided"))))]

add.args <- lapply(match.call(expand.dots = FALSE)$`...`, function(x) x)

Check warning on line 276 in R/plot.equivalence_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.equivalence_test.R,line=276,col=54,[keyword_quote_linter] Only quote targets of extraction with $ if necessary, i.e., if the name is not a valid R symbol (see ?make.names). Use backticks to create non-syntactic names, or use [[ to extract by string.
if ("colors" %in% names(add.args)) fill.color <- eval(add.args[["colors"]])
if ("x.title" %in% names(add.args)) x.title <- eval(add.args[["x.title"]])
if ("legend.title" %in% names(add.args)) legend.title <- eval(add.args[["legend.title"]])
Expand Down Expand Up @@ -395,7 +390,7 @@

fill.color <- fill.color[sort(unique(match(x$ROPE_Equivalence, c("Accepted", "Rejected", "Undecided"))))]

add.args <- lapply(match.call(expand.dots = FALSE)$`...`, function(x) x)

Check warning on line 393 in R/plot.equivalence_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.equivalence_test.R,line=393,col=54,[keyword_quote_linter] Only quote targets of extraction with $ if necessary, i.e., if the name is not a valid R symbol (see ?make.names). Use backticks to create non-syntactic names, or use [[ to extract by string.
if ("colors" %in% names(add.args)) fill.color <- eval(add.args[["colors"]])
if ("x.title" %in% names(add.args)) x.title <- eval(add.args[["x.title"]])
if ("legend.title" %in% names(add.args)) legend.title <- eval(add.args[["legend.title"]])
Expand Down
10 changes: 7 additions & 3 deletions R/plot.estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@

attr(dataplot, "summary") <- summary
attr(dataplot, "info") <- list(
"xlab" = "Values",

Check warning on line 54 in R/plot.estimate_density.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.estimate_density.R,line=54,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"ylab" = "Density",

Check warning on line 55 in R/plot.estimate_density.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.estimate_density.R,line=55,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"legend_fill" = "Parameter",

Check warning on line 56 in R/plot.estimate_density.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.estimate_density.R,line=56,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"legend_color" = "Parameter",

Check warning on line 57 in R/plot.estimate_density.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.estimate_density.R,line=57,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"title" = "Estimated Density Function"

Check warning on line 58 in R/plot.estimate_density.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.estimate_density.R,line=58,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
)

class(dataplot) <- c("data_plot", "see_estimate_density", class(dataplot))
Expand Down Expand Up @@ -123,11 +123,15 @@


if (!inherits(x, "data_plot")) {
x <- data_plot(x, data = model, centrality = centrality, ci = ci, ...)
x <- data_plot(x,
data = model,
centrality = centrality,
ci = ci,
...
)
}

if ((!"Effects" %in% names(x) || length(unique(x$Effects)) <= 1) &&
(!"Component" %in% names(x) || length(unique(x$Component)) <= 1)) {
if (.has_multiple_panels(x)) {
n_columns <- NULL
}

Expand Down
4 changes: 1 addition & 3 deletions R/plot.p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
dataplot,
cbind(
.compute_densities_pd(data[[i]], name = i),
"Effects" = params$Effects[params$Parameter == i],

Check warning on line 35 in R/plot.p_direction.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.p_direction.R,line=35,col=13,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"Component" = params$Component[params$Parameter == i]
)
)
Expand Down Expand Up @@ -174,9 +174,7 @@
x <- data_plot(x, data = data, show_intercept = show_intercept)
}

# check if we have multiple panels
if ((!"Effects" %in% names(x) || length(unique(x$Effects)) <= 1) &&
(!"Component" %in% names(x) || length(unique(x$Component)) <= 1)) {
if (.has_multiple_panels(x)) {
n_columns <- NULL
}

Expand Down
4 changes: 1 addition & 3 deletions R/plot.p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ plot.see_p_significance <- function(x,
x <- data_plot(x, data = data, show_intercept = show_intercept)
}

# check if we have multiple panels
if ((!"Effects" %in% names(x) || length(unique(x$Effects)) <= 1L) &&
(!"Component" %in% names(x) || length(unique(x$Component)) <= 1L)) {
if (.has_multiple_panels(x)) {
n_columns <- NULL
}

Expand Down
4 changes: 1 addition & 3 deletions R/plot.rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ plot.see_rope <- function(x,
x <- data_plot(x, data = data, show_intercept = show_intercept)
}

# check if we have multiple panels
if ((!"Effects" %in% names(x) || length(unique(x$Effects)) <= 1) &&
(!"Component" %in% names(x) || length(unique(x$Component)) <= 1)) {
if (.has_multiple_panels(x)) {
n_columns <- NULL
}

Expand Down
4 changes: 3 additions & 1 deletion R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ plots <- function(...,

# Add tags
if (!is.null(tags)) {
if (length(tags) == 1) {
if (length(tags) == 1L) {
if (isTRUE(tags)) {
tags <- "A"
} else if (isFALSE(tags) || is.na(tags)) {
Expand Down Expand Up @@ -100,13 +100,15 @@ plots <- function(...,
msg_display1 <- "\n- To fix this issue, please make the window larger."
msg_display3 <- "\n- If this still doesn't resolve your problems, you may check whether your apps are rescaled. On Windows, this can be done in the display settings (Start > Settings > System > Display, \"Scale and layout\"). Reduce the scaling and try again."
msg_display4 <- "\n- Finally, you can try to decrease the base font-size of your theme before plotting. Load `library(ggplot2)` and run: `theme_set(theme_classic(base_size = 6))`"

if (Sys.getenv("RSTUDIO") == "1") {
msg <- "The RStudio 'Plots' window is too small to show this set of plots."
msg_display2 <- "\n- If this doesn't help, try to reset your zoom settings. In RStudio, go to Menu \"View > Actual Size\" and then retry."
} else {
msg <- "The viewport is too small to show this set of plots."
msg_display2 <- "\n- If this doesn't help, try to reset the zoom settings of your IDE and then retry."
}

msg <- paste(msg, "You may try one of the following steps to resolve this problem.")
insight::format_error(msg, msg_display1, msg_display2, msg_display3, msg_display4)
}
Expand Down
19 changes: 12 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
as.numeric(as.character(x))
}


.has_multiple_panels <- function(x) {
(!"Effects" %in% names(x) || insight::n_unique(x$Effects) <= 1L) &&
(!"Component" %in% names(x) || insight::n_unique(x$Component) <= 1L)
}
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved

.clean_parameter_names <- function(params, grid = FALSE) {
params <- unique(params)
Expand Down Expand Up @@ -91,23 +94,25 @@
.fix_facet_names <- function(x) {
if ("Component" %in% names(x)) {
x$Component <- as.character(x$Component)
if (!"Effects" %in% names(x)) {
x$Component[x$Component == "conditional"] <- "Conditional"
x$Component[x$Component == "zero_inflated"] <- "Zero-Inflated"
x$Component[x$Component == "dispersion"] <- "Dispersion"
x$Component[x$Component == "simplex"] <- "Monotonic Effects"
} else {
if ("Effects" %in% names(x)) {
x$Component[x$Component == "conditional"] <- "(Conditional)"
x$Component[x$Component == "zero_inflated"] <- "(Zero-Inflated)"
x$Component[x$Component == "dispersion"] <- "(Dispersion)"
x$Component[x$Component == "simplex"] <- "(Monotonic Effects)"
} else {
x$Component[x$Component == "conditional"] <- "Conditional"
x$Component[x$Component == "zero_inflated"] <- "Zero-Inflated"
x$Component[x$Component == "dispersion"] <- "Dispersion"
x$Component[x$Component == "simplex"] <- "Monotonic Effects"
}
}

if ("Effects" %in% names(x)) {
x$Effects <- as.character(x$Effects)
x$Effects[x$Effects == "fixed"] <- "Fixed Effects"
x$Effects[x$Effects == "random"] <- "Random Effects"
}

x
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-plot.estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ test_that("`plot.see_estimate_contrasts()` works", {

model <- stan_glm(Sepal.Width ~ Species, data = iris, refresh = 0)
contrasts <- modelbased::estimate_contrasts(model, contrast = "Species")
means <- modelbased::estimate_means(model, at = c("Species"))
means <- modelbased::estimate_means(model, at = "Species")
expect_s3_class(plot(contrasts, means), "gg")
})
30 changes: 18 additions & 12 deletions vignettes/parameters.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,27 @@ plot(result)

## Bayesian Meta-Analysis using brms

```{r}
# We download the model to save computation time. Here is the code
# to refit the exact model used below...
We download the model to save computation time, but here is the code to refit the exact model used below:

```{r, eval=FALSE}
# Data from
# https://github.com/MathiasHarrer/Doing-Meta-Analysis-in-R/blob/master/_data/Meta_Analysis_Data.RData
# set.seed(123)
# priors <- c(prior(normal(0,1), class = Intercept),
# prior(cauchy(0,0.5), class = sd))
#
# model <-
# brm(TE|se(seTE) ~ 1 + (1|Author),
# data = Meta_Analysis_Data,
# prior = priors,
# iter = 4000)
set.seed(123)
priors <- c(
prior(normal(0, 1), class = Intercept),
prior(cauchy(0, 0.5), class = sd)
)

model <- brm(
TE | se(seTE) ~ 1 + (1 | Author),
data = Meta_Analysis_Data,
prior = priors,
iter = 4000
)
```


```{r}
library(brms)
model <- insight::download_model("brms_meta_1")
result <- model_parameters(model)
Expand Down
Loading