| t_test | R Documentation |
Provides a pipe-friendly framework to performs one and two sample t-tests.
See the Datanovia tutorial T-Test in R for a worked walkthrough.
t_test(
data,
formula,
comparisons = NULL,
ref.group = NULL,
p.adjust.method = "holm",
paired = FALSE,
var.equal = FALSE,
alternative = "two.sided",
mu = 0,
conf.level = 0.95,
detailed = FALSE,
id = NULL,
error.as.na = FALSE,
effect.size = FALSE
)
pairwise_t_test(
data,
formula,
comparisons = NULL,
ref.group = NULL,
p.adjust.method = "holm",
paired = FALSE,
pool.sd = !paired,
detailed = FALSE,
...,
effect.size = FALSE
)
data |
a data.frame containing the variables in the formula. |
formula |
a formula of the form |
comparisons |
A list of length-2 vectors specifying the groups of
interest to be compared. For example to compare groups "A" vs "B" and "B" vs
"C", the argument is as follow: |
ref.group |
a character string specifying the reference group. If specified, for a given grouping variable, each of the group levels will be compared to the reference group (i.e. control group). If |
p.adjust.method |
method to adjust p values for multiple comparisons. Used when pairwise comparisons are performed. Allowed values include "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". If you don't want to adjust the p value (not recommended), use p.adjust.method = "none". |
paired |
a logical indicating whether you want a paired test. |
var.equal |
a logical variable indicating whether to treat the
two variances as being equal. If |
alternative |
a character string specifying the alternative
hypothesis, must be one of |
mu |
a number specifying an optional parameter used to form the null hypothesis. |
conf.level |
confidence level of the interval. |
detailed |
logical value. Default is FALSE. If TRUE, a detailed result is shown. |
id |
(optional) character string specifying the column that contains the
sample/subject identifier, used only for a paired test
( |
error.as.na |
logical. If |
effect.size |
logical. Default is FALSE. If TRUE, a |
pool.sd |
logical value used in the function The If |
... |
other arguments to be passed to the function
|
- If a list of comparisons is specified, the result of the pairwise tests is filtered to keep only the comparisons of interest. The p-value is adjusted after filtering.
- For a grouped data, if pairwise test is performed, then the p-values are adjusted for each group level independently.
return a data frame with some the following columns:
.y.: the y variable used in the test.
group1,group2: the
compared groups in the pairwise tests.
n,n1,n2: Sample counts.
statistic: Test statistic used to compute the p-value.
df: degrees of freedom.
p: p-value.
p.adj:
the adjusted p-value.
method: the statistical test used to
compare groups.
p.signif, p.adj.signif: the significance level
of p-values and adjusted p-values, respectively.
estimate:
estimate of the effect size. It corresponds to the estimated mean or
difference in means depending on whether it was a one-sample test or a
two-sample test. For a two-sample test the difference is taken as
estimate1 - estimate2, i.e. mean(group1) - mean(group2)
(following t.test).
estimate1, estimate2:
show the mean values of the two groups, respectively, for independent
samples t-tests.
alternative: a character string describing the
alternative hypothesis.
conf.low,conf.high: Lower and upper
bound on a confidence interval.
The returned object has an attribute called args, which is a list holding the test arguments.
t_test(): t test
pairwise_t_test(): performs pairwise two sample t-test. Wrapper around the R
base function pairwise.t.test.
On the sign of statistic and estimate when a
ref.group is specified: the reference group is taken as group1
and the other group as group2, and the difference is computed as
estimate = mean(group1) - mean(group2) = mean(ref.group) -
mean(other) (the t.test convention). A positive
statistic/estimate therefore means the value is higher in the
reference group. To orient results so that a positive sign means "higher in
the non-reference group", flip the sign yourself, e.g.
mutate(statistic = -statistic, estimate = -estimate).
rstatix-programming for building the formula from
variable names held in strings (e.g. reformulate()).
The Datanovia tutorial: T-Test in R.
# Load data
#:::::::::::::::::::::::::::::::::::::::
data("ToothGrowth")
df <- ToothGrowth
# One-sample test
#:::::::::::::::::::::::::::::::::::::::::
df %>% t_test(len ~ 1, mu = 0)
# Two-samples unpaired test
#:::::::::::::::::::::::::::::::::::::::::
df %>% t_test(len ~ supp)
# Two-samples paired test
#:::::::::::::::::::::::::::::::::::::::::
df %>% t_test (len ~ supp, paired = TRUE)
# Compare supp levels after grouping the data by "dose"
#::::::::::::::::::::::::::::::::::::::::
df %>%
group_by(dose) %>%
t_test(data =., len ~ supp) %>%
adjust_pvalue(method = "bonferroni") %>%
add_significance("p.adj")
# pairwise comparisons
#::::::::::::::::::::::::::::::::::::::::
# As dose contains more than two levels ==>
# pairwise test is automatically performed.
df %>% t_test(len ~ dose)
# Comparison against reference group
#::::::::::::::::::::::::::::::::::::::::
# each level is compared to the ref group
df %>% t_test(len ~ dose, ref.group = "0.5")
# Comparison against all
#::::::::::::::::::::::::::::::::::::::::
df %>% t_test(len ~ dose, ref.group = "all")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.