Data frame outputs

#| label = "setup",
#| message = FALSE,
#| warning = FALSE,
#| include = FALSE,
#| echo = FALSE
source("../setup.R")

pkgs <- c(
  "ggplot2",
  "metaplus"
)

successfully_loaded <- purrr::map_lgl(pkgs, requireNamespace, quietly = TRUE)
can_evaluate <- all(successfully_loaded)

if (can_evaluate) {
  purrr::walk(pkgs, library, character.only = TRUE)
} else {
  knitr::opts_chunk$set(eval = FALSE)
}

This vignette can be cited as:

#| label = "citation",
#| echo = FALSE,
#| comment = ""
citation("statsExpressions")

One-sample tests

#| label = "onesample",
#| file = "../../man/examples/examples-one_sample_test.R"

# ----------------------- parametric ---------------------------------------

set.seed(123)
one_sample_test(
  data       = ggplot2::msleep,
  x          = brainwt,
  test.value = 0.275,
  type       = "parametric"
)

# ----------------------- non-parametric -----------------------------------

set.seed(123)
one_sample_test(
  data       = ggplot2::msleep,
  x          = brainwt,
  test.value = 0.275,
  type       = "nonparametric"
)

# ----------------------- robust --------------------------------------------

set.seed(123)
one_sample_test(
  data       = ggplot2::msleep,
  x          = brainwt,
  test.value = 0.275,
  type       = "robust"
)

# ----------------------- Bayesian ---------------------------------------

set.seed(123)
one_sample_test(
  data       = ggplot2::msleep,
  x          = brainwt,
  test.value = 0.275,
  type       = "bayes",
  bf.prior   = 0.8
)

Two-sample tests

within-subjects design

#| label = "twosample_w"

# data
df <- dplyr::filter(bugs_long, condition %in% c("LDLF", "LDHF"))

# ----------------------- parametric ---------------------------------------

set.seed(123)
two_sample_test(
  data       = df,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "p"
)

# ----------------------- non-parametric -----------------------------------

set.seed(123)
two_sample_test(
  data       = df,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "np"
)

# ----------------------- robust --------------------------------------------

set.seed(123)
two_sample_test(
  data       = df,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "r"
)

# ----------------------- Bayesian ---------------------------------------

set.seed(123)
two_sample_test(
  data       = df,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "bayes"
)

between-subjects design

#| label = "twosample_b"

# ----------------------- parametric ---------------------------------------

# unequal variance
set.seed(123)
two_sample_test(
  data      = ToothGrowth,
  x         = supp,
  y         = len,
  type      = "p"
)

# equal variance
set.seed(123)
two_sample_test(
  data      = ToothGrowth,
  x         = supp,
  y         = len,
  var.equal = TRUE,
  type      = "p"
)

# ----------------------- non-parametric -----------------------------------

set.seed(123)
two_sample_test(
  data      = ToothGrowth,
  x         = supp,
  y         = len,
  type      = "np"
)

# ----------------------- robust --------------------------------------------

set.seed(123)
two_sample_test(
  data      = ToothGrowth,
  x         = supp,
  y         = len,
  type      = "r"
)

# ----------------------- Bayesian ---------------------------------------

set.seed(123)
two_sample_test(
  data      = ToothGrowth,
  x         = supp,
  y         = len,
  type      = "bayes"
)

One-way ANOVAs

within-subjects design

#| label = "anova_w"
suppressPackageStartupMessages(library(afex))

# ----------------------- parametric ---------------------------------------

set.seed(123)
oneway_anova(
  data       = bugs_long,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "p"
)

# ----------------------- non-parametric -----------------------------------

set.seed(123)
oneway_anova(
  data       = bugs_long,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "np"
)

# ----------------------- robust --------------------------------------------

set.seed(123)
oneway_anova(
  data       = bugs_long,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "r"
)

# ----------------------- Bayesian ---------------------------------------

set.seed(123)
oneway_anova(
  data       = bugs_long,
  x          = condition,
  y          = desire,
  paired     = TRUE,
  subject.id = subject,
  type       = "bayes"
)

between-subjects design

#| label = "anova_b"

# ----------------------- parametric ---------------------------------------

# unequal variance
set.seed(123)
oneway_anova(
  data      = iris,
  x         = Species,
  y         = Sepal.Length,
  type      = "p"
)

# equal variance
set.seed(123)
oneway_anova(
  data      = iris,
  x         = Species,
  y         = Sepal.Length,
  var.equal = TRUE,
  type      = "p"
)

# ----------------------- non-parametric -----------------------------------

set.seed(123)
oneway_anova(
  data      = iris,
  x         = Species,
  y         = Sepal.Length,
  type      = "np"
)

# ----------------------- robust --------------------------------------------

set.seed(123)
oneway_anova(
  data      = iris,
  x         = Species,
  y         = Sepal.Length,
  type      = "r"
)

# ----------------------- Bayesian ---------------------------------------

set.seed(123)
oneway_anova(
  data      = iris,
  x         = Species,
  y         = Sepal.Length,
  type      = "bayes"
)

Contingency table analyses

association test

#| label = "conttabs"

# ------------------------ frequentist -----------------------------

# unpaired
set.seed(123)
contingency_table(
  data   = mtcars,
  x      = am,
  y      = vs,
  paired = FALSE
)

# paired
## example data
paired_data <- tibble(
  response_before = structure(c(1L, 2L, 1L, 2L), levels = c("no", "yes"), class = "factor"),
  response_after = structure(c(1L, 1L, 2L, 2L), levels = c("no", "yes"), class = "factor"),
  Freq = c(65L, 25L, 5L, 5L)
)

set.seed(123)
contingency_table(
  data   = paired_data,
  x      = response_before,
  y      = response_after,
  paired = TRUE,
  counts = "Freq"
)

# ------------------------ Bayesian -----------------------------

# unpaired
set.seed(123)
contingency_table(
  data   = mtcars,
  x      = am,
  y      = vs,
  paired = FALSE,
  type   = "bayes"
)

goodness-of-fit tests

#| label = "gof"
# ------------------------ frequentist -----------------------------

# with counts
set.seed(123)
contingency_table(
  data   = as.data.frame(HairEyeColor),
  x      = Eye,
  counts = Freq
)

# ------------------------ Bayesian -----------------------------

set.seed(123)
contingency_table(
  data   = as.data.frame(HairEyeColor),
  x      = Eye,
  counts = Freq,
  ratio  = c(0.2, 0.2, 0.3, 0.3),
  type   = "bayes"
)

Correlation analyses

#| label = "corr",
#| file = "../../man/examples/examples-corr_test.R"

Meta-analysis

#| label = "meta"
library(metaplus)

# renaming columns to `{statsExpressions}` conventions
df <- dplyr::rename(mag, estimate = yi, std.error = sei)

# ----------------------- parametric ---------------------------------------

set.seed(123)
meta_analysis(df, type = "parametric")

# ----------------------- robust --------------------------------------------

set.seed(123)
meta_analysis(df, type = "robust")

# ----------------------- Bayesian ---------------------------------------

# suppress warnings about divergent transitions after warmup
set.seed(123)
suppressWarnings(meta_analysis(df, type = "bayes"))

Centrality description

#| label = "centrality",
#| file = "../../man/examples/examples-centrality_description.R"

Pairwise comparisons for one-way design

Between-subjects design

# ----------------------- parametric -----------------------

# if `var.equal = TRUE`, then Student's *t*-test will be run
pairwise_comparisons(
  data            = ggplot2::msleep,
  x               = vore,
  y               = brainwt,
  type            = "parametric",
  var.equal       = TRUE,
  paired          = FALSE,
  p.adjust.method = "bonferroni"
)

# if `var.equal = FALSE`, then Games-Howell test will be run
pairwise_comparisons(
  data            = ggplot2::msleep,
  x               = vore,
  y               = brainwt,
  type            = "parametric",
  var.equal       = FALSE,
  paired          = FALSE,
  p.adjust.method = "bonferroni"
)

# ----------------------- non-parametric -------------------

pairwise_comparisons(
  data            = ggplot2::msleep,
  x               = vore,
  y               = brainwt,
  type            = "nonparametric",
  paired          = FALSE,
  p.adjust.method = "none"
)

# ----------------------- robust ---------------------------

pairwise_comparisons(
  data            = ggplot2::msleep,
  x               = vore,
  y               = brainwt,
  type            = "robust",
  paired          = FALSE,
  p.adjust.method = "fdr"
)

# ----------------------- Bayesian -------------------------

pairwise_comparisons(
  data   = ggplot2::msleep,
  x      = vore,
  y      = brainwt,
  type   = "bayes",
  paired = FALSE
)

Within-subjects design

# ----------------------- parametric -----------------------

pairwise_comparisons(
  data            = bugs_long,
  x               = condition,
  y               = desire,
  subject.id      = subject,
  type            = "parametric",
  paired          = TRUE,
  p.adjust.method = "BH"
)

# ----------------------- non-parametric -------------------

pairwise_comparisons(
  data            = bugs_long,
  x               = condition,
  y               = desire,
  subject.id      = subject,
  type            = "nonparametric",
  paired          = TRUE,
  p.adjust.method = "BY"
)

# ----------------------- robust ---------------------------

pairwise_comparisons(
  data            = bugs_long,
  x               = condition,
  y               = desire,
  subject.id      = subject,
  type            = "robust",
  paired          = TRUE,
  p.adjust.method = "hommel"
)

# ----------------------- Bayesian -------------------------

pairwise_comparisons(
  data       = bugs_long,
  x          = condition,
  y          = desire,
  subject.id = subject,
  type       = "bayes",
  paired     = TRUE,
  bf.prior   = 0.77
)

Suggestions

If you find any bugs or have any suggestions/remarks, please file an issue on GitHub: https://github.com/IndrajeetPatil/statsExpressions/issues



Try the statsExpressions package in your browser

Any scripts or data that you put into this service are public.

statsExpressions documentation built on Sept. 12, 2023, 5:07 p.m.