compare_means: Comparison of Means

Description Usage Arguments Value Examples

View source: R/compare_means.R

Description

Performs one or multiple mean comparisons.

Usage

1
2
3
compare_means(formula, data, method = "wilcox.test", paired = FALSE,
  group.by = NULL, ref.group = NULL, symnum.args = list(),
  p.adjust.method = "holm", ...)

Arguments

formula

a formula of the form x ~ group where x is a numeric variable giving the data values and group is a factor with one or multiple levels giving the corresponding groups. For example, formula = TP53 ~ cancer_group.

It's also possible to perform the test for multiple response variables at the same time. For example, formula = c(TP53, PTEN) ~ cancer_group.

data

a data.frame containing the variables in the formula.

method

the type of test. Default is wilcox.test. Allowed values include:

  • t.test (parametric) and wilcox.test (non-parametric). Perform comparison between two groups of samples. If the grouping variable contains more than two levels, then a pairwise comparison is performed.

  • anova (parametric) and kruskal.test (non-parametric). Perform one-way ANOVA test comparing multiple groups.

paired

a logical indicating whether you want a paired test. Used only in t.test and in wilcox.test.

group.by

a character vector containing the name of grouping variables.

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).

ref.group can be also ".all.". In this case, each of the grouping variable levels is compared to all (i.e. basemean).

symnum.args

a list of arguments to pass to the function symnum for symbolic number coding of p-values. For example, symnum.args <- list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("****", "***", "**", "*", "ns")).

In other words, we use the following convention for symbols indicating statistical significance:

  • ns: p > 0.05

  • *: p <= 0.05

  • **: p <= 0.01

  • ***: p <= 0.001

  • ****: p <= 0.0001

p.adjust.method

method for adjusting p values (see p.adjust). Has impact only in a situation, where multiple pairwise tests are performed; or when there are multiple grouping variables. 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".

Note that, when the formula contains multiple variables, the p-value adjustment is done independently for each variable.

...

Other arguments to be passed to the test function.

Value

return a data frame with the following columns:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Load data
#:::::::::::::::::::::::::::::::::::::::
data("ToothGrowth")
df <- ToothGrowth

# One-sample test
#:::::::::::::::::::::::::::::::::::::::::
compare_means(len ~ 1, df, mu = 0)

# Two-samples unpaired test
#:::::::::::::::::::::::::::::::::::::::::
compare_means(len ~ supp, df)

# Two-samples paired test
#:::::::::::::::::::::::::::::::::::::::::
compare_means(len ~ supp, df, paired = TRUE)

# Compare supp levels after grouping the data by "dose"
#::::::::::::::::::::::::::::::::::::::::
compare_means(len ~ supp, df, group.by = "dose")

# pairwise comparisons
#::::::::::::::::::::::::::::::::::::::::
# As dose contains more thant two levels ==>
# pairwise test is automatically performed.
compare_means(len ~ dose, df)

# Comparison against reference group
#::::::::::::::::::::::::::::::::::::::::
compare_means(len ~ dose, df, ref.group = "0.5")

# Comparison against all
#::::::::::::::::::::::::::::::::::::::::
compare_means(len ~ dose, df, ref.group = ".all.")

# Anova and kruskal.test
#::::::::::::::::::::::::::::::::::::::::
compare_means(len ~ dose, df, method = "anova")
compare_means(len ~ dose, df, method = "kruskal.test")

Example output

Loading required package: ggplot2
Loading required package: magrittr
# A tibble: 1 x 8
    .y. group1     group2            p        p.adj p.format p.signif   method
  <chr>  <dbl>      <chr>        <dbl>        <dbl>    <chr>    <chr>    <chr>
1   len      1 null model 1.664007e-11 1.664007e-11  1.7e-11     **** Wilcoxon
# A tibble: 1 x 8
    .y. group1 group2          p      p.adj p.format p.signif   method
  <chr>  <chr>  <chr>      <dbl>      <dbl>    <chr>    <chr>    <chr>
1   len     OJ     VC 0.06449067 0.06449067    0.064       ns Wilcoxon
# A tibble: 1 x 8
    .y. group1 group2           p       p.adj p.format p.signif   method
  <chr>  <chr>  <chr>       <dbl>       <dbl>    <chr>    <chr>    <chr>
1   len     OJ     VC 0.004312554 0.004312554   0.0043       ** Wilcoxon
# A tibble: 3 x 9
   dose   .y. group1 group2           p      p.adj p.format p.signif   method
  <dbl> <chr>  <chr>  <chr>       <dbl>      <dbl>    <chr>    <chr>    <chr>
1   0.5   len     OJ     VC 0.023186427 0.04637285    0.023        * Wilcoxon
2   1.0   len     OJ     VC 0.004030367 0.01209110    0.004       ** Wilcoxon
3   2.0   len     OJ     VC 1.000000000 1.00000000    1.000       ns Wilcoxon
# A tibble: 3 x 8
    .y. group1 group2            p        p.adj p.format p.signif   method
  <chr>  <chr>  <chr>        <dbl>        <dbl>    <chr>    <chr>    <chr>
1   len    0.5      1 7.020855e-06 1.404171e-05  7.0e-06     **** Wilcoxon
2   len    0.5      2 8.406447e-08 2.521934e-07  8.4e-08     **** Wilcoxon
3   len      1      2 1.772382e-04 1.772382e-04  0.00018      *** Wilcoxon
# A tibble: 2 x 8
    .y. group1 group2            p        p.adj p.format p.signif   method
  <chr>  <chr>  <chr>        <dbl>        <dbl>    <chr>    <chr>    <chr>
1   len    0.5      1 7.020855e-06 7.020855e-06  7.0e-06     **** Wilcoxon
2   len    0.5      2 8.406447e-08 1.681289e-07  8.4e-08     **** Wilcoxon
# A tibble: 3 x 8
    .y. group1 group2            p        p.adj p.format p.signif   method
  <chr>  <chr>  <chr>        <dbl>        <dbl>    <chr>    <chr>    <chr>
1   len  .all.    0.5 5.078788e-05 0.0001523636  5.1e-05     **** Wilcoxon
2   len  .all.      1 7.640429e-01 0.7640429386  0.76404       ns Wilcoxon
3   len  .all.      2 1.791243e-04 0.0003582486  0.00018      *** Wilcoxon
# A tibble: 1 x 6
    .y.            p        p.adj p.format p.signif method
  <chr>        <dbl>        <dbl>    <chr>    <chr>  <chr>
1   len 9.532727e-16 9.532727e-16  9.5e-16     ****  Anova
# A tibble: 1 x 6
    .y.            p        p.adj p.format p.signif         method
  <chr>        <dbl>        <dbl>    <chr>    <chr>          <chr>
1   len 1.475207e-09 1.475207e-09  1.5e-09     **** Kruskal-Wallis

ggpubr documentation built on Nov. 15, 2018, 9:04 a.m.