Package 'apa'

Title: Format Outputs of Statistical Tests According to APA Guidelines
Description: Formatter functions in the 'apa' package take the return value of a statistical test function, e.g. a call to chisq.test() and return a string formatted according to the guidelines of the APA (American Psychological Association).
Authors: Daniel Gromer [aut, cre]
Maintainer: Daniel Gromer <[email protected]>
License: GPL (>= 3)
Version: 0.3.4.9000
Built: 2024-11-10 04:00:38 UTC
Source: https://github.com/dgromer/apa

Help Index


Report ANOVA in APA style

Description

Report ANOVA in APA style

Usage

anova_apa(
  x,
  effect = NULL,
  sph_corr = c("greenhouse-geisser", "gg", "huynh-feldt", "hf", "none"),
  force_sph_corr = FALSE,
  es = c("petasq", "pes", "getasq", "ges"),
  format = c("text", "markdown", "rmarkdown", "html", "latex", "latex_math", "docx",
    "plotmath"),
  info = FALSE,
  print = TRUE
)

Arguments

x

A call to aov, ez::ezANOVA, or afex::afex_ez, afex::afex_car or afex::afex_4

effect

Character string indicating the name of the effect to display. If is NULL, all effects are reported (default).

sph_corr

Character string indicating the method used for correction if the assumption of sphericity is violated (only applies to repeated-measures and mixed design ANOVA). Can be one of "greenhouse-geisser" (default), "huynh-feldt" or "none" (you may also use the abbreviations "gg" or "hf").

force_sph_corr

Logical indicating if sphericity correction should be applied to all within factors regardless of what the result of Mauchly's test of sphericity is (default is FALSE).

es

Character string indicating the effect size to display in the output, one of "petasq" (partial eta squared) or "getasq" (generalized eta squared) (you may also use the abbreviations "pes" or "ges").

format

Character string specifying the output format. One of "text", "markdown", "rmarkdown", html, "latex", "latex_math", "docx" or "plotmath".

info

Logical indicating whether to print a message on the used test (default is FALSE)

print

Logical indicating whether to print the formatted output via cat (TRUE, default) or return as a data frame.

Examples

# Using the ez package
library(ez)
data(ANT)

x <- ezANOVA(ANT[ANT$error==0,], dv = rt, wid = subnum,
             within = c(cue, flank), between = group, detailed = TRUE)
anova_apa(x)

# Using the afex package
library(afex)
data(md_12.1)

y <- aov_ez(id = "id", dv = "rt", data = md_12.1,
            within = c("angle", "noise"))
anova_apa(y)

APA Formatting for RMarkdown Reports

Description

A wrapper around the *_apa functions, providing a convenient way to use the formatters in inline code in RMarkdown documents.

Usage

apa(x, effect = NULL, format = "rmarkdown", print = FALSE, ...)

Arguments

x

An R object. Must be a call to one of afex::aov_4, afex::aov_car, afex::aov_ez, chisq.test, cor.test, ez::ezANOVA or t_test.

effect

(only applicable if x is an ANOVA) Character string indicating the name of the effect to display. If is NULL, all effects are reported (default).

format

Character string specifying the output format. One of "text", "markdown", "rmarkdown", html, "latex" or "docx".

print

Logical indicating whether to return the result as an R object (FALSE) or print using cat (TRUE).

...

Further arguments passed to other methods

See Also

anova_apa, chisq_apa, cor_apa, t_apa


Report Chi-squared test in APA style

Description

Report Chi-squared test in APA style

Usage

chisq_apa(
  x,
  print_n = FALSE,
  format = c("text", "markdown", "rmarkdown", "html", "latex", "latex_math", "docx",
    "plotmath"),
  info = FALSE,
  print = TRUE
)

Arguments

x

A call to chisq.test

print_n

Logical indicating whether to show sample size in text

format

Character string specifying the output format. One of "text", "markdown", "rmarkdown", html, "latex", "latex_math", "docx" or "plotmath".

info

Logical indicating whether to print a message on the used test (default is FALSE)

print

Logical indicating whether to print the formatted output via cat (TRUE, default) or return as character string.

Examples

# Example data from ?chisq.test
m <- rbind(c(762, 327, 468), c(484, 239, 477))

chisq_apa(chisq.test(m))

Cohen's d

Description

Calculate Cohen's d from raw data or a call to t_test/t.test.

Usage

cohens_d(...)

## Default S3 method:
cohens_d(
  x,
  y = NULL,
  paired = FALSE,
  corr = c("none", "hedges_g", "glass_delta"),
  na.rm = FALSE,
  ...
)

## S3 method for class 'data.frame'
cohens_d(
  data,
  dv,
  iv,
  paired = FALSE,
  corr = c("none", "hedges_g", "glass_delta"),
  na.rm = FALSE,
  ...
)

## S3 method for class 'formula'
cohens_d(
  formula,
  data,
  corr = c("none", "hedges_g", "glass_delta"),
  na.rm = FALSE,
  ...
)

## S3 method for class 'htest'
cohens_d(ttest, corr = c("none", "hedges_g", "glass_delta"), ...)

Arguments

...

Further arguments passed to methods.

x

A (non-empty) numeric vector of data values.

y

An optional (non-empty) numeric vector of data values.

paired

A logical indicating whether Cohen's d should be calculated for a paired sample or two independent samples (default). Ignored when calculating Cohen's for one sample.

corr

Character specifying the correction applied to calculation of the effect size: "none" (default) returns Cohen's d, "hedges_g" applies Hedges correction and "glass_delta" calculates Glass' Δ\Delta (uses the standard deviation of the second group).

na.rm

Logical. Should missing values be removed?

data

A data frame containing either the variables in the formula formula or the variables specified by dv and iv.

dv

Character indicating the name of the column in data for the dependent variable

iv

Character indicating the name of the column in data for the independent variable

formula

A formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs either 1 for one sample or paired data or a factor with two levels giving the corresponding groups. If lhs is of class "Pair" and rhs is 1, Cohen's d for paired data will be calculated.

ttest

An object of class htest (a call to either t_test (preferred) or t.test).

Details

To calculate Cohen's d from summary statistics (M, SD, ..) use cohens_d_.

References

Lakens, D. (2013). Calculating and reporting effect sizes to facilitate cumulative science: a practical primer for t-tests and ANOVAs. Frontiers in Psychology, 4, 863. doi:10.3389/fpsyg.2013.00863

Examples

# Calculate from raw data
cohens_d(c(10, 15, 11, 14, 17), c(22, 18, 23, 25, 20))

# Methods when working with data frames
cohens_d(sleep, dv = extra, iv = group, paired = TRUE)
# or
cohens_d(sleep, dv = "extra", iv = "group", paired = TRUE)
# formula interface
sleep2 <- reshape(sleep, direction = "wide", idvar = "ID", timevar = "group")
cohens_d(Pair(extra.1, extra.2) ~ 1, sleep2, paired = TRUE)

# Or pass a call to t_test or t.test
cohens_d(t_test(Pair(extra.1, extra.2) ~ 1, sleep2))

Cohen's d

Description

Calculate Cohens'd from different statistics (see Details).

Usage

cohens_d_(
  m1 = NULL,
  m2 = NULL,
  sd1 = NULL,
  sd2 = NULL,
  n1 = NULL,
  n2 = NULL,
  t = NULL,
  n = NULL,
  paired = FALSE,
  one_sample = FALSE,
  corr = c("none", "hedges_g", "glass_delta")
)

Arguments

m1

Numeric, mean of the first group

m2

Numeric, mean of the second group

sd1

Numeric, standard deviation of the first group

sd2

Numeric, standard deviation of the second group

n1

Numeric, size of the first group

n2

Numeric, size of the second group

t

Numeric, t-test statistic

n

Numeric, total sample size

paired

Logical indicating whether to calculate Cohen's d for independent samples or one sample (FALSE, default) or for dependent samples (TRUE).

one_sample

Logical indicating whether to calculate Cohen's d for one sample (TRUE) or independent samples (FALSE, default) (only relevant when providing t and n, see below).

corr

Character specifying the correction applied to calculation of the effect size: "none" (default) returns Cohen's d, "hedges_g" applies Hedges correction and "glass_delta" calculates Glass' Δ\Delta (uses the standard deviation of the second group).

Details

The following combinations of statistics are possible:

  • m1, m2, sd1, sd2, n1 and n2

  • t, n1 and n2

  • t and n

References

Lakens, D. (2013). Calculating and reporting effect sizes to facilitate cumulative science: a practical primer for t-tests and ANOVAs. Frontiers in Psychology, 4, 863. doi:10.3389/fpsyg.2013.00863


Report Correlation in APA style

Description

Report Correlation in APA style

Usage

cor_apa(
  x,
  r_ci = FALSE,
  format = c("text", "markdown", "rmarkdown", "html", "latex", "latex_math", "docx",
    "plotmath"),
  info = FALSE,
  print = TRUE
)

Arguments

x

A call to cor.test

r_ci

Logical indicating whether to display the confidence interval for the correlation coefficient (default is FALSE). Only available for Pearson's product moment correlation (with n >= 4).

format

Character string specifying the output format. One of "text", "markdown", "rmarkdown", html, "latex", "latex_math", "docx" or "plotmath".

info

Logical indicating whether to print a message on the used test (default is FALSE)

print

Logical indicating whether to print the formatted output via cat (TRUE, default) or return as character string.

Examples

# Example data from ?cor.test
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6,  3.1,  2.5,  5.0,  3.6,  4.0,  5.2,  2.8,  3.8)

cor_apa(cor.test(x, y))

# Spearman's rho
cor_apa(cor.test(x, y, method = "spearman"))

# Kendall's tau
cor_apa(cor.test(x, y, method = "kendall"))

Partial Eta Squared

Description

Partial Eta Squared

Usage

petasq(x, effect)

Arguments

x

A call to aov, ez::ezANOVA or afex::aov_ez or afex::aov_car or afex::aov_4

effect

Character string indicating the name of the effect for which the partial eta squared should be returned.


Partial Eta Squared

Description

Calculate the partial eta squared effect size from sum of squares.

ηp2=SSeffectSSeffect+SSerror\eta_p^2 = \frac{SS_effect}{SS_effect + SS_error}

Usage

petasq_(ss_effect, ss_error)

Arguments

ss_effect

numeric, sum of squares of the effect

ss_error

numeric, sum of squares of the corresponding error


Report t-Test in APA style

Description

Report t-Test in APA style

Usage

t_apa(
  x,
  es = c("cohens_d", "hedges_g", "glass_delta"),
  es_ci = FALSE,
  format = c("text", "markdown", "rmarkdown", "html", "latex", "latex_math", "docx",
    "plotmath"),
  info = FALSE,
  print = TRUE
)

Arguments

x

A call to t_test or t.test

es

Character specifying the effect size to report. One of "cohens_d" (default), "hedges_g" or "glass_delta" if x is an independent samples t-test. Ignored if x is a paired samples or one sample t-test (cohen's d is reported for these test).

es_ci

Logical indicating whether to add the 95% confidence interval for Cohen's d (experimental; default is FALSE).

format

Character string specifying the output format. One of "text", "markdown", "rmarkdown", html, "latex", "latex_math", "docx" or "plotmath".

info

Logical indicating whether to print a message on the used test (default is FALSE)

print

Logical indicating whether to print the formatted output via cat (TRUE, default) or return as character string.

Examples

# Two independent samples t-test
t_apa(t_test(1:10, y = c(7:20)))

# Two dependent samples t-test
sleep2 <- reshape(sleep, direction = "wide", idvar = "ID", timevar = "group")
t_apa(t_test(Pair(extra.1, extra.2) ~ 1, sleep2))

Student's t-Test

Description

A wrapper for t.test which includes the original data in the returned object.

Usage

t_test(x, ...)

## Default S3 method:
t_test(
  x,
  y = NULL,
  alternative = c("two.sided", "less", "greater"),
  mu = 0,
  paired = FALSE,
  var.equal = FALSE,
  conf.level = 0.95,
  ...
)

## S3 method for class 'formula'
t_test(formula, data, subset, na.action, ...)

Arguments

x

a (non-empty) numeric vector of data values.

...

further arguments to be passed to or from methods.

y

an optional (non-empty) numeric vector of data values.

alternative

a character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less". You can specify just the initial letter.

mu

a number indicating the true value of the mean (or difference in means if you are performing a two sample test).

paired

a logical indicating whether you want a paired t-test.

var.equal

a logical variable indicating whether to treat the two variances as being equal. If TRUE then the pooled variance is used to estimate the variance otherwise the Welch (or Satterthwaite) approximation to the degrees of freedom is used.

conf.level

confidence level of the interval.

formula

a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs either 1 for a one-sample or paired test or a factor with two levels giving the corresponding groups. If lhs is of class "Pair" and rhs is 1, a paired test is done.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).

subset

an optional vector specifying a subset of observations to be used.

na.action

a function which indicates what should happen when the data contain NAs. Defaults to getOption("na.action").

See Also

t.test