Extracts label information from statistical tests. Useful for labelling plots with test outputs.
See the Datanovia tutorial P-values from Tests on ggplots in R (rstatix) for a worked walkthrough.
get_pwc_label(stat.test, type = c("expression", "text"))
get_test_label(
stat.test,
description = NULL,
p.col = "p",
type = c("expression", "text"),
correction = c("auto", "GG", "HF", "none"),
row = NULL,
detailed = FALSE,
style = c("classic", "apa")
)
create_test_label(
statistic.text,
statistic,
p,
parameter = NA,
description = NULL,
n = NA,
effect.size = NA,
effect.size.text = NA,
type = c("expression", "text"),
detailed = FALSE,
style = c("classic", "apa"),
effect.size.ci = NA,
effect.size.bounded = TRUE,
effect.size.ci.level = 0.95
)
get_n(stat.test)
get_description(stat.test)statistical test results returned by rstatix
functions.
the label type. Can be one of "text" and "expression". Partial
match allowed. If you want to add the label onto a ggplot, it might be
useful to specify type = "expresion".
the test description used as the prefix of the label.
Examples of description are "ANOVA", "Two Way ANOVA". To remove the default
description, specify description = NULL. If missing, we'll try to
guess the statistical test default description.
character specifying the column containing the p-value. Default
is "p", can be "p.adj".
character, considered only in the case of ANOVA test. Which sphericity
correction of the degrees of freedom should be reported for the
within-subject factors (repeated measures). The default is set to
"GG" corresponding to the Greenhouse-Geisser correction. Possible
values are "GG", "HF" (i.e., Hyunh-Feldt correction),
"none" (i.e., no correction) and "auto" (apply automatically
GG correction if the sphericity assumption is not for within-subject
design.
numeric, the row index to be considered. If NULL, the last row is automatically considered for ANOVA test.
logical value. If TRUE, returns detailed label.
the label style. Either "classic" (default, the historical
format) or "apa" for an APA-7 in-text statistical report: the leading
test-name label is dropped, the p-value is formatted APA-style
(p = .023, p < .001), and the effect size is shown with its
confidence interval when the object carries one (e.g. anova_test(ci = )),
otherwise as a point estimate, otherwise omitted. Bounded effect sizes
(\(\eta^2\), r, Cliff's \(\delta\), rank-biserial) drop the
leading zero; Cohen's d keeps it.
character specifying the test statistic. For example
statistic.text = "F" (for ANOVA test ); statistic.text = "t"
(for t-test ).
the numeric value of a statistic.
the p-value of the test.
string containing the degree of freedom (if exists). Default
is NA to accommodate non-parametric tests. For example
parameter = "1,9" (for ANOVA test. Two parameters exist: DFn and
DFd); sparameter = "9" (for t-test ).
sample count, example: n = 10.
the effect size value
a character specifying the relevant effect size. For
example, for Cohens d statistic, effect.size.text = "d". You
can also use plotmath expression as follow quote(italic("d")).
a length-two numeric c(low, high) giving the
effect size's confidence interval, appended after the effect size when
style = "apa". Defaults to NA (no interval).
logical. Whether the effect size lies in
[-1, 1] (e.g. \(\eta^2\), r, Cliff's \(\delta\)); used by
style = "apa" to decide whether to drop the leading zero. Defaults to
TRUE; set FALSE for Cohen's d.
the confidence level effect.size.ci was
computed at, shown before the interval (e.g. 95% CI). Defaults to
0.95.
a text label or an expression to pass to a plotting function.
get_pwc_label(): Extract label from pairwise comparisons.
get_test_label(): Extract labels for statistical tests.
create_test_label(): Create labels from user specified test results.
get_n(): Extracts sample counts (n) from an rstatix test outputs. Returns a numeric vector.
get_description(): Extracts the description of an rstatix test outputs. Returns a character vector.
American Psychological Association (2020). Publication Manual of the American Psychological Association (7th ed.).
The Datanovia tutorial: P-values from Tests on ggplots in R (rstatix).
# Load data
#:::::::::::::::::::::::::::::::::::::::
data("ToothGrowth")
df <- ToothGrowth
# One-way ANOVA test
#:::::::::::::::::::::::::::::::::::::::::
anov <- df %>% anova_test(len ~ dose)
get_test_label(anov, detailed = TRUE, type = "text")
#> [1] "Anova, F(1,58) = 105.06, p = <0.0001, eta2[g] = 0.644, n = 60"
# Two-way ANOVA test
#:::::::::::::::::::::::::::::::::::::::::
anov <- df %>% anova_test(len ~ supp*dose)
get_test_label(anov, detailed = TRUE, type = "text",
description = "Two Way ANOVA")
#> [1] "Two Way ANOVA, F(1,56) = 5.33, p = 0.025, eta2[g] = 0.087, n = 60"
# Kruskal-Wallis test
#:::::::::::::::::::::::::::::::::::::::::
kruskal<- df %>% kruskal_test(len ~ dose)
get_test_label(kruskal, detailed = TRUE, type = "text")
#> [1] "Kruskal-Wallis, X2(2) = 40.67, p = <0.0001, n = 60"
# Wilcoxon test
#:::::::::::::::::::::::::::::::::::::::::
# Unpaired test
wilcox <- df %>% wilcox_test(len ~ supp)
get_test_label(wilcox, detailed = TRUE, type = "text")
#> [1] "Wilcoxon test, W = 575.5, p = 0.064, n = 60"
# Paired test
wilcox <- df %>% wilcox_test(len ~ supp, paired = TRUE)
get_test_label(wilcox, detailed = TRUE, type = "text")
#> [1] "Wilcoxon test, V = 350, p = 0.0043, n = 30"
# T test
#:::::::::::::::::::::::::::::::::::::::::
ttest <- df %>% t_test(len ~ dose)
get_test_label(ttest, detailed = TRUE, type = "text")
#> [[1]]
#> [1] "T test, t(37.99) = -6.48, p = <0.0001, n = 40"
#>
#> [[2]]
#> [1] "T test, t(36.88) = -11.8, p = <0.0001, n = 40"
#>
#> [[3]]
#> [1] "T test, t(37.1) = -4.9, p = <0.0001, n = 40"
#>
# Pairwise comparisons labels
#:::::::::::::::::::::::::::::::::::::::::
get_pwc_label(ttest, type = "text")
#> [1] "pwc: T test; p.adjust: Holm"
# Create test labels
#:::::::::::::::::::::::::::::::::::::::::
create_test_label(
statistic.text = "F", statistic = 71.82,
parameter = "4, 294",
p = "<0.0001",
description = "ANOVA",
type = "text"
)
#> [1] "ANOVA, p = <0.0001"
# Extract infos
#:::::::::::::::::::::::::::::::::::::::::
stat.test <- df %>% t_test(len ~ dose)
get_n(stat.test)
#> [1] 40 40 40
get_description(stat.test)
#> [1] "T test"