Performs PCoA on a distance matrix (typically from
compute_distance()), optionally correcting for negative eigenvalues,
and returns coordinates, eigenvalues, variance explained, and feature-axis
associations.
Usage
compute_pcoa(
dist_obj,
neg_correction = c("none", "lingoes", "cailliez"),
n_axes = 5L
)Arguments
- dist_obj
a
distobject (for example returned bycompute_distance()). The normalized abundance matrix used to compute the distances is attached as attribute"abundances"(numeric matrix with samples in rows and features in columns). If missing, feature associations are skipped.- neg_correction
character scalar. Method for adjusting negative eigenvalues (if any). One of
"none","lingoes", or"cailliez". Default is"none".- n_axes
integer scalar. Number of PCoA axes to return in the sample scores. Must be > 0. Internally,
k = min(n_axes, n_samples - 1).
Value
a list of class "beta_pcoa" with elements:
sample_coords: tibble withsample_idand columnsPCoA1,PCoA2, ... up ton_axes(or fewer ifn_samples - 1is smaller).eigenvalues: numeric vector of eigenvalues from the PCoA.var_explained: one-row tibble with percent variance explained by the returned axes and%Other. percentages are computed from the sum of positive eigenvalues.eigen_diagnostics: one-row tibble with eigenvalue diagnostics:sum_negative,sum_positive, their ratio, the minimum eigenvalue, and the number and fraction of negative eigenvalues.correction_infos: one-row tibble describing any negative eigenvalue correction applied, includingcorrected,correction_method,correction_add, andcorrection_note.
Details
Negative eigenvalues indicate that the distances are not perfectly euclidean.
If neg_correction is "lingoes" or "cailliez", a
correction is applied via vegan::wcmdscale(add = ...).
Examples
# \donttest{
# compute a distance matrix with an attached abundance matrix
# build an example <phip_data> object from the package example dataset
ps <- load_example_data("small_mixture")
# compute distances (needs either 'parallelDist' or 'vegan')
val_col <- "fold_change"
d <- compute_distance(
ps,
value_col = val_col,
distance = "jaccard",
n_threads = 2L
)
#> [13:24:34] INFO building abundance matrix from `ps` using `fold_change`.
#> [13:24:34] INFO building pivot spec (sample_id x peptide_id).
#> [13:24:34] INFO Collecting long table (sample_id, peptide_id, value).
#> -> compute_distance
#> [13:24:34] INFO Pivoting to wide abundance matrix in R.
#> -> compute_distance
#> [13:24:34] INFO abundance matrix has 43 samples and 5 features after
#> preprocessing.
#> [13:24:34] INFO auto normalization selected -> using relative
#> [13:24:34] INFO computing distance: jaccard
#> [13:24:34] INFO distance matrix computation complete.
pcoa_res <- compute_pcoa(d, neg_correction = "none", n_axes = 3L)
#> [13:24:34] INFO performing principal coordinates analysis
#> [13:24:34] INFO extracting sample coordinates.
#> [13:24:34] INFO summarizing eigenvalues and variance explained.
#> [13:24:34] INFO pcoa analysis complete.
pcoa_res$sample_coords
#> # A tibble: 43 × 4
#> sample_id PCoA1 PCoA2 PCoA3
#> <chr> <dbl> <dbl> <dbl>
#> 1 A_T1_1 -0.457 0.0104 0.287
#> 2 A_T1_10 -0.497 0.0145 0.241
#> 3 A_T1_11 -0.453 0.0109 -0.259
#> 4 A_T1_12 -0.519 0.0166 0.109
#> 5 A_T1_13 -0.500 0.0146 0.148
#> 6 A_T1_14 -0.414 0.00651 0.432
#> 7 A_T1_15 -0.344 -0.000307 0.491
#> 8 A_T1_16 -0.514 0.0164 -0.0622
#> 9 A_T1_17 -0.389 0.00417 -0.328
#> 10 A_T1_18 -0.412 0.00686 -0.112
#> # ℹ 33 more rows
pcoa_res$var_explained
#> # A tibble: 1 × 4
#> `%PCoA1` `%PCoA2` `%PCoA3` `%Other`
#> <dbl> <dbl> <dbl> <dbl>
#> 1 57.2 15.8 9.45 17.6
# }