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

Commit

Permalink
Merge pull request #7 from IndrajeetPatil/simplify_dataframe
Browse files Browse the repository at this point in the history
simplify dataframe output
  • Loading branch information
IndrajeetPatil committed Oct 14, 2020
2 parents 8990120 + fed1294 commit 8f9479c
Show file tree
Hide file tree
Showing 36 changed files with 492 additions and 523 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Depends:
Imports:
BayesFactor,
dplyr,
insight (>= 0.9.6.1),
ipmisc,
metaBMA,
parameters,
Expand All @@ -35,6 +36,8 @@ Suggests:
rmarkdown,
spelling,
testthat
Remotes:
easystats/insight
Encoding: UTF-8
Language: en-US
LazyData: true
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,tibble)
importFrom(insight,standardize_names)
importFrom(ipmisc,"%$%")
importFrom(ipmisc,"%<-%")
importFrom(ipmisc,"%<>%")
importFrom(ipmisc,"%>%")
importFrom(ipmisc,as_tibble)
importFrom(ipmisc,blue)
importFrom(ipmisc,easystats_to_tidy_names)
importFrom(ipmisc,long_to_wide_converter)
importFrom(ipmisc,red)
importFrom(ipmisc,signif_column)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ BREAKING CHANGES

- Removes the unnecessary aliases for certain tests: `bf_one_sample_ttest`,
`bf_two_sample_ttest`, and `bf_onesample_proptest`.

- The output dataframe now only contains results for the alternative
hypothesis (`BF10`) and its `log` value to avoid cluttered dataframe since
all the other columns were essentially mathematical transformations of it
and not really relevant.

MAJOR CHANGES

Expand Down
27 changes: 14 additions & 13 deletions R/bf_contingency_tab.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ bf_contingency_tab <- function(data,
data %<>% dplyr::mutate(.data = ., {{ y }} := droplevels(as.factor({{ y }})))

# extracting results from Bayesian test and creating a dataframe
bf.df <-
df <-
bf_extractor(
BayesFactor::contingencyTableBF(
x = table(data %>% dplyr::pull({{ x }}), data %>% dplyr::pull({{ y }})),
Expand Down Expand Up @@ -185,21 +185,22 @@ bf_contingency_tab <- function(data,
pr_y_h1 <- BayesFactor::logMeanExpLogs(tmp_pr_h1)

# computing Bayes Factor and formatting the results
bf.df <-
df <-
tibble(bf10 = exp(pr_y_h1 - pr_y_h0)) %>%
bf_formatter(.) %>%
dplyr::mutate(.data = ., prior.concentration = prior.concentration)
dplyr::mutate(log_e_bf10 = log(bf10), prior.concentration = prior.concentration)
}

# ========================= caption preparation =============================

# changing aspects of the caption based on what output is needed
if (output %in% c("null", "caption", "H0", "h0")) {
bf.value <- bf.df$log_e_bf01[[1]]
bf.subscript <- "01"
# bf-related text
bf.value <- -log(df$bf10[[1]])
bf.sub <- "01"
} else {
bf.value <- -bf.df$log_e_bf01[[1]]
bf.subscript <- "10"
# bf-related text
bf.value <- log(df$bf10[[1]])
bf.sub <- "10"
}

# final expression
Expand All @@ -209,27 +210,27 @@ bf_contingency_tab <- function(data,
displaystyle(top.text),
expr = paste(
"log"["e"],
"(BF"[bf.subscript],
"(BF"[bf.sub],
") = ",
bf,
", ",
italic("a"),
italic("a")["Gunel-Dickey"],
" = ",
a
)
),
env = list(
top.text = caption,
bf.subscript = bf.subscript,
bf.sub = bf.sub,
bf = specify_decimal_p(x = bf.value, k = k),
a = specify_decimal_p(x = bf.df$prior.concentration[[1]], k = k)
a = specify_decimal_p(x = df$prior.concentration[[1]], k = k)
)
)

# return the text results or the dataframe with results
return(switch(
EXPR = output,
"results" = bf.df,
"results" = dplyr::select(df, -dplyr::matches("error|time|code")),
bf_message
))
}
1 change: 1 addition & 0 deletions R/bf_corr_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' @examples
#' # for reproducibility
#' set.seed(123)
#' library(tidyBF)
#'
#' # to see results
#' bf_corr_test(
Expand Down
20 changes: 10 additions & 10 deletions R/bf_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ bf_meta <- function(data,
dplyr::filter(.data = ., term == "d")

# dataframe with bayes factors
bf.df <-
tibble(bf10 = meta_res$BF["random_H1", "random_H0"]) %>%
bf_formatter(.)
df <- tibble(bf10 = meta_res$BF["random_H1", "random_H0"])

# changing aspects of the caption based on what output is needed
if (output %in% c("null", "caption", "H0", "h0")) {
bf.value <- bf.df$log_e_bf01[[1]]
bf.subscript <- "01"
# bf-related text
bf.value <- -log(df$bf10[[1]])
bf.sub <- "01"
} else {
bf.value <- bf.df$log_e_bf10[[1]]
bf.subscript <- "10"
# bf-related text
bf.value <- log(df$bf10[[1]])
bf.sub <- "10"
}

# prepare the Bayes factor message
Expand All @@ -108,7 +108,7 @@ bf_meta <- function(data,
atop(displaystyle(top.text),
expr = paste(
"log"["e"],
"(BF"[bf.subscript],
"(BF"[bf.sub],
") = ",
bf,
", ",
Expand All @@ -125,7 +125,7 @@ bf_meta <- function(data,
),
env = list(
top.text = caption,
bf.subscript = bf.subscript,
bf.sub = bf.sub,
bf = specify_decimal_p(x = bf.value, k = k),
d.pmean = specify_decimal_p(x = df_estimates$mean[[1]], k = k),
d.pmean.LB = specify_decimal_p(x = df_estimates$hpd95_lower[[1]], k = k),
Expand All @@ -136,7 +136,7 @@ bf_meta <- function(data,
# return the text results or the dataframe with results
return(switch(
EXPR = output,
"results" = bf.df,
"results" = df,
bf_message
))
}
31 changes: 14 additions & 17 deletions R/bf_ttest.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ bf_ttest <- function(data,
x <- rlang::ensym(x)
y <- if (!rlang::quo_is_null(rlang::enquo(y))) rlang::ensym(y)

# -------------------------- one-sample tests ------------------------------

if (rlang::quo_is_null(rlang::enquo(y))) {
bf_object <-
BayesFactor::ttestBF(
x = stats::na.omit(data %>% dplyr::pull({{ x }})),
rscale = bf.prior,
mu = test.value
)
}

# -------------------------- two-sample tests ------------------------------

if (!rlang::quo_is_null(rlang::enquo(y))) {
Expand All @@ -77,14 +88,11 @@ bf_ttest <- function(data,

# within-subjects design
if (isTRUE(paired)) {
# change names for convenience
colnames(data) <- c("rowid", "col1", "col2")

# extracting results from Bayesian test and creating a dataframe
bf_object <-
BayesFactor::ttestBF(
x = data$col1,
y = data$col2,
x = data[[2]],
y = data[[3]],
rscale = bf.prior,
paired = TRUE,
progress = FALSE
Expand All @@ -105,18 +113,7 @@ bf_ttest <- function(data,
}
}

# -------------------------- one-sample tests ------------------------------

if (rlang::quo_is_null(rlang::enquo(y))) {
bf_object <-
BayesFactor::ttestBF(
x = stats::na.omit(data %>% dplyr::pull({{ x }})),
rscale = bf.prior,
mu = test.value
)
}

# ============================ return ==================================
# -------------------------- return --------------------------

# return the text results or the dataframe with results
switch(
Expand Down
Loading

3 comments on commit 8f9479c

@lintr-bot
Copy link

Choose a reason for hiding this comment

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

tests/testthat/test-bf_ttest.R:124:7: style: Commented code should be removed.

# set.seed(123)
      ^~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:134:11: style: Commented code should be removed.

#     conf.level = 0.90
          ^~~~~~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:168:7: style: Commented code should be removed.

# set.seed(123)
      ^~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:177:11: style: Commented code should be removed.

#     conf.method = "eti"
          ^~~~~~~~~~~~~~~~~~~

@lintr-bot
Copy link

Choose a reason for hiding this comment

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

tests/testthat/test-bf_ttest.R:124:7: style: Commented code should be removed.

# set.seed(123)
      ^~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:134:11: style: Commented code should be removed.

#     conf.level = 0.90
          ^~~~~~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:168:7: style: Commented code should be removed.

# set.seed(123)
      ^~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:177:11: style: Commented code should be removed.

#     conf.method = "eti"
          ^~~~~~~~~~~~~~~~~~~

@lintr-bot
Copy link

Choose a reason for hiding this comment

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

tests/testthat/test-bf_ttest.R:124:7: style: Commented code should be removed.

# set.seed(123)
      ^~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:134:11: style: Commented code should be removed.

#     conf.level = 0.90
          ^~~~~~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:168:7: style: Commented code should be removed.

# set.seed(123)
      ^~~~~~~~~~~~~

tests/testthat/test-bf_ttest.R:177:11: style: Commented code should be removed.

#     conf.method = "eti"
          ^~~~~~~~~~~~~~~~~~~

Please sign in to comment.