Skip to content

Commit

Permalink
get analyze.stanreg to match preprint
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Aug 26, 2018
1 parent 9ee5762 commit d2cbf1c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
71 changes: 65 additions & 6 deletions R/analyze.stanreg.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#'
#' @param x A stanreg model.
#' @param CI Credible interval bounds.
#' @param index Index of effect existence to report. Can be 'overlap' or 'ROPE'.
#' @param ROPE_bounds Bounds ot the ROPE. If NULL and effsize is TRUE, than the ROPE.
#' will have default values c(-0.1, 0.1) and computed on the standardized posteriors.
#' @param effsize Compute Effect Sizes according to Cohen (1988). For linear models only.
#' @param effsize_rules Grid for effect size interpretation. See \link[=interpret_d]{interpret_d}.
#' @param ... Arguments passed to or from other methods.
Expand All @@ -15,6 +18,7 @@
#' \item{the Credible Interval (CI) (by default, the 90\% CI; see Kruschke, 2018), representing a range of possible parameter.}
#' \item{the Maximum Probability of Effect (MPE), the probability that the effect is positive or negative (depending on the median’s direction).}
#' \item{the Overlap (O), the percentage of overlap between the posterior distribution and a normal distribution of mean 0 and same SD than the posterior. Can be interpreted as the probability that a value from the posterior distribution comes from a null distribution.}
#' \item{the ROPE, the proportion of the 95\% CI of the posterior distribution that lies within the region of practical equivalence.}
#' }
#'
#' @examples
Expand Down Expand Up @@ -62,7 +66,7 @@
#' @importFrom broom tidy
#' @importFrom stringr str_squish str_replace
#' @export
analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988", ...) {
analyze.stanreg <- function(x, CI=90, index="overlap", ROPE_bounds=NULL, effsize=FALSE, effsize_rules="cohen1988", ...) {
fit <- x

# Info --------------------------------------------------------------------
Expand Down Expand Up @@ -164,7 +168,9 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
CI = CI,
effsize = effsize,
effsize_rules = effsize_rules,
fit = fit
fit = fit,
index=index,
ROPE_bounds=ROPE_bounds
)
}
}
Expand Down Expand Up @@ -507,7 +513,18 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",


#' @keywords internal
.process_effect <- function(varname, posteriors, posteriors_std, info_priors, predictors, CI=90, effsize=FALSE, effsize_rules=FALSE, fit) {
.process_effect <- function(varname,
posteriors,
posteriors_std,
info_priors,
predictors,
CI=90,
effsize=FALSE,
effsize_rules=FALSE,
fit,
index="overlap",
ROPE_bounds=NULL) {

values <- .get_info_priors(varname, info_priors, predictors)
posterior <- posteriors[, varname]

Expand All @@ -522,6 +539,8 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
values$CI_values <- c(values$CI_values$values$HDImin, values$CI_values$values$HDImax)
values$MPE <- mpe(posterior)$MPE
values$MPE_values <- mpe(posterior)$values

# Index
values$overlap <- 100 * overlap(
posterior,
rnorm_perfect(
Expand All @@ -531,6 +550,48 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
)
)

if(!is.null(ROPE_bounds)){
rope <- rope(posterior, bounds=ROPE_bounds)
values$ROPE_decision <- rope$rope_decision
values$ROPE <- rope$rope_probability
}

if(index == "overlap"){
index <- paste0("Overlap = ",
format_digit(values$overlap, null_treshold = 0.01),
"%).")
} else if(index == "ROPE"){
if(!is.null(ROPE_bounds)){
index <- paste0("ROPE = ",
format_digit(values$ROPE, null_treshold = 0.001),
").")
} else{
if(effsize == TRUE){
rope <- rope(posteriors_std[, varname], bounds=c(-0.1, 0.1))
values$ROPE_decision <- rope$rope_decision
values$ROPE <- rope$rope_probability
index <- paste0("ROPE = ",
format_digit(values$ROPE, null_treshold = 0.001),
").")
} else{
warning("you need to specify ROPE_bounds (e.g. 'c(-0.1, 0.1)'). Computing overlap instead.")
index <- paste0("Overlap = ",
format_digit(values$overlap, null_treshold = 0.01),
"%).")
}
}

} else{
warning("Parameter 'index' should be 'overlap' or 'ROPE'. Computing overlap.")
index <- paste0("Overlap = ",
format_digit(values$overlap, null_treshold = 0.01),
"%).")
}





# Text
if (grepl(":", varname)) {
splitted <- strsplit(varname, ":")[[1]]
Expand Down Expand Up @@ -564,9 +625,7 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
"% CI [",
format_digit(values$CI_values[1], null_treshold = 0.0001), ", ",
format_digit(values$CI_values[2], null_treshold = 0.0001), "], ",
"Overlap = ",
format_digit(values$overlap, null_treshold = 0.01),
"%)."
index
)


Expand Down
2 changes: 1 addition & 1 deletion R/simulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @examples
#' library(psycho)
#'
#' data <- simulate_data_regression(coefs=c(0, 0.8), sample=50, error=0)
#' data <- simulate_data_regression(coefs=c(0.1, 0.8), sample=50, error=0)
#' fit <- lm(y ~ ., data=data)
#' coef(fit)
#' analyze(fit)
Expand Down
11 changes: 9 additions & 2 deletions man/analyze.stanreg.Rd

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

2 changes: 1 addition & 1 deletion man/simulate_data_regression.Rd

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

0 comments on commit d2cbf1c

Please sign in to comment.