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

Use insight and dw remotes #648

Merged
merged 4 commits into from
May 19, 2024
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,4 @@ Config/Needs/website:
rstudio/bslib,
r-lib/pkgdown,
easystats/easystatstemplate
Remotes: easystats/insight, easystats/datawizard
14 changes: 7 additions & 7 deletions R/diagnostic_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#' @inheritParams insight::get_parameters
#' @rdname diagnostic_posterior
#' @export
diagnostic_posterior.stanreg <- function(posterior, diagnostic = "all", effects = c("fixed", "random", "all"), component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), parameters = NULL, ...) {

Check warning on line 67 in R/diagnostic_posterior.R

View workflow job for this annotation

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

file=R/diagnostic_posterior.R,line=67,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 241 characters.
# Find parameters
effects <- match.arg(effects)
component <- match.arg(component)
Expand All @@ -78,7 +78,7 @@

# If no diagnostic
if (is.null(diagnostic)) {
return(data.frame("Parameter" = params))
return(data.frame(Parameter = params))
}

diagnostic <- match.arg(diagnostic, c("ESS", "Rhat", "MCSE", "all"), several.ok = TRUE)
Expand Down Expand Up @@ -136,7 +136,7 @@

# If no diagnostic
if (is.null(diagnostic)) {
return(data.frame("Parameter" = params))
return(data.frame(Parameter = params))
}

diagnostic <- match.arg(diagnostic, c("ESS", "Rhat", "MCSE", "all"), several.ok = TRUE)
Expand Down Expand Up @@ -197,15 +197,15 @@

# If no diagnostic
if (is.null(diagnostic)) {
return(data.frame("Parameter" = params))
return(data.frame(Parameter = params))
}

# Get diagnostic
diagnostic <- match.arg(diagnostic, c("ESS", "Rhat", "MCSE", "all"), several.ok = TRUE)
if ("all" %in% diagnostic) {
diagnostic <- c("ESS", "Rhat", "MCSE", "khat") # Add MCSE
} else {
if ("Rhat" %in% diagnostic) diagnostic <- c(diagnostic, "khat")
} else if ("Rhat" %in% diagnostic) {
diagnostic <- c(diagnostic, "khat")
}

insight::check_if_installed("rstan")
Expand Down Expand Up @@ -234,14 +234,14 @@

#' @inheritParams insight::get_parameters
#' @export
diagnostic_posterior.stanfit <- function(posterior, diagnostic = "all", effects = c("fixed", "random", "all"), parameters = NULL, ...) {

Check warning on line 237 in R/diagnostic_posterior.R

View workflow job for this annotation

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

file=R/diagnostic_posterior.R,line=237,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 136 characters.
# Find parameters
effects <- match.arg(effects)
params <- insight::find_parameters(posterior, effects = effects, parameters = parameters, flatten = TRUE)

# If no diagnostic
if (is.null(diagnostic)) {
return(data.frame("Parameter" = params))
return(data.frame(Parameter = params))
}

# Get diagnostic
Expand Down Expand Up @@ -288,7 +288,7 @@
# Find parameters
params <- suppressWarnings(insight::find_parameters(posterior, flatten = TRUE))

out <- data.frame("Parameter" = params)
out <- data.frame(Parameter = params)

# If no diagnostic
if (is.null(diagnostic)) {
Expand Down
20 changes: 10 additions & 10 deletions R/estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#' See examples.
#' @param group_by Deprecated in favour of `at`.
#'
#' @note There is also a [`plot()`-method](https://easystats.github.io/see/articles/bayestestR.html) implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}.

Check warning on line 29 in R/estimate_density.R

View workflow job for this annotation

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

file=R/estimate_density.R,line=29,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 179 characters.
#'
#' @examplesIf require("logspline") && require("KernSmooth") && require("mclust") && require("emmeans") && require("rstanarm") && require("brms")

Check warning on line 31 in R/estimate_density.R

View workflow job for this annotation

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

file=R/estimate_density.R,line=31,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 145 characters.
#' library(bayestestR)
#'
#' set.seed(1)
Expand Down Expand Up @@ -258,7 +258,7 @@
} else {
# Deal with at- grouping --------

groups <- insight::get_datagrid(x[, at, drop = FALSE], at = at) # Get combinations
groups <- insight::get_datagrid(x[, at, drop = FALSE], by = at) # Get combinations
out <- data.frame()
for (row in seq_len(nrow(groups))) {
subdata <- datawizard::data_match(x, groups[row, , drop = FALSE])
Expand Down Expand Up @@ -383,7 +383,7 @@
extend_scale = 0.1,
bw = "SJ",
effects = c("fixed", "random", "all"),
component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"),

Check warning on line 386 in R/estimate_density.R

View workflow job for this annotation

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

file=R/estimate_density.R,line=386,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 141 characters.
parameters = NULL,
...) {
effects <- match.arg(effects)
Expand Down Expand Up @@ -607,8 +607,8 @@
#' density_at(posterior, c(0, 1))
#' @export
density_at <- function(posterior, x, precision = 2^10, method = "kernel", ...) {
density <- estimate_density(posterior, precision = precision, method = method, ...)
stats::approx(density$x, density$y, xout = x)$y
posterior_density <- estimate_density(posterior, precision = precision, method = method, ...)
stats::approx(posterior_density$x, posterior_density$y, xout = x)$y
}


Expand All @@ -620,30 +620,30 @@
dots[c("effects", "component", "parameters")] <- NULL

# Get the kernel density estimation (KDE)
args <- c(dots, list(
my_args <- c(dots, list(
x = x,
n = precision,
bw = bw,
from = x_range[1],
to = x_range[2]
))
fun <- get("density", asNamespace("stats"))
kde <- suppressWarnings(do.call("fun", args))
df <- as.data.frame(kde)
kde <- suppressWarnings(do.call("fun", my_args))
my_df <- as.data.frame(kde)

# Get CI (https://bookdown.org/egarpor/NP-UC3M/app-kde-ci.html)
if (!is.null(ci)) {
h <- kde$bw # Selected bandwidth
# R(K) for a normal
Rk <- 1 / (2 * sqrt(pi))
# Estimate the SD
sd_kde <- sqrt(df$y * Rk / (length(x) * h))
sd_kde <- sqrt(my_df$y * Rk / (length(x) * h))
# CI with estimated variance
z_alpha <- stats::qnorm(ci)
df$CI_low <- df$y - z_alpha * sd_kde
df$CI_high <- df$y + z_alpha * sd_kde
my_df$CI_low <- my_df$y - z_alpha * sd_kde
my_df$CI_high <- my_df$y + z_alpha * sd_kde
}
df
my_df
}


Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-check_prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test_that("check_prior - brms (linux)", {
)

# TODO: check hard-coded values
expect_warning(expect_equal(
expect_warning(expect_identical(
check_prior(model2)$Prior_Quality,
c(
"uninformative", "informative", "informative", "uninformative",
Expand All @@ -92,7 +92,7 @@ test_that("check_prior - brms (linux)", {
expect_warning(expect_identical(
check_prior(model2, method = "lakeland")$Prior_Quality,
c(
"informative", "informative", "informative", "informative",
"informative", "misinformative", "informative", "informative",
"informative", "not determinable", "not determinable", "not determinable"
)
))
Expand Down
6 changes: 3 additions & 3 deletions vignettes/bayes_factors.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
labs(x = "Drug effect", y = "Density") +
coord_cartesian(ylim = c(0, 0.45)) +
theme_modern() +
theme(legend.position = c(0.2, 0.8))
theme(legend.position.inside = c(0.2, 0.8))

pnull <- diff(pnorm(null, sd = 3))
prior_odds <- (1 - pnull) / pnull
Expand Down Expand Up @@ -249,7 +249,7 @@
labs(x = "Drug effect", y = "Density") +
coord_cartesian(ylim = c(0, 0.45)) +
theme_modern() +
theme(legend.position = c(0.2, 0.8))
theme(legend.position.inside = c(0.2, 0.8))

My_first_BF <- bayesfactor_parameters(model, null = c(-1, 1), prior = model_prior)

Expand Down Expand Up @@ -721,7 +721,7 @@
ggplot(iris, aes(Petal.Length, Sepal.Length, color = Species)) +
geom_point() +
scale_color_flat() +
theme(legend.position = c(0.2, 0.8))
theme(legend.position.inside = c(0.2, 0.8))
```

Note that these Bayes factors compare the restricted model to the unrestricted
Expand Down Expand Up @@ -972,7 +972,7 @@
```{r eval=FALSE}
library(emmeans)

(group_diff <- emmeans(model, pairwise ~ group, data = sleep))

Check warning on line 975 in vignettes/bayes_factors.Rmd

View workflow job for this annotation

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

file=vignettes/bayes_factors.Rmd,line=975,col=2,[implicit_assignment_linter] Call print() explicitly instead of relying on implicit printing behavior via '('.

# pass the original model via prior
bayesfactor_parameters(group_diff, prior = model)
Expand All @@ -980,7 +980,7 @@
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(emmeans)

suppressWarnings((group_diff <- emmeans(model, pairwise ~ group, data = sleep)))

Check warning on line 983 in vignettes/bayes_factors.Rmd

View workflow job for this annotation

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

file=vignettes/bayes_factors.Rmd,line=983,col=19,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.

# pass the original model via prior
suppressWarnings(bayesfactor_parameters(group_diff, prior = model))
Expand Down Expand Up @@ -1048,7 +1048,7 @@
```

```{r eval=FALSE}
(pairs_sum <- pairs(emmeans(fit_sum, ~Species)))

Check warning on line 1051 in vignettes/bayes_factors.Rmd

View workflow job for this annotation

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

file=vignettes/bayes_factors.Rmd,line=1051,col=2,[implicit_assignment_linter] Call print() explicitly instead of relying on implicit printing behavior via '('.

ggplot(stack(insight::get_parameters(pairs_sum)), aes(x = values, fill = ind)) +
geom_density(linewidth = 1) +
Expand All @@ -1057,7 +1057,7 @@
theme(legend.position = "none")
```
```{r echo=FALSE, warning=FALSE, message=FALSE}
(pairs_sum <- suppressWarnings(pairs(emmeans(fit_sum, ~Species, data = iris))))

Check warning on line 1060 in vignettes/bayes_factors.Rmd

View workflow job for this annotation

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

file=vignettes/bayes_factors.Rmd,line=1060,col=2,[implicit_assignment_linter] Call print() explicitly instead of relying on implicit printing behavior via '('.

ggplot(stack(insight::get_parameters(pairs_sum)), aes(x = values, fill = ind)) +
geom_density(linewidth = 1) +
Expand Down Expand Up @@ -1112,7 +1112,7 @@


```{r eval=FALSE}
(pairs_bayes <- pairs(emmeans(fit_bayes, ~Species)))

Check warning on line 1115 in vignettes/bayes_factors.Rmd

View workflow job for this annotation

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

file=vignettes/bayes_factors.Rmd,line=1115,col=2,[implicit_assignment_linter] Call print() explicitly instead of relying on implicit printing behavior via '('.

ggplot(stack(insight::get_parameters(pairs_bayes)), aes(x = values, fill = ind)) +
geom_density(linewidth = 1) +
Expand Down
Loading