Skip to content

Commit

Permalink
Merge branch 'dev' into FindBestModel
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski authored Aug 27, 2018
2 parents 2410f9c + d2cbf1c commit 7ad824f
Show file tree
Hide file tree
Showing 49 changed files with 549 additions and 263 deletions.
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
Package: psycho
Type: Package
Title: Efficient and Publishing-Oriented Workflow for Psychological Science
Version: 0.3.5
Version: 0.3.6
Authors@R: c(
person("Dominique",
"Makowski",
email = "[email protected]",
role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-5375-9967")),
person("Hugo",
"Najberg",
role = "ctb"),
person("Viliam",
"Simko",
role = "ctb",
email = "[email protected]"),
role = "ctb"),
person("Sasha",
"Epskamp",
role = "rev",
Expand All @@ -26,7 +28,7 @@ Description: The main goal of the psycho package is to provide tools for psychol
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 6.1.0
Depends:
R (>= 3.4.0)
Imports:
Expand Down
12 changes: 10 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ S3method(analyze,lm)
S3method(analyze,lmerModLmerTest)
S3method(analyze,stanreg)
S3method(as.data.frame,density)
S3method(find_best_model,merModLmerTest)
S3method(find_best_model,lmerModLmerTest)
S3method(find_best_model,stanreg)
S3method(find_combinations,formula)
S3method(get_R2,glm)
Expand All @@ -22,6 +22,9 @@ S3method(get_R2,stanreg)
S3method(get_contrasts,glmerMod)
S3method(get_contrasts,lmerModLmerTest)
S3method(get_contrasts,stanreg)
S3method(get_data,lm)
S3method(get_data,merMod)
S3method(get_data,stanreg)
S3method(get_formula,glm)
S3method(get_formula,glmerMod)
S3method(get_formula,lm)
Expand Down Expand Up @@ -50,6 +53,9 @@ S3method(standardize,numeric)
S3method(standardize,stanreg)
S3method(summary,psychobject)
export(.fa_variance_text)
export(HDI)
export(HDImax)
export(HDImin)
export(R2_LOO_Adjusted)
export(R2_nakagawa)
export(R2_tjur)
Expand Down Expand Up @@ -82,7 +88,6 @@ export(get_formula)
export(get_info)
export(get_loadings_max)
export(get_predicted)
export(hdi)
export(interpret_R2)
export(interpret_R2_posterior)
export(interpret_RMSEA)
Expand Down Expand Up @@ -115,6 +120,7 @@ export(remove_empty_cols)
export(reorder_matrix)
export(rnorm_perfect)
export(rope)
export(simulate_data_regression)
export(standardize)
export(values)
import(broom)
Expand Down Expand Up @@ -173,6 +179,7 @@ importFrom(stats,dnorm)
importFrom(stats,ecdf)
importFrom(stats,family)
importFrom(stats,formula)
importFrom(stats,getCall)
importFrom(stats,hclust)
importFrom(stats,mad)
importFrom(stats,median)
Expand Down Expand Up @@ -204,5 +211,6 @@ importFrom(stringr,str_trim)
importFrom(tibble,rownames_to_column)
importFrom(utils,capture.output)
importFrom(utils,combn)
importFrom(utils,data)
importFrom(utils,head)
importFrom(utils,tail)
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Current Dev

### Breaking changes
- Changed `hdi` to `HDI`
- Removed Mean and SD from summary in `bayes_cor.test`
### New functions / parameters
- Added `simulate_data_regression`
- Extended `standardize` method
- Added `get_R2` method
- Added `interpret_odds` and logistic regression effect size interpretation
Expand Down Expand Up @@ -163,7 +165,7 @@

### Breaking changes
### New functions / parameters
- `hdi`: Compute highest density intervals
- `HDI`: Compute highest density intervals
- `format_string`: A tidyverse friendly version of `sprintf`
### Major changes
- Changed credible interval computation in analyze.stanreg
Expand Down
9 changes: 5 additions & 4 deletions R/analyze.htest.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ analyze.htest <- function(x, effsize_rules="cohen1988", ...) {

# Text
# -------------

# CORRELATION
if (grepl("correlation", values$method)) {
text <- paste0(
"The ",
Expand All @@ -68,6 +70,8 @@ analyze.htest <- function(x, effsize_rules="cohen1988", ...) {
format_p(values$p, stars = FALSE),
")."
)

# T-TEST
} else if (grepl("t-test", values$method)) {
if (names(x$null.value) == "mean") {
means <- paste0(
Expand All @@ -93,10 +97,6 @@ analyze.htest <- function(x, effsize_rules="cohen1988", ...) {
vars <- paste0(values$names, " (", means, ")")
}





values$effect <- values$effect[1] - values$effect[2]

text <- paste0(
Expand All @@ -117,6 +117,7 @@ analyze.htest <- function(x, effsize_rules="cohen1988", ...) {
format_p(values$p, stars = FALSE),
")."
)
# OTHER
} else {
stop(paste0("The ", values$method, " is not implemented yet."))
}
Expand Down
2 changes: 1 addition & 1 deletion R/analyze.lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ analyze.lm <- function(x, CI=95, effsize_rules="cohen1988", ...) {
summary$p <- summary$`Pr...t..`

# standardized coefficients
standardized <- tibble::rownames_to_column(standardize(fit, method = "refit"), "Variable")
standardized <- tibble::rownames_to_column(standardize(fit, method = "refit", data=data), "Variable")
summary <- merge(summary, standardized, by = "Variable", all.x = TRUE, sort = FALSE)
summary$Effect_Size <- c(NA, interpret_d(tail(summary$Coef_std, -1), rules = effsize_rules))

Expand Down
79 changes: 69 additions & 10 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 @@ -377,7 +383,7 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
values$mad <- mad(posterior)
values$mean <- mean(posterior)
values$sd <- sd(posterior)
values$CI_values <- hdi(posterior, prob = CI / 100)
values$CI_values <- HDI(posterior, prob = CI / 100)
values$CI_values <- c(values$CI_values$values$HDImin, values$CI_values$values$HDImax)
values$MPE <- NA
values$MPE_values <- NA
Expand Down Expand Up @@ -454,7 +460,7 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
values$mad <- mad(posterior)
values$mean <- mean(posterior)
values$sd <- sd(posterior)
values$CI_values <- hdi(posterior, prob = CI / 100)
values$CI_values <- HDI(posterior, prob = CI / 100)
values$CI_values <- c(values$CI_values$values$HDImin, values$CI_values$values$HDImax)
values$MPE <- NA
values$MPE_values <- NA
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 @@ -518,10 +535,12 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
values$mad <- mad(posterior)
values$mean <- mean(posterior)
values$sd <- sd(posterior)
values$CI_values <- hdi(posterior, prob = CI / 100)
values$CI_values <- HDI(posterior, prob = CI / 100)
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 All @@ -579,7 +638,7 @@ analyze.stanreg <- function(x, CI=90, effsize=FALSE, effsize_rules="cohen1988",
values$std_mad <- mad(posterior_std)
values$std_mean <- mean(posterior_std)
values$std_sd <- sd(posterior_std)
values$std_CI_values <- hdi(posterior_std, prob = CI / 100)
values$std_CI_values <- HDI(posterior_std, prob = CI / 100)
values$std_CI_values <- c(values$std_CI_values$values$HDImin, values$std_CI_values$values$HDImax)

if (fit$family$family == "binomial" & fit$family$link == "logit") {
Expand Down
4 changes: 2 additions & 2 deletions R/bayes_cor.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ bayes_cor.test <- function(x, y, CI=90, iterations = 10000, effsize_rules_r="coh
values$mad <- mad(posterior)
values$mean <- mean(posterior)
values$sd <- sd(posterior)
values$CI <- hdi(posterior, prob = CI / 100)$text
values$CI_values <- hdi(posterior, prob = CI / 100)
values$CI <- HDI(posterior, prob = CI / 100)$text
values$CI_values <- HDI(posterior, prob = CI / 100)
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
Expand Down
2 changes: 1 addition & 1 deletion R/crawford.test.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ crawford.test <- function(patient,

pvalues <- pvalues / 2
p <- mean(pvalues)
CI <- hdi(pvalues, prob = CI / 100)
CI <- HDI(pvalues, prob = CI / 100)
# CI_1 <- sort(pvalues)[iter * (100 - CI) / 100]


Expand Down
2 changes: 1 addition & 1 deletion R/find_best_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' documentation for your model's class:
#' \itemize{
#' \item{\link[=find_best_model.stanreg]{find_best_model.stanreg}}
#' \item{\link[=find_best_model.merModLmerTest]{find_best_model.merModLmerTest}}
#' \item{\link[=find_best_model.lmerModLmerTest]{find_best_model.lmerModLmerTest}}
#' }
#'
#' @param fit Model
Expand Down
2 changes: 1 addition & 1 deletion R/find_best_model.lmerModLmerTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ find_best_model.lmerModLmerTest <- function(fit, interaction=TRUE, fixed=NULL, .

best <- list(formula = best, by_criterion = by_criterion, table = comparison)
return(best)
}
}
Loading

0 comments on commit 7ad824f

Please sign in to comment.