compare_groups {metacoder} | R Documentation |
Compare groups of samples
Description
Apply a function to compare data, usually abundance, from pairs of
treatments/groups. By default, every pairwise combination of treatments are
compared. A custom function can be supplied to perform the comparison. The
plotting function heat_tree_matrix
is useful for visualizing
these results.
Usage
compare_groups(
obj,
data,
cols,
groups,
func = NULL,
combinations = NULL,
other_cols = FALSE,
dataset = NULL
)
Arguments
obj |
A |
data |
The name of a table in |
cols |
The names/indexes of columns in
|
groups |
A vector defining how samples are grouped into "treatments". Must be the same
order and length as |
func |
The function to apply for each comparison. For each row in
function(abund_1, abund_2) { log_ratio <- log2(median(abund_1) / median(abund_2)) if (is.nan(log_ratio)) { log_ratio <- 0 } list(log2_median_ratio = log_ratio, median_diff = median(abund_1) - median(abund_2), mean_diff = mean(abund_1) - mean(abund_2), wilcox_p_value = wilcox.test(abund_1, abund_2)$p.value) } |
combinations |
Which combinations of groups to use. Must be a list of vectors, each containing the names of 2 groups to compare. By default, all pairwise combinations of groups are compared. |
other_cols |
If |
dataset |
DEPRECIATED. use "data" instead. |
Value
A tibble
See Also
Other calculations:
calc_diff_abund_deseq2()
,
calc_group_mean()
,
calc_group_median()
,
calc_group_rsd()
,
calc_group_stat()
,
calc_n_samples()
,
calc_obs_props()
,
calc_prop_samples()
,
calc_taxon_abund()
,
counts_to_presence()
,
rarefy_obs()
,
zero_low_counts()
Examples
## Not run:
# Parse data for plotting
x = parse_tax_data(hmp_otus, class_cols = "lineage", class_sep = ";",
class_key = c(tax_rank = "taxon_rank", tax_name = "taxon_name"),
class_regex = "^(.+)__(.+)$")
# Convert counts to proportions
x$data$otu_table <- calc_obs_props(x, data = "tax_data", cols = hmp_samples$sample_id)
# Get per-taxon counts
x$data$tax_table <- calc_taxon_abund(x, data = "otu_table", cols = hmp_samples$sample_id)
# Calculate difference between groups
x$data$diff_table <- compare_groups(x, data = "tax_table",
cols = hmp_samples$sample_id,
groups = hmp_samples$body_site)
# Plot results (might take a few minutes)
heat_tree_matrix(x,
data = "diff_table",
node_size = n_obs,
node_label = taxon_names,
node_color = log2_median_ratio,
node_color_range = diverging_palette(),
node_color_trans = "linear",
node_color_interval = c(-3, 3),
edge_color_interval = c(-3, 3),
node_size_axis_label = "Number of OTUs",
node_color_axis_label = "Log2 ratio median proportions")
# How to get results for only some pairs of groups
compare_groups(x, data = "tax_table",
cols = hmp_samples$sample_id,
groups = hmp_samples$body_site,
combinations = list(c('Nose', 'Saliva'),
c('Skin', 'Throat')))
## End(Not run)