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

dev #176

Merged
merged 9 commits into from
Jun 18, 2019
Merged

dev #176

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
8 changes: 3 additions & 5 deletions R/bayesfactor_inclusion.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#'
#' BFmodels <- bayesfactor_models(mo1, mo2, mo3, denominator = mo0)
#' bayesfactor_inclusion(BFmodels)
#'
#' \dontrun{
#' # BayesFactor
#' # -------------------------------
Expand Down Expand Up @@ -71,7 +70,7 @@ bayesfactor_inclusion.bayesfactor_models <- function(models, match_models = FALS

# Build Interaction Matrix #
if (isTRUE(match_models)) {
effects.matrix <- as.matrix(df.model[,-c(1:3)])
effects.matrix <- as.matrix(df.model[, -c(1:3)])

df.interaction <- data.frame(effnames, stringsAsFactors = FALSE)

Expand All @@ -80,7 +79,6 @@ bayesfactor_inclusion.bayesfactor_models <- function(models, match_models = FALS
}
rownames(df.interaction) <- effnames
df.interaction <- as.matrix(df.interaction[, -1])

}

# Build Effect Table #
Expand All @@ -105,7 +103,7 @@ bayesfactor_inclusion.bayesfactor_models <- function(models, match_models = FALS
has_not_high_order_interactions &
!effects.matrix[, eff]

df.model_temp <- df.model[ind_include | ind_exclude, ,drop = FALSE]
df.model_temp <- df.model[ind_include | ind_exclude, , drop = FALSE]
} else {
df.model_temp <- df.model
}
Expand Down Expand Up @@ -236,4 +234,4 @@ bayesfactor_inclusion.BFBayesFactor <- function(models, match_models = FALSE, pr
}

temp
}
}
28 changes: 16 additions & 12 deletions R/bayesfactor_savagedickey.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#'
#' This method computes the ratio between the density of a single value (typically the null)
#' of two distributions. When the compared distributions are the posterior and the prior distributions,
#' this results in an approximation of a Bayes factor against the (point) null model.
#' this results is an approximation of a Bayes factor comparing the model against a model in which
#' the parameter of choice is restricted to the point null.
#' \cr \cr
#' See also \href{https://easystats.github.io/bayestestR/articles/bayes_factors.html}{the Bayes factors vignette}.
#'
Expand Down Expand Up @@ -58,7 +59,7 @@
#' # emmGrid objects
#' # ---------------
#' library(emmeans)
#' group_diff <- pairs(emmeans(stan_model, ~ group))
#' group_diff <- pairs(emmeans(stan_model, ~group))
#' bayesfactor_savagedickey(group_diff, prior = stan_model)
#'
#' # brms models
Expand Down Expand Up @@ -243,7 +244,7 @@ bayesfactor_savagedickey.data.frame <- function(posterior, prior = NULL,
#' @keywords internal
#' @importFrom insight print_color
.bayesfactor_savagedickey <- function(posterior, prior, direction = 0, hypothesis = 0) {
if (isTRUE(all.equal(posterior,prior))) {
if (isTRUE(all.equal(posterior, prior))) {
return(1)
}

Expand Down Expand Up @@ -294,7 +295,7 @@ bayesfactor_savagedickey.data.frame <- function(posterior, prior = NULL,
}

String <- c("left", "right", "one-sided", "onesided", "two-sided", "twosided", "<", ">", "=", "-1", "0", "1", "+1")
Value <- c(-1, 1, 1, 1, 0, 0, -1, 1, 0, -1, 0, 1, 1)
Value <- c(-1, 1, 1, 1, 0, 0, -1, 1, 0, -1, 0, 1, 1)

ind <- String == direction
if (length(ind) == 0) {
Expand Down Expand Up @@ -329,8 +330,10 @@ bayesfactor_savagedickey.data.frame <- function(posterior, prior = NULL,

x_range <- range(x)
x_rangex <- stats::median(x) + 7 * stats::mad(x) * c(-1, 1)
x_range <- c(max(c(x_range[1],x_rangex[1])),
min(c(x_range[2],x_rangex[2])))
x_range <- c(
max(c(x_range[1], x_rangex[1])),
min(c(x_range[2], x_rangex[2]))
)

extension_scale <- diff(x_range) * extend_scale
x_range[1] <- x_range[1] - extension_scale
Expand Down Expand Up @@ -396,7 +399,7 @@ bayesfactor_savagedickey.data.frame <- function(posterior, prior = NULL,


#' @keywords internal
.update_to_priors <- function(model, verbose = TRUE){
.update_to_priors <- function(model, verbose = TRUE) {
UseMethod(".update_to_priors")
}

Expand All @@ -405,7 +408,7 @@ bayesfactor_savagedickey.data.frame <- function(posterior, prior = NULL,
#' @keywords internal
#' @importFrom stats update
#' @importFrom utils capture.output
.update_to_priors.stanreg <- function(model, verbose = TRUE){
.update_to_priors.stanreg <- function(model, verbose = TRUE) {
if (!requireNamespace("rstanarm")) {
stop("Package \"rstanarm\" needed for this function to work. Please install it.")
}
Expand All @@ -429,7 +432,7 @@ bayesfactor_savagedickey.data.frame <- function(posterior, prior = NULL,
#' @importFrom stats update
#' @importFrom utils capture.output
#' @importFrom methods is
.update_to_priors.brmsfit <- function(model, verbose = TRUE){
.update_to_priors.brmsfit <- function(model, verbose = TRUE) {
if (!requireNamespace("brms")) {
stop("Package \"brms\" needed for this function to work. Please install it.")
}
Expand All @@ -447,12 +450,13 @@ bayesfactor_savagedickey.data.frame <- function(posterior, prior = NULL,
if (is(model_prior, "try-error")) {
if (grepl("proper priors", model_prior)) {
stop("Cannot compute BF for 'brmsfit' models fit with default priors.\n",
"See '?bayesfactor_savagedickey'",
call. = FALSE)
"See '?bayesfactor_savagedickey'",
call. = FALSE
)
} else {
stop(model_prior)
}
}

model_prior
}
}
3 changes: 1 addition & 2 deletions R/ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
#' ci(model, ci = c(.80, .89, .95))
#'
#' library(emmeans)
#' ci(emtrends(model, ~ 1, "wt"))
#'
#' ci(emtrends(model, ~1, "wt"))
#' \dontrun{
#' library(brms)
#' model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
Expand Down
9 changes: 4 additions & 5 deletions R/convert_pd_to_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#' @examples
#' pd_to_p(pd = 95)
#' pd_to_p(pd = 95, direction = "one-sided")
#'
#' @export
pd_to_p <- function(pd, direction = "two-sided", ...) {
direction <- .get_direction(direction)
p <- (1 - pd/100)
if(direction == 0){
p <- (1 - pd / 100)
if (direction == 0) {
p <- 2 * p
}
p
Expand All @@ -26,10 +25,10 @@ pd_to_p <- function(pd, direction = "two-sided", ...) {
#' @export
p_to_pd <- function(p, direction = "two-sided", ...) {
direction <- .get_direction(direction)
if(direction == 0){
if (direction == 0) {
p <- p / 2
}
(1-p)*100
(1 - p) * 100
}


Expand Down
3 changes: 1 addition & 2 deletions R/describe_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#' # -----------------------------------------------
#' library(emmeans)
#' describe_posterior(emtrends(model, ~1, "wt"))
#'
#' \dontrun{
#' # brms models
#' # -----------------------------------------------
Expand Down Expand Up @@ -396,4 +395,4 @@ describe_posterior.BFBayesFactor <- function(posteriors, centrality = "median",
"rope", "equivalence", "equivalence_test", "equitest",
"bf", "bayesfactor", "bayes_factor", "p_map", "all"
), several.ok = TRUE)
}
}
2 changes: 1 addition & 1 deletion R/diagnostic_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param diagnostic Diagnostic metrics to compute. Character (vector) or list with one or more of these options: \code{"ESS"}, \code{"Rhat"}, \code{"MCSE"} or \code{"all"}.
#'
#' @details
#' \strong{Effective Sample (ESS)} should be as large as possible, altough for most applications, an effective sample size greater than 1,000 is sufficient for stable estimates (Bürkner, 2017). The ESS corresponds to the number of independent samples with the same estimation power as the N autocorrelated samples. It is is a measure of \dQuote{how much independent information there is in autocorrelated chains} (\emph{Kruschke 2015, p182-3}).
#' \strong{Effective Sample (ESS)} should be as large as possible, altough for most applications, an effective sample size greater than 1000 is sufficient for stable estimates (Bürkner, 2017). The ESS corresponds to the number of independent samples with the same estimation power as the N autocorrelated samples. It is is a measure of \dQuote{how much independent information there is in autocorrelated chains} (\emph{Kruschke 2015, p182-3}).
#' \cr \cr
#' \strong{Rhat} should be the closest to 1. It should not be larger than 1.1 (Gelman and Rubin, 1992) or 1.01 (Vehtari et al., 2019). The split R-hat statistic quantifies the consistency of an ensemble of Markov chains.
#' \cr \cr
Expand Down
4 changes: 2 additions & 2 deletions R/distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ distribution_custom <- function(n, type = "norm", ..., random = FALSE) {
distribution_mixture_normal <- function(n, mean = c(-3, 3), sd = 1, random = FALSE, ...) {
n <- round(n / length(mean))
sd <- c(sd)
if(length(sd) != length(mean)){
if (length(sd) != length(mean)) {
sd <- rep(sd, length.out = length(mean))
}


x <- c()
for(i in 1:length(mean)){
for (i in 1:length(mean)) {
x <- c(x, distribution_normal(n = n, mean = mean[i], sd = sd[i], random = random))
}
x
Expand Down
8 changes: 5 additions & 3 deletions R/estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ estimate_density.emmGrid <- function(x, method = "kernel", precision = 2^10, ext
}
x <- as.data.frame(as.matrix(emmeans::as.mcmc.emmGrid(x, names = FALSE)))

estimate_density(x, method = method, precision = precision,
extend = extend, extend_scale = extend_scale,
bw = bw, ...)
estimate_density(x,
method = method, precision = precision,
extend = extend, extend_scale = extend_scale,
bw = bw, ...
)
}


Expand Down
2 changes: 1 addition & 1 deletion R/hdi.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ hdi.emmGrid <- function(x, ci = .89, verbose = TRUE, ...) {
stop("Package \"emmeans\" needed for this function to work. Please install it.")
}
xdf <- as.data.frame(as.matrix(emmeans::as.mcmc.emmGrid(x, names = FALSE)))
out <- hdi(xdf , ci = ci, verbose = verbose, ...)
out <- hdi(xdf, ci = ci, verbose = verbose, ...)
attr(out, "object_name") <- deparse(substitute(x), width.cutoff = 500)
out
}
Expand Down
2 changes: 1 addition & 1 deletion R/p_rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ p_rope.emmGrid <- function(x, range = "default", precision = .1, ...) {
}
xdf <- as.data.frame(as.matrix(emmeans::as.mcmc.emmGrid(x, names = FALSE)))

out <- p_rope(xdf , range = range, precision = precision, ...)
out <- p_rope(xdf, range = range, precision = precision, ...)
attr(out, "object_name") <- deparse(substitute(x), width.cutoff = 500)
out
}
Expand Down
2 changes: 1 addition & 1 deletion R/print.bayesfactor_inclusion.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ print.bayesfactor_inclusion <- function(x, digits = 2, log = FALSE, ...) {

xBF <- BFE$BF
BFE$BF <- as.character(round(xBF, digits = digits))
big_ind <- abs(xBF) >= 1000 | abs(xBF) < 1 / (10 ^ digits)
big_ind <- abs(xBF) >= 1000 | abs(xBF) < 1 / (10^digits)
big_ind <- sapply(big_ind, isTRUE)
if (isTRUE(any(big_ind))) {
BFE$BF[big_ind] <- formatC(xBF, format = "e", digits = digits)[big_ind]
Expand Down
2 changes: 1 addition & 1 deletion R/print.bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ print.bayesfactor_models <- function(x, digits = 2, log = FALSE, ...) {
}
xBF <- BFE$BF
BFE$BF <- as.character(round(xBF, digits = digits))
big_ind <- abs(xBF) >= 1000 | abs(xBF) < 1 / (10 ^ digits)
big_ind <- abs(xBF) >= 1000 | abs(xBF) < 1 / (10^digits)
big_ind <- sapply(big_ind, isTRUE)
if (isTRUE(any(big_ind))) {
BFE$BF[big_ind] <- formatC(xBF, format = "e", digits = digits)[big_ind]
Expand Down
2 changes: 1 addition & 1 deletion R/print.bayesfactor_savagedickey.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ print.bayesfactor_savagedickey <- function(x, digits = 2, log = FALSE, ...) {

xBF <- BFE$BF
BFE$BF <- as.character(round(xBF, digits = digits))
big_ind <- abs(xBF) >= 1000 | abs(xBF) < 1 / (10 ^ digits)
big_ind <- abs(xBF) >= 1000 | abs(xBF) < 1 / (10^digits)
big_ind <- sapply(big_ind, isTRUE)
if (isTRUE(any(big_ind))) {
BFE$BF[big_ind] <- formatC(xBF, format = "e", digits = digits)[big_ind]
Expand Down
1 change: 0 additions & 1 deletion R/update.bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#' m
#'
#' update(m, reference = "bottom")
#'
#' @export
update.bayesfactor_models <- function(object, subset = NULL, reference = NULL, ...) {
if (!is.null(reference)) {
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Click on the buttons above to access the package [**documentation**](https://eas

#### Articles

- [Credible Intervals (CIs)](https://easystats.github.io/bayestestR/articles/credible_interval.html)
- [Credible Intervals (CI)](https://easystats.github.io/bayestestR/articles/credible_interval.html)
- [Probability of Direction (pd)](https://easystats.github.io/bayestestR/articles/probability_of_direction.html)
- [Region of Practical Equivalence (ROPE)](https://easystats.github.io/bayestestR/articles/region_of_practical_equivalence.html)
- [Bayes Factors (BF)](https://easystats.github.io/bayestestR/articles/bayes_factors.html)
Expand Down Expand Up @@ -227,7 +227,7 @@ posterior %>%

### Bayes Factor

[**`bayesfactor_savagedickey()`**](https://easystats.github.io/bayestestR/reference/bayesfactor_savagedickey.html) computes the ratio between the density of a single value (typically the null) in two distributions, typically the posterior vs. the prior distributions. This method is used to examine if the hypothesis value is less or more likely given the observed data.
[**`bayesfactor_savagedickey()`**](https://easystats.github.io/bayestestR/reference/bayesfactor_savagedickey.html) computes the ratio between the density of a single value (typically the null) of two distributions. When the compared distributions are the posterior and the prior distributions, this results is an approximation of a Bayes factor comparing the model against a model in which the parameter of choice is restricted to the point null.


```{r message=FALSE, warning=FALSE, results='hide'}
Expand Down
8 changes: 4 additions & 4 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ navbar:
href: articles/example3.html
- text: Articles
menu:
- text: 'Credible Intervals'
- text: 'Credible Intervals (CI)'
href: articles/credible_interval.html
- text: 'Region of Practical Equivalence'
- text: 'Region of Practical Equivalence (ROPE)'
href: articles/region_of_practical_equivalence.html
- text: 'Probability of Direction'
- text: 'Probability of Direction (pd)'
href: articles/probability_of_direction.html
- text: 'Bayes Factors'
- text: 'Bayes Factors (BF)'
href: articles/bayes_factors.html
- text: 'Comparison of Point-Estimates'
href: articles/indicesEstimationComparison.html
Expand Down
8 changes: 4 additions & 4 deletions docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions docs/articles/bayes_factors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading