Title: | GEMMA Multivariate Linear Mixed Model |
---|---|
Description: | Fits a multivariate linear mixed effects model that uses a polygenic term, after Zhou & Stephens (2014) (<https://www.nature.com/articles/nmeth.2848>). Of particular interest is the estimation of variance components with restricted maximum likelihood (REML) methods. Genome-wide efficient mixed-model association (GEMMA), as implemented in the package 'gemma2', uses an expectation-maximization algorithm for variance components inference for use in quantitative trait locus studies. |
Authors: | Frederick Boehm [aut, cre] |
Maintainer: | Frederick Boehm <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2025-01-27 04:27:05 UTC |
Source: | https://github.com/fboehm/gemma2 |
Calculate Omega matrices
calc_omega(eval, D_l)
calc_omega(eval, D_l)
eval |
vector of eigenvalues from decomposition of relatedness matrix |
D_l |
vector of length d_size |
list of length 2. First entry in the list is the symmetric matrix OmegaU. Second entry in the list is the symmetric matrix OmegaE.
calc_omega(eval = 50:1, D_l = runif(2))
calc_omega(eval = 50:1, D_l = runif(2))
Calculate Qi (inverse of Q) and log determinant of Q
calc_qi(eval, D_l, X)
calc_qi(eval, D_l, X)
eval |
vector of eigenvalues from decomposition of relatedness matrix |
D_l |
vector of length d_size |
X |
design matrix |
a list of length two. First entry in the list is a symmetric numeric matrix, Qi, the inverse of the Q matrix. The second entry in the outputted list is the log determinant of the matrix Q for use in likelihood calculations.
as.matrix(readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100]) -> kinship eigen2(kinship) -> e2_out e2_out$values -> eval e2_out$vectors -> U eigen_proc(V_g = diag(c(1.91352, 0.530827)), V_e = diag(c(0.320028, 0.561589))) -> ep_out calc_qi(eval = eval, D_l = ep_out[[4]], X = t(rep(1, 100)) %*% U)
as.matrix(readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100]) -> kinship eigen2(kinship) -> e2_out e2_out$values -> eval e2_out$vectors -> U eigen_proc(V_g = diag(c(1.91352, 0.530827)), V_e = diag(c(0.320028, 0.561589))) -> ep_out calc_qi(eval = eval, D_l = ep_out[[4]], X = t(rep(1, 100)) %*% U)
Calculate Sigma_ee and Sigma_uu matrices
calc_sigma(eval, D_l, X, OmegaU, OmegaE, UltVeh, Qi)
calc_sigma(eval, D_l, X, OmegaU, OmegaE, UltVeh, Qi)
eval |
eigenvalues vector from decomposition of relatedness matrix |
D_l |
vector |
X |
design matrix |
OmegaU |
matrix |
OmegaE |
matrix |
UltVeh |
matrix |
Qi |
inverse of Q matrix |
Calculate XHiY
calc_XHiY(eval, D_l, X, UltVehiY)
calc_XHiY(eval, D_l, X, UltVehiY)
eval |
vector of eigenvalues from the decomposition of the relatedness matrix |
D_l |
vector of length d_size |
X |
design matrix |
UltVehiY |
a matrix |
numeric vector
readr::read_tsv(system.file("extdata", "mouse100.pheno.txt", package = "gemma2"), col_names = FALSE) -> pheno phe16 <- as.matrix(pheno[, c(1, 6)]) as.matrix(readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100]) -> kinship eigen2(kinship) -> eout eout$values -> eval eout$vectors -> U UltVehi <- matrix(c(0, -1.76769, -1.334414, 0), nrow = 2, byrow = FALSE) # from output of eigen_proc() calc_XHiY(eval = eval, D_l = c(0.9452233, 5.9792268), X = rep(1, 100) %*% U, UltVehiY = UltVehi %*% t(phe16) %*% U )
readr::read_tsv(system.file("extdata", "mouse100.pheno.txt", package = "gemma2"), col_names = FALSE) -> pheno phe16 <- as.matrix(pheno[, c(1, 6)]) as.matrix(readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100]) -> kinship eigen2(kinship) -> eout eout$values -> eval eout$vectors -> U UltVehi <- matrix(c(0, -1.76769, -1.334414, 0), nrow = 2, byrow = FALSE) # from output of eigen_proc() calc_XHiY(eval = eval, D_l = c(0.9452233, 5.9792268), X = rep(1, 100) %*% U, UltVehiY = UltVehi %*% t(phe16) %*% U )
Center a relatedness matrix, after Zhou's GEMMA function CenterMatrix
center_kinship(mat)
center_kinship(mat)
mat |
a relatedness matrix |
a centered relatedness matrix
readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100] -> kinship e_out <- eigen2(as.matrix(kinship)) center_kinship(as.matrix(kinship)) -> kinship_centered
readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100] -> kinship e_out <- eigen2(as.matrix(kinship)) center_kinship(as.matrix(kinship)) -> kinship_centered
Eigendecomposition procedure for Vg and Ve
eigen_proc(V_g, V_e, tol = 1/10000)
eigen_proc(V_g, V_e, tol = 1/10000)
V_g |
a d_size by d_size covariance matrix |
V_e |
a d_size by d_size covariance matrix |
tol |
a positive number indicating the tolerance for isSymmetric |
a named list of length 4 containing the outputs of eigendecomposition procedure
eigen_proc(diag(2), diag(2))
eigen_proc(diag(2), diag(2))
Calculate eigendecomposition and return ordered eigenvalues and eigenvectors
eigen2(spd, decreasing = FALSE)
eigen2(spd, decreasing = FALSE)
spd |
a semi-positive definite matrix |
decreasing |
argument passed to order() |
a list with 2 components, the eigenvalues and the eigenvectors
readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100] -> kinship e_out <- eigen2(as.matrix(kinship))
readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100] -> kinship e_out <- eigen2(as.matrix(kinship))
We implement an expectation-maximization algorithm for multivariate variance components after the GEMMA software's algorithm.
Calculate log likelihood
MphCalcLogL(eval, D_l, Qi, UltVehiY, xHiy)
MphCalcLogL(eval, D_l, Qi, UltVehiY, xHiy)
eval |
eigenvalues vector from decomposition of relatedness matrix |
D_l |
vector of eigenvalues from decomposition of Ve matrix |
Qi |
inverse of Q matrix |
UltVehiY |
matrix of (transformed) Y values |
xHiy |
vector |
Perform expectation-maximization algorithm to infer Vg and Ve values for a pair of traits.
MphEM( max_iter = 10000, max_prec = 1/1e+06, eval, X, Y, V_g, V_e, verbose_output = FALSE )
MphEM( max_iter = 10000, max_prec = 1/1e+06, eval, X, Y, V_g, V_e, verbose_output = FALSE )
max_iter |
maximum number of iterations for EM algorithm |
max_prec |
maximum precision for EM algorithm |
eval |
vector of eigenvalues from relatedness matrix decomposition |
X |
design matrix. Typically contains founder allele dosages. |
Y |
matrix of phenotype values |
V_g |
genetic covariance matrix |
V_e |
error covariance matrix |
verbose_output |
logical indicating whether to output entire collection of intermediate values for all iterations. Default is FALSE. |
a list of lists. Length of list corresponds to number of EM iterations
Stagger matrices within a larger, block-diagonal matrix
stagger_mats(...)
stagger_mats(...)
... |
one or more matrices, separated by commas |
a block-diagonal matrix, with the inputted matrices as blocks on the diagonal.
foo <- matrix(rnorm(40000), ncol = 8) block_diag <- stagger_mats(foo, foo) dim(foo) dim(block_diag)
foo <- matrix(rnorm(40000), ncol = 8) block_diag <- stagger_mats(foo, foo) dim(foo) dim(block_diag)
Update E
update_e(UltVehiY, UltVehiBX, UltVehiU)
update_e(UltVehiY, UltVehiBX, UltVehiU)
UltVehiY |
matrix of transformed Y values |
UltVehiBX |
matrix of transformed BX values |
UltVehiU |
matrix of transformed U values |
Other expectation-maximization functions:
UpdateRL_B()
,
update_u()
,
update_v()
Update U matrix
update_u(OmegaE, UltVehiY, UltVehiBX)
update_u(OmegaE, UltVehiY, UltVehiBX)
OmegaE |
the OmegaE matrix, calculated in calc_omega |
UltVehiY |
matrix |
UltVehiBX |
matrix |
Other expectation-maximization functions:
UpdateRL_B()
,
update_e()
,
update_v()
readr::read_tsv(system.file("extdata", "mouse100.pheno.txt", package = "gemma2"), col_names = FALSE) -> pheno phe16 <- as.matrix(pheno[, c(1, 6)]) as.matrix(readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100]) -> kinship eigen2(kinship) -> e2_out e2_out$values -> eval e2_out$vectors -> U eigen_proc(V_g = diag(c(1.91352, 0.530827)), V_e = diag(c(0.320028, 0.561589))) -> ep_out UltVehi <- ep_out[[3]] calc_omega(eval, ep_out$D_l) -> co_out update_u(OmegaE = co_out[[2]], UltVehiY = UltVehi %*% t(phe16), UltVehiBX = matrix(c(-0.71342, -0.824482), ncol = 1) %*% t(rep(1, 100)) )
readr::read_tsv(system.file("extdata", "mouse100.pheno.txt", package = "gemma2"), col_names = FALSE) -> pheno phe16 <- as.matrix(pheno[, c(1, 6)]) as.matrix(readr::read_tsv(system.file("extdata", "mouse100.cXX.txt", package = "gemma2"), col_names = FALSE)[, 1:100]) -> kinship eigen2(kinship) -> e2_out e2_out$values -> eval e2_out$vectors -> U eigen_proc(V_g = diag(c(1.91352, 0.530827)), V_e = diag(c(0.320028, 0.561589))) -> ep_out UltVehi <- ep_out[[3]] calc_omega(eval, ep_out$D_l) -> co_out update_u(OmegaE = co_out[[2]], UltVehiY = UltVehi %*% t(phe16), UltVehiBX = matrix(c(-0.71342, -0.824482), ncol = 1) %*% t(rep(1, 100)) )
Update V_e and V_g
update_v(eval, U, E, Sigma_uu, Sigma_ee, tol = 1/10000)
update_v(eval, U, E, Sigma_uu, Sigma_ee, tol = 1/10000)
eval |
vector of eigenvalues from eigendecomposition of relatedness matrix |
U |
matrix |
E |
matrix |
Sigma_uu |
matrix |
Sigma_ee |
matrix |
tol |
a positive number indicating tolerance to be passed to isSymmetric() |
Other expectation-maximization functions:
UpdateRL_B()
,
update_e()
,
update_u()
Update B for restricted log likelihood
UpdateRL_B(xHiy, Qi, d_size)
UpdateRL_B(xHiy, Qi, d_size)
xHiy |
vector |
Qi |
Q inverse matrix |
d_size |
number of traits |
Other expectation-maximization functions:
update_e()
,
update_u()
,
update_v()