associate_components: Calculate associations between variables and principal...

View source: R/dim-reduction.R

associate_componentsR Documentation

Calculate associations between variables and principal components

Description

Calculate associations between metadata variables and PCA rotations. This function is inspired by methylKit::assocComp but is designed to work with arbitrary input data.

Usage

associate_components(x, metadata, N = 10, ...)

Arguments

x

data.frame or matrix of values passed to prcomp()

metadata

data.frame of metadata variables to test associations. rownames(metadata) must be identical to colnames(x).

N

First N PCs to test associations between. Default 10.

...

Additional arguments passed to the prcomp() function.

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

Value

data.frame with rows for each metadata variable, columns for each PC, and p-values from the given test in cells.

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)

jcalendo/coriell documentation built on March 5, 2025, 5:42 a.m.