Calculate associations between variables and principal components
Source:R/dim-reduction.R
associate_components.Rd
Calculate associations between metadata variables and PCA rotations. This
function is inspired by methylKit::assocComp
but is designed to work
with arbitrary input data.
Value
data.frame with rows for each metadata variable, columns for each PC, and p-values from the given test in cells.
Details
This function returns the p-values from testing the association
between a given metadata column and all PC rotations. For numeric values the
p-value returned is computed using the cor.test()
function. For
factor variables and variables that can be converted to factor variables the
function will return p-values from the wilcox.test()
function or the
kruskal.test()
function (when the metadata variable has > 2 levels).
Examples
# Specify metadata
metadata <- data.frame(
age = c(30, 80, 34, 30, 80, 40),
treatment = factor(c(rep("Treatment", 3), rep("Control", 3))),
class = factor(c(rep("A", 2), rep("B", 2), rep("C", 2))),
row.names = c(paste0("trt", 1:3), paste0("ctrl", 1:3))
)
# Create values to perform PCA on
df <- data.frame(replicate(6, runif(1000, 0, 100)))
colnames(df) <- c(paste0("trt", 1:3), paste0("ctrl", 1:3))
# Test for associations
res <- associate_components(df, metadata)
# Show results
head(res)
#> PC1 PC2 PC3 PC4 PC5 PC6
#> age 0.3669306 0.6047674 0.2282258 0.6979298 0.4756648 0.1893065
#> treatment 1.0000000 0.7000000 1.0000000 0.4000000 0.4000000 0.1000000
#> class 0.5647181 0.3678794 0.5647181 1.0000000 0.1017014 0.1800923