bed.loadings | R Documentation |
Compute the loadings corresponding to given PCs.
bed.loadings(x, pc)
x |
A |
pc |
A matrix with Principal Components in columns |
A matrix with the corresponding loadings in columns.
Hervé Perdry and Claire Dandine-Roulland
# load chr2 data set (~10k SNPs in low LD)
x <- read.bed.matrix( system.file("extdata", "chr2.bed", package="gaston") )
# Compute Genetic Relationship Matrix
standardize(x) <- "p"
K <- GRM(x)
# Eigen decomposition
eiK <- eigen(K)
# deal with small negative eigen values
eiK$values[ eiK$values < 0 ] <- 0
# Note: the eigenvectors are normalized, to compute 'true' PCs
# multiply them by the square root of the associated eigenvalues
PC <- sweep(eiK$vectors, 2, sqrt(eiK$values), "*")
# Compute loadings for the 2 first PCs
# one can use PC[,1:2] instead of eiK$vectors[,1:2] as well
L <- bed.loadings(x, eiK$vectors[,1:2])
dim(L)
head(L)
# the loadings are normalized
colSums(L**2)
# Verify that these are loadings
head( (x %*% L) / sqrt(ncol(x)-1) )
head( PC[,1:2] )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.