pca.factor.analysis: PCA factor analysis and annotation enrichment

View source: R/pca.factor.analysis.R

pca.factor.analysisR Documentation

PCA factor analysis and annotation enrichment

Description

This function performs (1) a factor analysis on numeric data and PC loadings derived from said data, then subsequently (3) performs a hypergeometrix enrichment test to ask if a certain class of variables are enriched on a particular PC.

Usage

pca.factor.analysis(
  metabolitedata,
  pcloadings,
  sigthreshold = 0.3,
  feature_anno = feature_data$SUPER_PATHWAY
)

Arguments

metabolitedata

a matrix or data frame of metabolite data

pcloadings

a matrix or data frame of pc loadings you wish to test

sigthreshold

Spearman's rho correlation threshold to declare an association between the numeric variable and a PC loading

feature_anno

a vector of variable annotations to perform the hypergeomtric enrichment on

Value

a list object of length 2: (1) a list object of enrichment_tables, and (2) the Spearman's correlation matrix between features and the PC loadings

Examples

## define a covariance matrix
cmat = matrix(1, 4, 4 )
cmat[1,] = c(1, 0.8, 0.6, 0.2)
cmat[2,] = c(0.8, 1, 0.7, 0.5)
cmat[3,] = c(0.6, 0.7, 1, 0.6)
cmat[4,] = c(0.2, 0.5, 0.6,1)
## simulate some correlated data (multivariable random normal)
set.seed(1110)
d1 = MASS::mvrnorm(n = 250, mu = c(5, 45, 25, 15), Sigma = cmat )
set.seed(1010)
d2 = MASS::mvrnorm(n = 250, mu = c(5, 45, 25, 15), Sigma = cmat )
## simulate some random data
d3 = sapply(1:20, function(x){ rnorm(250, 40, 5) })
ex_data = cbind(d1,d2,d3)
rownames(ex_data) = paste0("ind", 1:nrow(ex_data))
colnames(ex_data) = paste0("var", 1:ncol(ex_data))
## annotation
met_anno = c( rep("A", 10), rep("B", 10), rep("C", 8) )
## PCA
pca = prcomp(ex_data, center = TRUE, scale = TRUE)
## run pca.factor.analysis()
ex_out = pca.factor.analysis(metabolitedata = ex_data, 
                             pcloadings = pca$x[,1:5], 
                             sigthreshold = 0.3, 
                             feature_anno = met_anno )



MRCIEU/metaboprep documentation built on Jan. 28, 2023, 7:29 p.m.