diff --git a/NAMESPACE b/NAMESPACE index 0151ef477..125750df7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -112,7 +112,6 @@ S3method(ci,sim.merMod) S3method(ci,slopes) S3method(ci,stanfit) S3method(ci,stanreg) -S3method(cwi,data.frame) S3method(describe_posterior,BFBayesFactor) S3method(describe_posterior,BGGM) S3method(describe_posterior,MCMCglmm) @@ -642,7 +641,6 @@ export(contr.orthonorm) export(convert_bayesian_as_frequentist) export(convert_p_to_pd) export(convert_pd_to_p) -export(cwi) export(density_at) export(describe_posterior) export(describe_prior) diff --git a/R/bic_to_bf.R b/R/bic_to_bf.R index 306b5e750..f8e6ae832 100644 --- a/R/bic_to_bf.R +++ b/R/bic_to_bf.R @@ -27,8 +27,8 @@ bic_to_bf <- function(bic, denominator, log = FALSE) { delta <- (denominator - bic) / 2 if (log) { - return(delta) + delta } else { - return(exp(delta)) + exp(delta) } } diff --git a/R/describe_prior.R b/R/describe_prior.R index 7f94a68ea..54b140c83 100644 --- a/R/describe_prior.R +++ b/R/describe_prior.R @@ -63,7 +63,7 @@ describe_prior.brmsfit <- function(model, # If the prior scale has been adjusted, it is the actual scale that was used. if ("Prior_Adjusted_Scale" %in% names(priors)) { - priors$Prior_Scale[!is.na(priors$Prior_Adjusted_Scale)] <- priors$Prior_Adjusted_Scale[!is.na(priors$Prior_Adjusted_Scale)] + priors$Prior_Scale[!is.na(priors$Prior_Adjusted_Scale)] <- priors$Prior_Adjusted_Scale[!is.na(priors$Prior_Adjusted_Scale)] # nolint priors$Prior_Adjusted_Scale <- NULL } @@ -85,7 +85,7 @@ describe_prior.brmsfit <- function(model, colnames(priors)[1] <- "Cleaned_Parameter" out <- merge(cp, priors, by = "Cleaned_Parameter", all = TRUE) out <- out[!duplicated(out$Parameter), ] - priors <- out[intersect(colnames(out), c("Parameter", "Prior_Distribution", "Prior_df", "Prior_Location", "Prior_Scale", "Response"))] + priors <- out[intersect(colnames(out), c("Parameter", "Prior_Distribution", "Prior_df", "Prior_Location", "Prior_Scale", "Response"))] # nolint } priors diff --git a/R/diagnostic_draws.R b/R/diagnostic_draws.R index f1b1ddc48..30858f453 100644 --- a/R/diagnostic_draws.R +++ b/R/diagnostic_draws.R @@ -27,17 +27,26 @@ diagnostic_draws <- function(posterior, ...) { diagnostic_draws.brmsfit <- function(posterior, ...) { insight::check_if_installed("brms") - data <- brms::nuts_params(posterior) - data$idvar <- paste0(data$Chain, "_", data$Iteration) + nuts_parameters <- brms::nuts_params(posterior) + nuts_parameters$idvar <- paste0( + nuts_parameters$Chain, + "_", + nuts_parameters$Iteration + ) out <- stats::reshape( - data, + nuts_parameters, v.names = "Value", idvar = "idvar", timevar = "Parameter", direction = "wide" ) out$idvar <- NULL - out <- merge(out, brms::log_posterior(posterior), by = c("Chain", "Iteration"), sort = FALSE) + out <- merge( + out, + brms::log_posterior(posterior), + by = c("Chain", "Iteration"), + sort = FALSE + ) # Rename names(out)[names(out) == "Value.accept_stat__"] <- "Acceptance_Rate" diff --git a/R/distribution.R b/R/distribution.R index 97c347ddd..d89e05548 100644 --- a/R/distribution.R +++ b/R/distribution.R @@ -33,21 +33,21 @@ distribution <- function(type = "normal", ...) { ) switch(match.arg(arg = type, choices = basr_r_distributions), - "beta" = distribution_beta(...), - "binom" = , - "binomial" = distribution_binomial(...), - "cauchy" = distribution_cauchy(...), - "chisq" = , - "chisquared" = distribution_chisquared(...), - "gamma" = distribution_gamma(...), - "gaussian" = , - "normal" = distribution_normal(...), - "nbinom" = distribution_nbinom(...), - "poisson" = distribution_poisson(...), - "t" = , - "student" = , - "student_t" = distribution_student(...), - "uniform" = distribution_uniform(...), + beta = distribution_beta(...), + binom = , + binomial = distribution_binomial(...), + cauchy = distribution_cauchy(...), + chisq = , + chisquared = distribution_chisquared(...), + gamma = distribution_gamma(...), + gaussian = , + normal = distribution_normal(...), + nbinom = distribution_nbinom(...), + poisson = distribution_poisson(...), + t = , + student = , + student_t = distribution_student(...), + uniform = distribution_uniform(...), distribution_custom(type = type, ...) ) } @@ -148,7 +148,7 @@ distribution_mixture_normal <- function(n, mean = c(-3, 3), sd = 1, random = FAL n <- round(n / length(mean)) sd <- sd if (length(sd) != length(mean)) { - sd <- rep(sd, length.out = length(mean)) + sd <- rep_len(sd, length(mean)) } diff --git a/R/effective_sample.R b/R/effective_sample.R index fa81bd5d5..0b5c6df7c 100644 --- a/R/effective_sample.R +++ b/R/effective_sample.R @@ -16,8 +16,10 @@ #' information there is in autocorrelated chains} (*Kruschke 2015, p182-3*). #' #' @references -#' - Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Academic Press. -#' - Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models using Stan. Journal of Statistical Software, 80(1), 1-28 +#' - Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, +#' and Stan. Academic Press. +#' - Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models +#' using Stan. Journal of Statistical Software, 80(1), 1-28 #' #' @examplesIf require("rstanarm") #' \donttest{ @@ -82,7 +84,7 @@ effective_sample.brmsfit <- function(model, #' @export effective_sample.stanreg <- function(model, effects = c("fixed", "random", "all"), - component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), + component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), # nolint parameters = NULL, ...) { # check arguments @@ -112,7 +114,7 @@ effective_sample.stanreg <- function(model, #' @export effective_sample.stanmvreg <- function(model, effects = c("fixed", "random", "all"), - component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), + component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), # nolint parameters = NULL, ...) { # check arguments diff --git a/R/hdi.R b/R/hdi.R index 0662a2c52..fa31b23bd 100644 --- a/R/hdi.R +++ b/R/hdi.R @@ -82,7 +82,8 @@ #' @inherit ci return #' #' @family ci -#' @seealso Other interval functions, such as [hdi()], [eti()], [bci()], [spi()], [si()], [cwi()]. +#' @seealso Other interval functions, such as [`hdi()`], [`eti()`], [`bci()`], +#' [`spi()`], [`si()`]. #' #' @examplesIf require("rstanarm") && require("brms") && require("emmeans") && require("BayesFactor") #' library(bayestestR) diff --git a/R/model_to_priors.R b/R/model_to_priors.R index 29634e547..20dfb9d1a 100644 --- a/R/model_to_priors.R +++ b/R/model_to_priors.R @@ -35,12 +35,12 @@ model_to_priors.brmsfit <- function(model, scale_multiply = 3, ...) { for (p in priors_params$Parameter) { if (p %in% params$Parameter) { - subset <- params[params$Parameter == p, ] + param_subset <- params[params$Parameter == p, ] priors$prior[priors_params$Parameter == p] <- paste0( "normal(", - insight::format_value(subset$Mean), + insight::format_value(param_subset$Mean), ", ", - insight::format_value(subset$SD * scale_multiply), + insight::format_value(param_subset$SD * scale_multiply), ")" ) } diff --git a/R/overlap.R b/R/overlap.R index 36faf6899..8fdb29737 100644 --- a/R/overlap.R +++ b/R/overlap.R @@ -1,6 +1,8 @@ #' Overlap Coefficient #' -#' A method to calculate the overlap coefficient between two empirical distributions (that can be used as a measure of similarity between two samples). +#' A method to calculate the overlap coefficient between two empirical +#' distributions (that can be used as a measure of similarity between two +#' samples). #' #' @param x Vector of x values. #' @param y Vector of x values. @@ -17,32 +19,56 @@ #' overlap(x, y) #' plot(overlap(x, y)) #' @export -overlap <- function(x, y, method_density = "kernel", method_auc = "trapezoid", precision = 2^10, extend = TRUE, extend_scale = 0.1, ...) { +overlap <- function(x, + y, + method_density = "kernel", + method_auc = "trapezoid", + precision = 2^10, + extend = TRUE, + extend_scale = 0.1, + ...) { # Generate densities - dx <- estimate_density(x, method = method_density, precision = precision, extend = extend, extend_scale = extend_scale, ...) - dy <- estimate_density(y, method = method_density, precision = precision, extend = extend, extend_scale = extend_scale, ...) + dx <- estimate_density( + x, + method = method_density, + precision = precision, + extend = extend, + extend_scale = extend_scale, + ... + ) + dy <- estimate_density( + y, + method = method_density, + precision = precision, + extend = extend, + extend_scale = extend_scale, + ... + ) # Create density estimation functions fx <- stats::approxfun(dx$x, dx$y, method = "linear", rule = 2) fy <- stats::approxfun(dy$x, dy$y, method = "linear", rule = 2) x_axis <- seq(min(c(dx$x, dy$x)), max(c(dx$x, dy$x)), length.out = precision) - data <- data.frame(x = x_axis, y1 = fx(x_axis), y2 = fy(x_axis)) - + approx_data <- data.frame(x = x_axis, y1 = fx(x_axis), y2 = fy(x_axis)) # calculate intersection densities - data$intersection <- pmin(data$y1, data$y2) - data$exclusion <- pmax(data$y1, data$y2) + approx_data$intersection <- pmin(approx_data$y1, approx_data$y2) + approx_data$exclusion <- pmax(approx_data$y1, approx_data$y2) # integrate areas under curves - area_intersection <- area_under_curve(data$x, data$intersection, method = method_auc) + area_intersection <- area_under_curve( + approx_data$x, + approx_data$intersection, + method = method_auc + ) # area_exclusion <- area_under_curve(data$x, data$exclusion, method = method_auc) # compute overlap coefficient overlap <- area_intersection - attr(overlap, "data") <- data + attr(overlap, "data") <- approx_data class(overlap) <- c("overlap", class(overlap)) overlap @@ -59,7 +85,7 @@ print.overlap <- function(x, ...) { #' @export plot.overlap <- function(x, ...) { # Can be improved through see - data <- attributes(x)$data - graphics::plot(data$x, data$exclusion, type = "l") - graphics::polygon(data$x, data$intersection, col = "red") + plot_data <- attributes(x)$data + graphics::plot(plot_data$x, plot_data$exclusion, type = "l") + graphics::polygon(plot_data$x, plot_data$intersection, col = "red") } diff --git a/R/p_significance.R b/R/p_significance.R index 12a692c1b..c38a3842a 100644 --- a/R/p_significance.R +++ b/R/p_significance.R @@ -285,7 +285,7 @@ p_significance.predictions <- p_significance.slopes p_significance.stanreg <- function(x, threshold = "default", effects = c("fixed", "random", "all"), - component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), + component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), # nolint parameters = NULL, verbose = TRUE, ...) { diff --git a/R/cwi.R b/WIP/cwi.R similarity index 100% rename from R/cwi.R rename to WIP/cwi.R diff --git a/man/bayestestR-package.Rd b/man/bayestestR-package.Rd index 3552e5bdb..c61f4e45c 100644 --- a/man/bayestestR-package.Rd +++ b/man/bayestestR-package.Rd @@ -33,15 +33,15 @@ Useful links: } \author{ -\strong{Maintainer}: Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) (@Dom_Makowski) +\strong{Maintainer}: Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) Authors: \itemize{ - \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) (@strengejacke) - \item Mattan S. Ben-Shachar \email{matanshm@post.bgu.ac.il} (\href{https://orcid.org/0000-0002-4287-4801}{ORCID}) (@mattansb) - \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) (@patilindrajeets) + \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) + \item Mattan S. Ben-Shachar \email{matanshm@post.bgu.ac.il} (\href{https://orcid.org/0000-0002-4287-4801}{ORCID}) + \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) \item Micah K. Wilson \email{micah.k.wilson@curtin.edu.au} (\href{https://orcid.org/0000-0003-4143-7308}{ORCID}) - \item Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) (@bmwiernik) + \item Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) } Other contributors: diff --git a/man/bci.Rd b/man/bci.Rd index 172d51655..3e2f66a42 100644 --- a/man/bci.Rd +++ b/man/bci.Rd @@ -174,7 +174,6 @@ Statistical Science. 11(3): 189–212. 10.1214/ss/1032280214 \seealso{ Other ci: \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{si}()}, diff --git a/man/ci.Rd b/man/ci.Rd index 1ef0d7948..92c7a9ae4 100644 --- a/man/ci.Rd +++ b/man/ci.Rd @@ -155,7 +155,6 @@ intervals"? BMJ 2019;l5381. 10.1136/bmj.l5381 \seealso{ Other ci: \code{\link{bci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{si}()}, diff --git a/man/cwi.Rd b/man/cwi.Rd deleted file mode 100644 index 6b3db0ae0..000000000 --- a/man/cwi.Rd +++ /dev/null @@ -1,95 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/cwi.R -\name{cwi} -\alias{cwi} -\alias{cwi.data.frame} -\title{Curvewise Intervals (CWI)} -\usage{ -cwi(x, ...) - -\method{cwi}{data.frame}(x, ci = 0.95, ...) -} -\arguments{ -\item{x}{Vector representing a posterior distribution, or a data frame of such -vectors. Can also be a Bayesian model. \strong{bayestestR} supports a wide range -of models (see, for example, \code{methods("hdi")}) and not all of those are -documented in the 'Usage' section, because methods for other classes mostly -resemble the arguments of the \code{.numeric} or \code{.data.frame}methods.} - -\item{...}{Currently not used.} - -\item{ci}{Value or vector of probability of the (credible) interval - CI -(between 0 and 1) to be estimated. Default to \code{.95} (\verb{95\%}).} -} -\value{ -A data frame with following columns: -\itemize{ -\item \code{Parameter} The model parameter(s), if \code{x} is a model-object. If \code{x} is a -vector, this column is missing. -\item \code{CI} The probability of the credible interval. -\item \code{CI_low}, \code{CI_high} The lower and upper credible interval limits for the parameters. -} -} -\description{ -Compute the \strong{Curvewise interval (CWI)} (also called the "simultaneous interval" or "joint interval") of posterior distributions using \code{ggdist::curve_interval()}. -Whereas the more typical "pointwise intervals" contain xx\% of the posterior for a single parameter, -joint/curvewise intervals contain xx\% of the posterior distribution for \strong{all} parameters. -} -\details{ -Applied model predictions, pointwise intervals contain xx\% of the predicted response values \strong{conditional} on specific predictor values. -In contrast, curvewise intervals contain xx\% of the predicted response values across all predictor values. -Put another way, curvewise intervals contain xx\% of the full \strong{prediction lines} from the model. - -For more details, see the \href{https://mjskay.github.io/ggdist/articles/lineribbon.html#curve-boxplots-aka-lineribbons-with-joint-intervals-or-curvewise-intervals-}{\emph{ggdist} documentation on curvewise intervals}. -} -\examples{ -\donttest{ -library(bayestestR) - -if (require("ggplot2") && require("rstanarm") && require("ggdist")) { - # Generate data ============================================= - k <- 11 # number of curves (iterations) - n <- 201 # number of rows - data <- data.frame(x = seq(-15, 15, length.out = n)) - - # Simulate iterations as new columns - for (i in 1:k) { - data[paste0("iter_", i)] <- dnorm(data$x, seq(-5, 5, length.out = k)[i], 3) - } - - # Note: first, we need to transpose the data to have iters as rows - iters <- datawizard::data_transpose(data[paste0("iter_", 1:k)]) - - # Compute Median - data$Median <- point_estimate(iters)[["Median"]] - - # Compute Credible Intervals ================================ - - # Compute ETI (default type of CI) - data[c("ETI_low", "ETI_high")] <- eti(iters, ci = 0.5)[c("CI_low", "CI_high")] - - # Compute CWI - # ggdist::curve_interval(reshape_iterations(data), iter_value .width = 0.5) - - # Visualization ============================================= - ggplot(data, aes(x = x, y = Median)) + - geom_ribbon(aes(ymin = ETI_low, ymax = ETI_high), fill = "red", alpha = 0.3) + - geom_line(linewidth = 1) + - geom_line( - data = reshape_iterations(data), - aes(y = iter_value, group = iter_group), - alpha = 0.3 - ) -} -} -} -\seealso{ -Other ci: -\code{\link{bci}()}, -\code{\link{ci}()}, -\code{\link{eti}()}, -\code{\link{hdi}()}, -\code{\link{si}()}, -\code{\link{spi}()} -} -\concept{ci} diff --git a/man/effective_sample.Rd b/man/effective_sample.Rd index 50c8aac01..7ca6d5745 100644 --- a/man/effective_sample.Rd +++ b/man/effective_sample.Rd @@ -70,7 +70,9 @@ effective_sample(model) } \references{ \itemize{ -\item Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Academic Press. -\item Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models using Stan. Journal of Statistical Software, 80(1), 1-28 +\item Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, +and Stan. Academic Press. +\item Bürkner, P. C. (2017). brms: An R package for Bayesian multilevel models +using Stan. Journal of Statistical Software, 80(1), 1-28 } } diff --git a/man/eti.Rd b/man/eti.Rd index 4b116beb7..b81a35a60 100644 --- a/man/eti.Rd +++ b/man/eti.Rd @@ -171,7 +171,6 @@ eti(bf, ci = c(0.80, 0.89, 0.95)) Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{hdi}()}, \code{\link{si}()}, \code{\link{spi}()} diff --git a/man/hdi.Rd b/man/hdi.Rd index 927d4f3da..62ffc2e67 100644 --- a/man/hdi.Rd +++ b/man/hdi.Rd @@ -177,12 +177,12 @@ examples in R and Stan. Chapman and Hall/CRC. } } \seealso{ -Other interval functions, such as \code{\link[=hdi]{hdi()}}, \code{\link[=eti]{eti()}}, \code{\link[=bci]{bci()}}, \code{\link[=spi]{spi()}}, \code{\link[=si]{si()}}, \code{\link[=cwi]{cwi()}}. +Other interval functions, such as \code{\link[=hdi]{hdi()}}, \code{\link[=eti]{eti()}}, \code{\link[=bci]{bci()}}, +\code{\link[=spi]{spi()}}, \code{\link[=si]{si()}}. Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{si}()}, \code{\link{spi}()} diff --git a/man/overlap.Rd b/man/overlap.Rd index d3a170c29..86448f776 100644 --- a/man/overlap.Rd +++ b/man/overlap.Rd @@ -34,7 +34,9 @@ means that the x axis will be extended by \code{1/10} of the range of the data.} \item{...}{Currently not used.} } \description{ -A method to calculate the overlap coefficient between two empirical distributions (that can be used as a measure of similarity between two samples). +A method to calculate the overlap coefficient between two empirical +distributions (that can be used as a measure of similarity between two +samples). } \examples{ library(bayestestR) diff --git a/man/si.Rd b/man/si.Rd index 0ca1492af..a066e987c 100644 --- a/man/si.Rd +++ b/man/si.Rd @@ -226,7 +226,6 @@ The Support Interval. \doi{10.31234/osf.io/zwnxb} Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{spi}()} diff --git a/man/spi.Rd b/man/spi.Rd index 6212d9343..b1bf0b560 100644 --- a/man/spi.Rd +++ b/man/spi.Rd @@ -127,7 +127,6 @@ Liu, Y., Gelman, A., & Zheng, T. (2015). Simulation-efficient shortest probabili Other ci: \code{\link{bci}()}, \code{\link{ci}()}, -\code{\link{cwi}()}, \code{\link{eti}()}, \code{\link{hdi}()}, \code{\link{si}()}