Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
more refactoring of ttest and anova
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Oct 28, 2020
1 parent e9127c6 commit c713bf9
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 221 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Expand Up @@ -35,7 +35,6 @@ Depends:
R (>= 3.6.0)
Imports:
BayesFactor,
bayestestR (>= 0.7.5),
dplyr,
effectsize (>= 0.4.0),
insight (>= 0.10.0),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -26,8 +26,11 @@ importFrom(BayesFactor,ttestBF)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,rename_with)
importFrom(dplyr,select)
importFrom(dplyr,starts_with)
importFrom(effectsize,effectsize)
importFrom(insight,get_priors)
importFrom(insight,standardize_names)
importFrom(ipmisc,"%$%")
importFrom(ipmisc,"%<-%")
Expand Down
52 changes: 19 additions & 33 deletions R/bf_oneway_anova.R
Expand Up @@ -8,7 +8,7 @@
#'
#' @importFrom BayesFactor anovaBF
#' @importFrom dplyr mutate
#' @importFrom rlang new_formula enexpr expr
#' @importFrom rlang new_formula enexpr expr exec !!!
#' @importFrom ipmisc long_to_wide_converter
#' @importFrom lme4 nobars findbars
#'
Expand Down Expand Up @@ -68,8 +68,6 @@ bf_oneway_anova <- function(data,
# make sure both quoted and unquoted arguments are allowed
c(x, y) %<-% c(rlang::ensym(x), rlang::ensym(y))

# ============================ data preparation ==========================

# have a proper cleanup with NA removal
data %<>%
ipmisc::long_to_wide_converter(
Expand All @@ -79,40 +77,28 @@ bf_oneway_anova <- function(data,
subject.id = {{ subject.id }},
paired = paired,
spread = FALSE
)

# ========================= within-subjects design ==========================
) %>%
dplyr::mutate(.data = ., rowid = as.factor(rowid))

# relevant arguments
if (isTRUE(paired)) {
# remove NAs
data %<>% dplyr::mutate(.data = ., rowid = as.factor(rowid))

# extracting results from Bayesian test (`y ~ x + id`) and creating a dataframe
bf_object <-
BayesFactor::anovaBF(
formula = rlang::new_formula(
{{ rlang::enexpr(y) }}, rlang::expr(!!rlang::enexpr(x) + rowid)
),
data = as.data.frame(data),
whichRandom = "rowid",
rscaleFixed = bf.prior,
progress = FALSE,
rscaleRandom = 1
)
bf.args <- list(
formula = rlang::new_formula({{ rlang::enexpr(y) }}, rlang::expr(!!rlang::enexpr(x) + rowid)),
whichRandom = "rowid",
rscaleRandom = 1
)
}
if (isFALSE(paired)) bf.args <- list(formula = rlang::new_formula({{ y }}, {{ x }}))

# ========================= between-subjects design =========================

if (isFALSE(paired)) {
# extracting results from Bayesian test and creating a dataframe
bf_object <-
BayesFactor::anovaBF(
formula = rlang::new_formula({{ y }}, {{ x }}),
data = as.data.frame(data),
rscaleFixed = bf.prior,
progress = FALSE
)
}
# creating a BayesFactor object
bf_object <-
rlang::exec(
.fn = BayesFactor::anovaBF,
data = as.data.frame(data),
rscaleFixed = bf.prior,
progress = FALSE,
!!!bf.args
)

# final return
bf_extractor(bf_object, ...)
Expand Down
39 changes: 14 additions & 25 deletions R/bf_ttest.R
Expand Up @@ -14,7 +14,7 @@
#' @inheritDotParams bf_extractor -bf.object
#'
#' @importFrom BayesFactor ttestBF
#' @importFrom rlang quo_is_null new_formula ensym enquo
#' @importFrom rlang quo_is_null new_formula ensym enquo exec !!!
#' @importFrom stats na.omit
#' @importFrom dplyr pull
#' @importFrom ipmisc long_to_wide_converter
Expand Down Expand Up @@ -101,31 +101,20 @@ bf_ttest <- function(data,
spread = paired
)

# within-subjects design
if (isTRUE(paired)) {
# extracting results from Bayesian test and creating a dataframe
bf_object <-
BayesFactor::ttestBF(
x = data[[2]],
y = data[[3]],
rscale = bf.prior,
paired = TRUE,
progress = FALSE
)
}
# relevant arguments
if (isTRUE(paired)) bf.args <- list(x = data[[2]], y = data[[3]])
if (isFALSE(paired)) bf.args <- list(formula = rlang::new_formula({{ y }}, {{ x }}))

# between-subjects design
if (isFALSE(paired)) {
# extracting results from Bayesian test and creating a dataframe
bf_object <-
BayesFactor::ttestBF(
formula = rlang::new_formula({{ y }}, {{ x }}),
data = as.data.frame(data),
rscale = bf.prior,
paired = FALSE,
progress = FALSE
)
}
# creating a BayesFactor object
bf_object <-
rlang::exec(
.fn = BayesFactor::ttestBF,
rscale = bf.prior,
paired = paired,
progress = FALSE,
data = as.data.frame(data),
!!!bf.args
)
}

# final return
Expand Down
3 changes: 2 additions & 1 deletion R/global_vars.R
Expand Up @@ -13,7 +13,8 @@ utils::globalVariables(
"term",
"conf.level",
"hpd95_lower",
"hpd95_upper"
"hpd95_upper",
"prior.parameter"
),
package = "tidyBF",
add = FALSE
Expand Down
14 changes: 6 additions & 8 deletions R/helpers_bf_tests.R
Expand Up @@ -20,8 +20,8 @@
#' @param ... Additional arguments passed to
#' [parameters::model_parameters.BFBayesFactor()].
#'
#' @importFrom dplyr mutate rename
#' @importFrom insight standardize_names
#' @importFrom dplyr mutate rename rename_with starts_with
#' @importFrom insight standardize_names get_priors
#' @importFrom performance r2_bayes
#' @importFrom effectsize effectsize
#' @importFrom parameters model_parameters
Expand Down Expand Up @@ -107,16 +107,14 @@ bf_extractor <- function(bf.object,

# prior
df_prior <-
bayestestR::describe_prior(bf.object) %>%
insight::get_priors(bf.object) %>%
dplyr::rename_with(.fn = ~ paste0("Prior_", .x), .cols = dplyr::everything()) %>%
insight::standardize_names(., style = "broom") %>%
dplyr::filter(., term == "fixed")
dplyr::filter(.data = ., prior.parameter == "fixed")

# merge the parameters dataframe with prior dataframe
df <-
dplyr::bind_cols(
dplyr::select(.data = df, -dplyr::contains("prior.")),
dplyr::select(.data = df_prior, dplyr::contains("prior."))
)
dplyr::bind_cols(dplyr::select(.data = df, -dplyr::contains("prior.")), df_prior)
}

# ------------------------ correlation ------------------------------
Expand Down
30 changes: 15 additions & 15 deletions README.md
Expand Up @@ -7,9 +7,9 @@
|-------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [![CRAN\_Release\_Badge](https://www.r-pkg.org/badges/version-ago/tidyBF)](https://CRAN.R-project.org/package=tidyBF) | [![Travis Build Status](https://travis-ci.org/IndrajeetPatil/tidyBF.svg?branch=master)](https://travis-ci.org/IndrajeetPatil/tidyBF) | [![Daily downloads badge](https://cranlogs.r-pkg.org/badges/last-day/tidyBF?color=blue)](https://CRAN.R-project.org/package=tidyBF) | [![GitHub version](https://img.shields.io/badge/GitHub-0.4.0.9000-orange.svg?style=flat-square)](https://github.com/IndrajeetPatil/tidyBF/) | [![Website](https://img.shields.io/badge/website-tidyBF-orange.svg?colorB=E91E63)](https://indrajeetpatil.github.io/tidyBF/) |
| [![CRAN Checks](https://cranchecks.info/badges/summary/tidyBF)](https://cran.r-project.org/web/checks/check_results_tidyBF.html) | [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/IndrajeetPatil/tidyBF?branch=master&svg=true)](https://ci.appveyor.com/project/IndrajeetPatil/tidyBF) | [![Weekly downloads badge](https://cranlogs.r-pkg.org/badges/last-week/tidyBF?color=blue)](https://CRAN.R-project.org/package=tidyBF) | [![Forks](https://img.shields.io/badge/forks-1-blue.svg)](https://github.com/IndrajeetPatil/tidyBF/) | [![Features](https://img.shields.io/badge/features-tidyBF-orange.svg?colorB=2196F3)](https://indrajeetpatil.github.io/tidyBF/reference/index.html) |
| [![minimal R version](https://img.shields.io/badge/R%3E%3D-3.6.0-6666ff.svg)](https://cran.r-project.org/) | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/) | [![Monthly downloads badge](https://cranlogs.r-pkg.org/badges/last-month/tidyBF?color=blue)](https://CRAN.R-project.org/package=tidyBF) | [![Github Issues](https://img.shields.io/badge/issues-1-red.svg)](https://github.com/IndrajeetPatil/tidyBF/issues) | [![vignettes](https://img.shields.io/badge/vignettes-0.4.0-orange.svg?colorB=FF5722)](https://indrajeetpatil.github.io/statsExpressions/articles/) |
| [![minimal R version](https://img.shields.io/badge/R%3E%3D-3.6.0-6666ff.svg)](https://cran.r-project.org/) | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/) | [![Monthly downloads badge](https://cranlogs.r-pkg.org/badges/last-month/tidyBF?color=blue)](https://CRAN.R-project.org/package=tidyBF) | [![Github Issues](https://img.shields.io/badge/issues-2-red.svg)](https://github.com/IndrajeetPatil/tidyBF/issues) | [![vignettes](https://img.shields.io/badge/vignettes-0.4.0-orange.svg?colorB=FF5722)](https://indrajeetpatil.github.io/statsExpressions/articles/) |
| [![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/IndrajeetPatil/tidyBF.svg)](https://github.com/IndrajeetPatil/tidyBF) | [![Coverage Status](https://coveralls.io/repos/github/IndrajeetPatil/tidyBF/badge.svg?branch=master)](https://coveralls.io/github/IndrajeetPatil/tidyBF?branch=master) | [![Total downloads badge](https://cranlogs.r-pkg.org/badges/grand-total/tidyBF?color=blue)](https://CRAN.R-project.org/package=tidyBF) | [![Github Stars](https://img.shields.io/github/stars/IndrajeetPatil/tidyBF.svg?style=social&label=Github)](https://github.com/IndrajeetPatil/tidyBF) | [![DOI](https://zenodo.org/badge/126624251.svg)](https://zenodo.org/badge/latestdoi/126624251) |
| [![Licence](https://img.shields.io/badge/licence-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html) | [![Codecov test coverage](https://codecov.io/gh/IndrajeetPatil/tidyBF/branch/master/graph/badge.svg)](https://codecov.io/gh/IndrajeetPatil/tidyBF?branch=master) | [![HitCount](https://hits.dwyl.com/IndrajeetPatil/tidyBF.svg)](https://hits.dwyl.com/IndrajeetPatil/tidyBF) | [![Last-changedate](https://img.shields.io/badge/last%20change-2020--10--27-yellowgreen.svg)](https://github.com/IndrajeetPatil/tidyBF/commits/master) | [![GitHub last commit](https://img.shields.io/github/last-commit/IndrajeetPatil/tidyBF.svg)](https://github.com/IndrajeetPatil/tidyBF/commits/master) |
| [![Licence](https://img.shields.io/badge/licence-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html) | [![Codecov test coverage](https://codecov.io/gh/IndrajeetPatil/tidyBF/branch/master/graph/badge.svg)](https://codecov.io/gh/IndrajeetPatil/tidyBF?branch=master) | [![HitCount](https://hits.dwyl.com/IndrajeetPatil/tidyBF.svg)](https://hits.dwyl.com/IndrajeetPatil/tidyBF) | [![Last-changedate](https://img.shields.io/badge/last%20change-2020--10--28-yellowgreen.svg)](https://github.com/IndrajeetPatil/tidyBF/commits/master) | [![GitHub last commit](https://img.shields.io/github/last-commit/IndrajeetPatil/tidyBF.svg)](https://github.com/IndrajeetPatil/tidyBF/commits/master) |
| [![status](https://tinyverse.netlify.com/badge/tidyBF)](https://CRAN.R-project.org/package=tidyBF) | [![R build status](https://github.com/IndrajeetPatil/tidyBF/workflows/R-CMD-check/badge.svg)](https://github.com/IndrajeetPatil/tidyBF) | [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/tidyBF/community) | [![Project Status](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) | [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/IndrajeetPatil/tidyBF/issues) |

# Overview <img src="man/figures/logo.png" align="right" width="240" />
Expand Down Expand Up @@ -314,7 +314,7 @@ result <-

# extract details
bf_extractor(result)
#> # A tibble: 21 x 19
#> # A tibble: 21 x 20
#> term estimate conf.low conf.high pd rope.percentage effect
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 mu 45.0 43.7 46.4 1 0 fixed
Expand All @@ -339,18 +339,18 @@ bf_extractor(result)
#> 8 conditional 0.233 -1.45 0.732 0.0509 95 0.609 0.812
#> 9 conditional 0.239 -1.43 0.732 0.0509 95 0.609 0.812
#> 10 conditional 2.65 0.974 0.732 0.0509 95 0.609 0.812
#> r2.component prior.distribution prior.location prior.scale
#> <chr> <chr> <dbl> <dbl>
#> 1 conditional cauchy 0 0.5
#> 2 conditional cauchy 0 0.5
#> 3 conditional cauchy 0 0.5
#> 4 conditional cauchy 0 0.5
#> 5 conditional cauchy 0 0.5
#> 6 conditional cauchy 0 0.5
#> 7 conditional cauchy 0 0.5
#> 8 conditional cauchy 0 0.5
#> 9 conditional cauchy 0 0.5
#> 10 conditional cauchy 0 0.5
#> r2.component prior.parameter prior.distribution prior.location prior.scale
#> <chr> <chr> <chr> <dbl> <dbl>
#> 1 conditional fixed cauchy 0 0.5
#> 2 conditional fixed cauchy 0 0.5
#> 3 conditional fixed cauchy 0 0.5
#> 4 conditional fixed cauchy 0 0.5
#> 5 conditional fixed cauchy 0 0.5
#> 6 conditional fixed cauchy 0 0.5
#> 7 conditional fixed cauchy 0 0.5
#> 8 conditional fixed cauchy 0 0.5
#> 9 conditional fixed cauchy 0 0.5
#> 10 conditional fixed cauchy 0 0.5
#> # ... with 11 more rows
```

Expand Down
15 changes: 1 addition & 14 deletions codemeta.json
Expand Up @@ -136,19 +136,6 @@
},
"sameAs": "https://CRAN.R-project.org/package=BayesFactor"
},
{
"@type": "SoftwareApplication",
"identifier": "bayestestR",
"name": "bayestestR",
"version": ">= 0.7.5",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=bayestestR"
},
{
"@type": "SoftwareApplication",
"identifier": "dplyr",
Expand Down Expand Up @@ -290,7 +277,7 @@
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"fileSize": "1989.395KB",
"fileSize": "1989.505KB",
"citation": [
{
"@type": "ScholarlyArticle",
Expand Down

0 comments on commit c713bf9

Please sign in to comment.