Skip to content

Commit

Permalink
made the prior type more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Feb 11, 2021
1 parent 7c34fa2 commit 5f01107
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 57 deletions.
48 changes: 46 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
Meta
doc
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
# =========================
# Operating System Files
# OSX
.DS_Store
.AppleDouble
.LSOverride
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# R Studio files
.Rproj.user
.Rhistory
.RData
.Ruserdata
inst/doc
doc
Meta
.httr-oauth
revdep/checks
revdep/library
revdep/checks.noindex
revdep/library.noindex
revdep/data.sqlite
2 changes: 1 addition & 1 deletion API
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ expr_meta_random(data, type = "parametric", random = "mixture", k = 2L, conf.lev
expr_oneway_anova(data, x, y, subject.id = NULL, type = "parametric", paired = FALSE, k = 2L, conf.level = 0.95, effsize.type = "omega", var.equal = FALSE, bf.prior = 0.707, tr = 0.2, nboot = 100, top.text = NULL, output = "expression", ...)
expr_t_onesample(data, x, type = "parametric", test.value = 0, k = 2L, conf.level = 0.95, tr = 0.2, bf.prior = 0.707, effsize.type = "g", nboot = 100L, top.text = NULL, output = "expression", ...)
expr_t_twosample(data, x, y, subject.id = NULL, type = "parametric", paired = FALSE, k = 2L, conf.level = 0.95, effsize.type = "g", var.equal = FALSE, bf.prior = 0.707, tr = 0.2, nboot = 100, top.text = NULL, output = "expression", ...)
expr_template(data, no.parameters = 0L, bayesian = FALSE, statistic.text = NULL, effsize.text = NULL, top.text = NULL, prior.text = NULL, n = NULL, n.text = NULL, paired = FALSE, centrality = "median", conf.method = "HDI", k = 2L, k.df = 0L, k.df.error = 0L, ...)
expr_template(data, no.parameters = 0L, bayesian = FALSE, statistic.text = NULL, effsize.text = NULL, top.text = NULL, prior.distribution = NULL, prior.type = NULL, n = NULL, n.text = NULL, paired = FALSE, conf.method = "HDI", k = 2L, k.df = 0L, k.df.error = 0L, ...)
tidy_model_effectsize(data)
tidy_model_parameters(model, ...)

Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Depends:
Imports:
afex,
BayesFactor,
correlation (>= 0.5.0.1),
correlation (>= 0.5.1),
dplyr,
effectsize (>= 0.4.3),
insight (>= 0.12.0.1),
Expand Down
40 changes: 28 additions & 12 deletions R/helpers_expr_templates.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#' Ideally, this dataframe would come from having run `tidy_model_parameters`
#' function on your model object.
#'
#' This function is currently **not** stable and should not be used outside of
#' this package context.
#'
#' @param no.parameters An integer that specifies that the number of parameters
#' for the statistical test. Can be `0` for non-parametric tests, `1` for
#' tests based on *t*-statistic or chi-squared statistic, `2` for tests based
Expand Down Expand Up @@ -44,13 +47,11 @@
#' what the `n` stands for. If `NULL`, defaults to
#' `quote(italic("n")["pairs"])` if `paired = TRUE`, and to
#' `quote(italic("n")["obs"])` if `paired = FALSE`.
#' @param prior.text A character that specifies the prior type.
#' @param prior.distribution A character that specifies the prior type.
#' @param effsize.text A character that specifies the relevant effect size.
#' @param bayesian Is this Bayesian analysis? Defaults to `FALSE`. The template
#' is slightly different for Bayesian analysis.
#' @param centrality The point-estimates (centrality indices) to compute.
#' Character (vector) or list with one or more of these options: `"median"`,
#' `"mean"`, `"MAP"` or `"all"`.
#' @param prior.type The type of prior.
#' @param conf.method The type of index used for Credible Interval. Can be
#' `"hdi"` (default), `"eti"`, or `"si"` (see `si()`, `hdi()`, `eti()`
#' functions from `bayestestR` package).
Expand Down Expand Up @@ -96,11 +97,11 @@ expr_template <- function(data,
statistic.text = NULL,
effsize.text = NULL,
top.text = NULL,
prior.text = NULL,
prior.distribution = NULL,
prior.type = NULL,
n = NULL,
n.text = NULL,
paired = FALSE,
centrality = "median",
conf.method = "HDI",
k = 2L,
k.df = 0L,
Expand Down Expand Up @@ -130,35 +131,37 @@ expr_template <- function(data,
if (isFALSE(paired) && is.null(n.text)) n.text <- quote(italic("n")["obs"])
if (is.null(statistic.text)) statistic.text <- stat_text_switch(data$method[[1]])
if (is.null(effsize.text)) effsize.text <- estimate_type_switch(data$effectsize[[1]])
if (is.null(prior.text) && bayesian) prior.text <- prior_type_switch(data$method[[1]])
if ("conf.level" %in% names(data)) conf.level <- data$conf.level[[1]] else conf.level <- 0.95

# -------------------------- Bayesian analysis ------------------------------

if (isTRUE(bayesian)) {
if (is.null(prior.distribution)) prior.distribution <- prior_switch(data$method[[1]])
if (is.null(prior.type)) prior.type <- prior_type_switch(data$method[[1]])

# Bayesian expression
expression <-
substitute(
atop(
displaystyle(top.text),
expr = paste(
"log"["e"] * "(BF"["01"] * ") = " * bf * ", ",
widehat(effsize.text)[centrality]^"posterior" * " = " * estimate * ", ",
widehat(effsize.text)[prior.type]^"posterior" * " = " * estimate * ", ",
"CI"[conf.level]^conf.method * " [" * estimate.LB * ", " * estimate.UB * "], ",
prior.text * " = " * bf.prior
prior.distribution * " = " * bf.prior
)
),
env = list(
top.text = top.text,
effsize.text = effsize.text,
centrality = centrality,
prior.type = prior.type,
conf.level = paste0(conf.level * 100, "%"),
conf.method = toupper(conf.method),
bf = format_num(-log(data$bf10[[1]]), k),
estimate = format_num(estimate, k),
estimate.LB = format_num(estimate.LB, k),
estimate.UB = format_num(estimate.UB, k),
prior.text = prior.text,
prior.distribution = prior.distribution,
bf.prior = format_num(data$prior.scale[[1]], k)
)
)
Expand Down Expand Up @@ -379,13 +382,26 @@ estimate_type_switch <- function(x) {

#' @noRd

prior_type_switch <- function(x) {
prior_switch <- function(x) {
dplyr::case_when(
grepl("contingency", tolower(x)) ~ quote(italic("a")["Gunel-Dickey"]),
grepl("correlation", tolower(x)) ~ quote(italic("r")["beta"]^"JZS"),
TRUE ~ quote(italic("r")["Cauchy"]^"JZS")
)
}

#' @noRd

prior_type_switch <- function(x) {
dplyr::case_when(
grepl("contingency", tolower(x)) ~ list("Cramer"),
grepl("correlation", tolower(x)) ~ list("Pearson"),
grepl("t-|meta-", tolower(x)) ~ list("difference"),
grepl("linear", tolower(x)) ~ list("Bayesian"),
TRUE ~ list(NULL)
)[[1]]
}

#' @noRd

.prettyNum <- function(x) prettyNum(x, big.mark = ",", scientific = FALSE)
1 change: 0 additions & 1 deletion R/meta_random.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ expr_meta_random <- function(data,
no.parameters = 0L,
k = k,
top.text = top.text,
centrality = "mean",
bayesian = ifelse(type == "bayes", TRUE, FALSE)
)

Expand Down
15 changes: 10 additions & 5 deletions R/oneway_anova.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
#' @param ... Additional arguments (currently ignored).
#' @inheritParams stats::oneway.test
#'
#' @note
#' 1. Please note that the function expects that the data is
#' already sorted by subject/repeated measures ID.
#'
#' 2. To carry out Bayesian analysis for ANOVA designs, you will need to install
#' the development version of `BayesFactor` (`0.9.12-4.3`). You can download it
#' by running:
#' `remotes::install_github("richarddmorey/BayesFactor/pkg/BayesFactor")`.
#'
#' @importFrom dplyr select rename matches
#' @importFrom rlang !! !!! quo_is_null eval_tidy expr enexpr ensym exec new_formula
#' @importFrom stats oneway.test
Expand Down Expand Up @@ -206,11 +215,7 @@ expr_oneway_anova <- function(data,
# tidying it up
stats_df <- tidy_model_parameters(mod)
effsize_df <-
suppressWarnings(rlang::exec(
.fn = .f.es,
model = mod,
ci = conf.level
)) %>%
suppressWarnings(rlang::exec(.fn = .f.es, model = mod, ci = conf.level)) %>%
tidy_model_effectsize(.)

# combining dataframes
Expand Down
9 changes: 7 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ output: github_document
<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, echo = FALSE}
# show me all columns
options(tibble.width = Inf, pillar.bold = TRUE, pillar.subtle_num = TRUE)
options(
tibble.width = Inf,
pillar.bold = TRUE,
pillar.neg = TRUE,
pillar.subtle_num = TRUE,
pillar.min_chars = Inf
)
knitr::opts_chunk$set(
collapse = TRUE,
Expand Down
7 changes: 3 additions & 4 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"name": "R",
"url": "https://r-project.org"
},
"runtimePlatform": "R Under development (unstable) (2021-01-13 r79826)",
"runtimePlatform": "R version 4.0.3 (2020-10-10)",
"author": [
{
"@type": "Person",
Expand Down Expand Up @@ -202,7 +202,7 @@
"@type": "SoftwareApplication",
"identifier": "correlation",
"name": "correlation",
"version": ">= 0.5.0.1",
"version": ">= 0.5.1",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand Down Expand Up @@ -392,6 +392,5 @@
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"fileSize": "3910.285KB",
"readme": "https://github.com/IndrajeetPatil/statsExpressions/blob/master/README.md"
"fileSize": "3833.346KB"
}
2 changes: 1 addition & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ bayesian
bayestestR
changedate
checkmark
ci
codecov
conf
df
disgustingness
easystats
effectsize
Expand Down
10 changes: 10 additions & 0 deletions man/expr_oneway_anova.Rd

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

15 changes: 8 additions & 7 deletions man/expr_template.Rd

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

11 changes: 5 additions & 6 deletions tests/testthat/test-expr_contingency_tab_bayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ test_that(
counts = "Freq",
sampling.plan = "jointMulti",
fixed.margin = "rows",
k = 3L,
output = "expression",
conf.level = 0.99,
centrality = "mean"
k = 3L,
output = "expression"
)

# with counts
Expand All @@ -123,7 +122,7 @@ test_that(
ggplot2::expr(
paste(
"log"["e"] * "(BF"["01"] * ") = " * "-3.335" * ", ",
widehat(italic("V"))["median"]^"posterior" * " = " * "0.479" * ", ",
widehat(italic("V"))["Cramer"]^"posterior" * " = " * "0.479" * ", ",
"CI"["89%"]^"HDI" * " [" * "0.285" * ", " * "0.692" * "], ",
italic("a")["Gunel-Dickey"] * " = " * "1.000"
)
Expand All @@ -138,7 +137,7 @@ test_that(
ggplot2::expr(
paste(
"log"["e"] * "(BF"["01"] * ") = " * "-214.255" * ", ",
widehat(italic("V"))["median"]^"posterior" * " = " * "0.455" * ", ",
widehat(italic("V"))["Cramer"]^"posterior" * " = " * "0.455" * ", ",
"CI"["99%"]^"HDI" * " [" * "0.402" * ", " * "0.508" * "], ",
italic("a")["Gunel-Dickey"] * " = " * "1.000"
)
Expand All @@ -150,7 +149,7 @@ test_that(
ggplot2::expr(
paste(
"log"["e"] * "(BF"["01"] * ") = " * "-213.873" * ", ",
widehat(italic("V"))["median"]^"posterior" * " = " * "0.454" * ", ",
widehat(italic("V"))["Cramer"]^"posterior" * " = " * "0.454" * ", ",
"CI"["95%"]^"HDI" * " [" * "0.417" * ", " * "0.495" * "], ",
italic("a")["Gunel-Dickey"] * " = " * "1.500"
)
Expand Down
Loading

3 comments on commit 5f01107

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R/helpers_expr_templates.R:94:1: style: functions should have cyclomatic complexity of less than 15, this has 28.

expr_template <- function(data,
^

R/oneway_anova.R:151:1: style: functions should have cyclomatic complexity of less than 15, this has 19.

expr_oneway_anova <- function(data,
^

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R/helpers_expr_templates.R:94:1: style: functions should have cyclomatic complexity of less than 15, this has 28.

expr_template <- function(data,
^

R/oneway_anova.R:151:1: style: functions should have cyclomatic complexity of less than 15, this has 19.

expr_oneway_anova <- function(data,
^

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R/helpers_expr_templates.R:94:1: style: functions should have cyclomatic complexity of less than 15, this has 28.

expr_template <- function(data,
^

R/oneway_anova.R:151:1: style: functions should have cyclomatic complexity of less than 15, this has 19.

expr_oneway_anova <- function(data,
^

Please sign in to comment.