Cox Mixed-Effects Models for Genome-Wide Association Studies

Liang He

2020-05-05

Overview

Time-to-event is one of the most important phenotypes in genetic epidemiology. The R-package, “coxmeg”, provides a set of utilities to fit a Cox mixed-effects model and to efficiently perform genome-wide association analysis of time-to-event phenotypes using a Cox mixed-effects model. More details can be found in (He and Kulminski 2020).

Installation

Most-recent version

Functions

The current version provides four functions.

Fit a Cox mixed-effects model with a sparse relatedness matrix

We illustrate how to use coxmeg to fit a Cox mixed-effects model with a sparse relatedness matrix. We first simulate a block-diagonal relatedness matrix for a cohort consisting of 200 families, each of which has five members.

library(coxmeg)
## Loading required package: Rcpp
library(MASS)
library(Matrix)
n_f <- 200
mat_list <- list()
size <- rep(5,n_f)
offd <- 0.5
for(i in 1:n_f)
{
  mat_list[[i]] <- matrix(offd,size[i],size[i])
  diag(mat_list[[i]]) <- 1
}
sigma <- as.matrix(bdiag(mat_list))
sigma = as(sigma,'dgCMatrix')

We use ‘dgCMatrix’ to save memory. Next, we simulate random effects and time-to-event outcomes assuming a constant baseline hazard function. We assume that the variance component is 0.2. We also simulate a risk factor with log(HR)=0.1.

n = nrow(sigma)
tau_var <- 0.2
x <- mvrnorm(1, rep(0,n), tau_var*sigma)
pred = rnorm(n,0,1)
myrates <- exp(x+0.1*pred-1)
y <- rexp(n, rate = myrates)
cen <- rexp(n, rate = 0.02 )
ycen <- pmin(y, cen)
outcome <- cbind(ycen,as.numeric(y <= cen))
head(outcome)
##             ycen  
## [1,] 10.21075432 1
## [2,]  2.08420109 1
## [3,]  5.80418750 1
## [4,]  0.07548893 1
## [5,] 13.27873907 1
## [6,]  0.58854789 1
sigma[1:5,1:5]
## 5 x 5 sparse Matrix of class "dgCMatrix"
##                         
## [1,] 1.0 0.5 0.5 0.5 0.5
## [2,] 0.5 1.0 0.5 0.5 0.5
## [3,] 0.5 0.5 1.0 0.5 0.5
## [4,] 0.5 0.5 0.5 1.0 0.5
## [5,] 0.5 0.5 0.5 0.5 1.0

We fit a Cox mixed-effects model using the coxmeg function. Here, we set type='bd' because the relatedness matrix is a block-diagonal matrix. Note that type='bd' should be used only for a block-diagonal matrix or a sparse matrix of which its inverse matrix is also sparse. For a general sparse relatedness matrix of which its inverse is not sparse, it is recommended that type='sparse' be used. When type='sparse' is specified, the relatedness matrix will not be inverted during the estimation procedure. The function will automatically treat the relatedness matrix as dense if there are more than 50% non-zero elements in the matrix.

re = coxmeg(outcome,sigma,type='bd',X=pred,order=1,detap='diagonal')
## Remove 0 subjects censored before the first failure.
## There is/are 1 predictors. The sample size included is 1000.
## The relatedness matrix is treated as sparse.
## The relatedness matrix is inverted.
## The method for computing the determinant is 'diagonal'.
## Solver: Cholesky decomposition (RcppEigen=TRUE).
re
## $beta
## [1] 0.05645472
## 
## $HR
## [1] 1.058079
## 
## $sd_beta
## [1] 0.03886043
## 
## $p
## [1] 0.1462915
## 
## $tau
## [1] 0.2102882
## 
## $iter
## [1] 18
## 
## $rank
## [1] 1000
## 
## $nsam
## [1] 1000
## 
## $int_ll
## [1] 11450.82

In the above result, tau is the estimated variance component, and int_ll is -2*log(lik) of the integrated/marginal likelihood of tau.

We give more details about specifying order and detap. We set order=1 (also by default) to use the first-order approximation of the inverse Hessian matrix in the optimization. By detap='diagonal', we tell coxmeg to use a diagonal approximation to compute the determinant, which is much faster under this setting, when estimating the variance component. By default (detap='NULL'), coxmeg will automatically select a method for computing the determinant based on type, the sample size, and whether the relatedness matrix is symmetric positive definite (SPD).

It should be noted that when the relatedness matrix is SPD, coxmeg will make use of the sparsity by setting type='sparse' or type='bd' regardless of whether the relatedness matrix or its inverse is sparse. However, when the relatedness matrix is symmetric positive semidefinite (SPSD), coxmeg can make use of the sparsity only when its inverse is sparse. When the relatedness matrix is SPSD and its inverse is dense, setting type='sparse' may result in worse performance. In such a case, it would be better to use type='dense' or to convert the relatedness matrix to SPD or block-diagonal if possible.

We compare the results with coxme, which are slightly different due to different approximation of the log-determinant used in the estimation of the variance component. Also, the integrated log-likelihoods cannot be compared directly because different approximation of log-determinant is used.

library(coxme)
## Loading required package: survival
## Loading required package: bdsmatrix
## 
## Attaching package: 'bdsmatrix'
## The following object is masked from 'package:base':
## 
##     backsolve
bls <- c(1)
for(i in (size[1]-1):1)
{bls <- c(bls, c(rep(offd,i),1))}
tmat <- bdsmatrix(blocksize=size, blocks=rep(bls,n_f),dimnames=list(as.character(1:n),as.character(1:n)))
re_coxme = coxme(Surv(outcome[,1],outcome[,2])~as.matrix(pred)+(1|as.character(1:n)), varlist=list(tmat),ties='breslow')
re_coxme
## Cox mixed-effects model fit by maximum likelihood
## 
##   events, n = 940, 1000
##   Iterations= 7 34 
##                     NULL Integrated    Fitted
## Log-likelihood -5564.321  -5557.906 -5408.671
## 
##                    Chisq     df          p   AIC     BIC
## Integrated loglik  12.83   2.00 1.6377e-03  8.83   -0.86
##  Penalized loglik 311.30 136.55 1.1102e-15 38.21 -623.47
## 
## Model:  Surv(outcome[, 1], outcome[, 2]) ~ as.matrix(pred) + (1 | as.character(1:n)) 
## Fixed coefficients
##                       coef exp(coef)   se(coef)    z    p
## as.matrix(pred) 0.05650983  1.058137 0.03890067 1.45 0.15
## 
## Random effects
##  Group             Variable Std Dev   Variance 
##  as.character.1.n. Vmat.1   0.4606879 0.2122333

In GWAS, we may split the procedure into two separate steps, (1) estimate the variance component under the null model, and (2) estimate the coefficients for the predictors using the estimated variance component. This can be carried out in the following way.

re = coxmeg(outcome,sigma,type='bd',order=1,detap='diagonal')
## Remove 0 subjects censored before the first failure.
## There is/are 0 predictors. The sample size included is 1000.
## The relatedness matrix is treated as sparse.
## The relatedness matrix is inverted.
## The method for computing the determinant is 'diagonal'.
## Solver: Cholesky decomposition (RcppEigen=TRUE).
tau = re$tau
print(tau)
## [1] 0.2086507
re2 = fit_ppl(pred,outcome,sigma,type='bd',tau=tau,order=1)
## Remove 0 subjects censored before the first failure.
## The sample size included is 1000.
## The relatedness matrix is treated as sparse.
## The relatedness matrix is inverted.
## Solver: Cholesky decomposition (RcppEigen=TRUE).
re2
## $beta
## [1] 0.05641035
## 
## $HR
## [1] 1.058032
## 
## $sd_beta
## [1] 0.03883509
## 
## $p
## [1] 0.1463456
## 
## $iter
## [1] 4
## 
## $ppl
##           [,1]
## [1,] -5478.329

Perform GWAS of an age-at-onset phenotype with a sparse relatedness matrix

We illustrate how to perform a GWAS using the coxmeg_plink function. This function supports plink bed files. We provide example files in the package. The example plink files include 20 SNPs and 3000 subjects from 600 families. The following code performs a GWAS for all SNPs in the example bed files. The coxmeg_plink function will write a temporary .gds file for the SNPs in the folder specified by tmp_dir. The user needs to specify a tmp_dir to store the temporary file when bed is provided. The temporary file is removed after the analysis is done.

library(coxmeg)
bed = system.file("extdata", "example_null.bed", package = "coxmeg")
bed = substr(bed,1,nchar(bed)-4)
pheno = system.file("extdata", "ex_pheno.txt", package = "coxmeg")
cov = system.file("extdata", "ex_cov.txt", package = "coxmeg")

## building a relatedness matrix
n_f <- 600
mat_list <- list()
size <- rep(5,n_f)
offd <- 0.5
for(i in 1:n_f)
{
  mat_list[[i]] <- matrix(offd,size[i],size[i])
  diag(mat_list[[i]]) <- 1
}
sigma <- as.matrix(bdiag(mat_list))

re = coxmeg_plink(pheno,sigma,type='bd',bed=bed,tmp_dir=tempdir(),cov_file=cov,verbose=FALSE)
## Excluding 0 SNP on non-autosomes
## Excluding 0 SNP (monomorphic: TRUE, MAF: 0.05, missing rate: 0)
re
## $summary
##     snp.id chromosome position allele      afreq  afreq_inc   index
## 1   null_0          1        1    d/D 0.30983333 0.30983333  null_0
## 2   null_1          1        2    d/D 0.23466667 0.23466667  null_1
## 3   null_2          1        3    D/d 0.14033333 0.14033333  null_2
## 4   null_3          1        4    D/d 0.16183333 0.16183333  null_3
## 5   null_4          1        5    d/D 0.19933333 0.19933333  null_4
## 6   null_5          1        6    D/d 0.11800000 0.11800000  null_5
## 7   null_6          1        7    d/D 0.09483333 0.09483333  null_6
## 8   null_7          1        8    D/d 0.49683333 0.49683333  null_7
## 9   null_8          1        9    d/D 0.31366667 0.31366667  null_8
## 10  null_9          1       10    D/d 0.49183333 0.49183333  null_9
## 11 null_10          1       11    d/D 0.34833333 0.34833333 null_10
## 12 null_11          1       12    D/d 0.25100000 0.25100000 null_11
## 13 null_12          1       13    d/D 0.17500000 0.17500000 null_12
## 14 null_13          1       14    D/d 0.06333333 0.06333333 null_13
## 15 null_14          1       15    D/d 0.20833333 0.20833333 null_14
## 16 null_15          1       16    d/D 0.17050000 0.17050000 null_15
## 17 null_16          1       17    D/d 0.33550000 0.33550000 null_16
## 18 null_17          1       18    d/D 0.26633333 0.26633333 null_17
## 19 null_18          1       19    D/d 0.09433333 0.09433333 null_18
## 20 null_19          1       20    d/D 0.11650000 0.11650000 null_19
##            beta        HR    sd_beta           p
## 1   0.015672101 1.0157956 0.02938524 0.593803537
## 2   0.019439150 1.0196293 0.03222054 0.546298835
## 3  -0.049845757 0.9513762 0.03860368 0.196628160
## 4   0.044130767 1.0451190 0.03701019 0.233106387
## 5   0.028473176 1.0288824 0.03432500 0.406811816
## 6  -0.114319159 0.8919732 0.04234095 0.006934636
## 7  -0.017981231 0.9821795 0.04655562 0.699325464
## 8  -0.004207897 0.9958009 0.02717805 0.876957699
## 9  -0.063741849 0.9382472 0.02958441 0.031195036
## 10 -0.008409562 0.9916257 0.02730686 0.758108827
## 11 -0.013581479 0.9865103 0.02859980 0.634872392
## 12  0.037508301 1.0382206 0.03113254 0.228282858
## 13 -0.017215848 0.9829315 0.03628637 0.635183349
## 14 -0.068207724 0.9340664 0.05698849 0.231357835
## 15 -0.013965386 0.9861317 0.03431600 0.684034201
## 16  0.002172773 1.0021751 0.03685682 0.952990554
## 17  0.004762350 1.0047737 0.02859957 0.867749134
## 18  0.001786995 1.0017886 0.03098518 0.954009439
## 19 -0.016052310 0.9840758 0.04731969 0.734435643
## 20 -0.022398126 0.9778508 0.04231689 0.596600710
## 
## $tau
## [1] 0.04028041
## 
## $rank
## [1] 3000
## 
## $nsam
## [1] 3000

The above code first retrieves the full path of the files. If the full path is not given, coxmeg_plink will search the current working directory. The file name of the bed file should not include the suffix (.bed). The phenotype and covariate files have the same format as used in plink, and the IDs must be consistent with the bed files. Specifically, the phenotype file should include four columns including family ID, individual ID, time, and status. The covariate file always starts with two columns, family ID and individual ID. Missing values in the phenotype and covariate files are denoted by -9 and NA, respectively. In the current version, the coxmeg_plink function does not impute genotypes itself, and only SNPs without missing values will be analyzed, so it will be better to use imputed genotype data.

The coxmeg_plink function fist estimates the variance component with only the covariates, and then uses it to analyze each SNP after filtering. These two steps can be done separately as follows. The first command without bed only esitmates the variance component tau, and the second command uses the estimated tau to analyze the SNPs.

re = coxmeg_plink(pheno,sigma,type='bd',cov_file=cov,verbose=FALSE)
re = coxmeg_plink(pheno,sigma,type='bd',bed=bed,tmp_dir=tempdir(),tau=re$tau,cov_file=cov,verbose=FALSE)

When the genotypes of a group of SNPs are stored in a matrix, the function coxmeg_m can be used to perform GWAS for each of the SNPs. Similarly, coxmeg_m first estimates the variance component without the SNPs. In the following example, we simulate 10 independent SNPs, and use coxmeg_m to perform association analysis.

geno = matrix(rbinom(nrow(sigma)*10,2,runif(nrow(sigma)*10,0.05,0.5)),nrow(sigma),10)
pheno_m = read.table(pheno)
re = coxmeg_m(geno,pheno_m[,3:4],sigma,type='bd',verbose=FALSE)
re
## $summary
##            beta        HR    sd_beta         p
## 1  -0.001421832 0.9985792 0.03004193 0.9622516
## 2   0.000274503 1.0002745 0.02946136 0.9925659
## 3   0.035435498 1.0360708 0.02932110 0.2268426
## 4   0.003821789 1.0038291 0.03009558 0.8989496
## 5   0.017551274 1.0177062 0.02932354 0.5494805
## 6   0.022647735 1.0229061 0.02912362 0.4367804
## 7  -0.004917531 0.9950945 0.02947971 0.8675190
## 8   0.012820447 1.0129030 0.02926799 0.6613598
## 9   0.038858044 1.0396229 0.02982924 0.1926830
## 10 -0.018191099 0.9819734 0.02876464 0.5271177
## 
## $tau
## [1] 0.04052206
## 
## $rank
## [1] 3000
## 
## $nsam
## [1] 3000

By default, coxmeg_m and coxmeg_plink will choose an optimal order between 1 and 10 for analyzing the SNPs when order is not specified.

Perform GWAS of an age-at-onset phenotype with a dense relatedness matrix

When the relatedness matrix is dense and large (>5000), type='dense' should be used. In thise case, it will be more efficient to use preconditioned conjugate gradiant (PCG) (e.g., by explicitly specifying solver=2) and stochastic lanczos quadrature (SLQ) detap='slq' in the optimization. These can be specified as follows.

re = coxmeg_plink(pheno,sigma,type='dense',bed=bed,tmp_dir=tempdir(),cov_file=cov,detap='slq',verbose=FALSE,solver=2)
## Excluding 0 SNP on non-autosomes
## Excluding 0 SNP (monomorphic: TRUE, MAF: 0.05, missing rate: 0)
re
## $summary
##     snp.id chromosome position allele      afreq  afreq_inc   index
## 1   null_0          1        1    d/D 0.30983333 0.30983333  null_0
## 2   null_1          1        2    d/D 0.23466667 0.23466667  null_1
## 3   null_2          1        3    D/d 0.14033333 0.14033333  null_2
## 4   null_3          1        4    D/d 0.16183333 0.16183333  null_3
## 5   null_4          1        5    d/D 0.19933333 0.19933333  null_4
## 6   null_5          1        6    D/d 0.11800000 0.11800000  null_5
## 7   null_6          1        7    d/D 0.09483333 0.09483333  null_6
## 8   null_7          1        8    D/d 0.49683333 0.49683333  null_7
## 9   null_8          1        9    d/D 0.31366667 0.31366667  null_8
## 10  null_9          1       10    D/d 0.49183333 0.49183333  null_9
## 11 null_10          1       11    d/D 0.34833333 0.34833333 null_10
## 12 null_11          1       12    D/d 0.25100000 0.25100000 null_11
## 13 null_12          1       13    d/D 0.17500000 0.17500000 null_12
## 14 null_13          1       14    D/d 0.06333333 0.06333333 null_13
## 15 null_14          1       15    D/d 0.20833333 0.20833333 null_14
## 16 null_15          1       16    d/D 0.17050000 0.17050000 null_15
## 17 null_16          1       17    D/d 0.33550000 0.33550000 null_16
## 18 null_17          1       18    d/D 0.26633333 0.26633333 null_17
## 19 null_18          1       19    D/d 0.09433333 0.09433333 null_18
## 20 null_19          1       20    d/D 0.11650000 0.11650000 null_19
##            beta        HR    sd_beta           p
## 1   0.015352311 1.0154708 0.02927876 0.600035322
## 2   0.019289830 1.0194771 0.03209994 0.547886378
## 3  -0.049213986 0.9519774 0.03847053 0.200803912
## 4   0.043977623 1.0449590 0.03687021 0.232960102
## 5   0.028322704 1.0287276 0.03419633 0.407535608
## 6  -0.113854108 0.8923881 0.04217857 0.006947814
## 7  -0.018342795 0.9818244 0.04639545 0.692579002
## 8  -0.004169125 0.9958396 0.02707614 0.877627225
## 9  -0.063679679 0.9383055 0.02947557 0.030739973
## 10 -0.008398538 0.9916366 0.02721315 0.757610150
## 11 -0.013534231 0.9865569 0.02849337 0.634789988
## 12  0.037074697 1.0377705 0.03101927 0.232003067
## 13 -0.017161741 0.9829847 0.03614966 0.634971320
## 14 -0.068087053 0.9341791 0.05678522 0.230517155
## 15 -0.014220388 0.9858802 0.03418702 0.677440204
## 16  0.002326025 1.0023287 0.03672381 0.949497091
## 17  0.004819686 1.0048313 0.02849229 0.865672733
## 18  0.001444992 1.0014460 0.03087821 0.962675411
## 19 -0.016068012 0.9840604 0.04715020 0.733266573
## 20 -0.022148630 0.9780948 0.04216401 0.599376699
## 
## $tau
## [1] 0.03535305
## 
## $rank
## [1] 3000
## 
## $nsam
## [1] 3000

If solver is not specified, coxmeg_plink will by default choose PCG as a solver when type='dense'. If detap is not specified, coxmeg_plink will by default use detap='slq' for a dense matrix when the sample size exceeds 3000. The number of Monte Carlo samples in the SLQ can be specified by mc (by default mc=100).

The above command estimates HRs and reports p-values. Instead, a score test, which is computationally much more efficient, can be used by specifying score=TRUE.

re = coxmeg_plink(pheno,sigma,type='dense',bed=bed,tmp_dir=tempdir(),tau=re$tau,cov_file=cov,detap='slq',verbose=FALSE,solver=2,score=TRUE)
## Excluding 0 SNP on non-autosomes
## Excluding 0 SNP (monomorphic: TRUE, MAF: 0.05, missing rate: 0)
re
## $summary
##     snp.id chromosome position allele      afreq  afreq_inc   index  score_test
## 1   null_0          1        1    d/D 0.30983333 0.30983333  null_0 0.274948425
## 2   null_1          1        2    d/D 0.23466667 0.23466667  null_1 0.361129138
## 3   null_2          1        3    D/d 0.14033333 0.14033333  null_2 1.636877777
## 4   null_3          1        4    D/d 0.16183333 0.16183333  null_3 1.422910275
## 5   null_4          1        5    d/D 0.19933333 0.19933333  null_4 0.686023093
## 6   null_5          1        6    D/d 0.11800000 0.11800000  null_5 7.295630403
## 7   null_6          1        7    d/D 0.09483333 0.09483333  null_6 0.156313850
## 8   null_7          1        8    D/d 0.49683333 0.49683333  null_7 0.023709163
## 9   null_8          1        9    d/D 0.31366667 0.31366667  null_8 4.669012717
## 10  null_9          1       10    D/d 0.49183333 0.49183333  null_9 0.095247662
## 11 null_10          1       11    d/D 0.34833333 0.34833333 null_10 0.225624257
## 12 null_11          1       12    D/d 0.25100000 0.25100000 null_11 1.428706644
## 13 null_12          1       13    d/D 0.17500000 0.17500000 null_12 0.225385317
## 14 null_13          1       14    D/d 0.06333333 0.06333333 null_13 1.438271485
## 15 null_14          1       15    D/d 0.20833333 0.20833333 null_14 0.173024293
## 16 null_15          1       16    d/D 0.17050000 0.17050000 null_15 0.004011749
## 17 null_16          1       17    D/d 0.33550000 0.33550000 null_16 0.028614359
## 18 null_17          1       18    d/D 0.26633333 0.26633333 null_17 0.002189943
## 19 null_18          1       19    D/d 0.09433333 0.09433333 null_18 0.116136972
## 20 null_19          1       20    d/D 0.11650000 0.11650000 null_19 0.275948679
##              p
## 1  0.600031477
## 2  0.547879809
## 3  0.200754388
## 4  0.232924979
## 5  0.407520377
## 6  0.006912251
## 7  0.692573193
## 8  0.877627240
## 9  0.030711578
## 10 0.757608932
## 11 0.634787531
## 12 0.231975631
## 13 0.634966861
## 14 0.230419257
## 15 0.677437891
## 16 0.949497062
## 17 0.865672528
## 18 0.962675159
## 19 0.733262621
## 20 0.599368975
## 
## $tau
## [1] 0.03535305
## 
## $rank
## [1] 3000
## 
## $nsam
## [1] 3000

In the results, the column score_test is the score test statistics, which follow a chi-sq distribution.

Handle positive semidefinite relatedness matrices

We now assume that the first two subjects in the sample are monozygotic twins. In this case, the relatedness matrix becomes positive semidefinite. Specifying spd=FALSE will tell coxmeg_plink to handle a positive semidefinite relatedness matrix.

sigma[2,1] = sigma[1,2] = 1
re = coxmeg_plink(pheno,sigma,type='bd',cov_file=cov,verbose=FALSE,spd=FALSE)
## Warning in chol.default(x, pivot = TRUE): the matrix is either rank-deficient or
## indefinite
re
## $tau
## [1] 0.04024134
## 
## $iter
## [1] 15
## 
## $rank
## [1] 3000
## 
## $nsam
## [1] 3000

The warning indicates that the relatedness matrix is not full rank. Because there is a twin pair in the sample, the rank of the relatedness matrix is less than the sample size. If the user is not sure whether the relatedness matrix is positive definite or positive semidefinite, it is better to use spd=FALSE although it is slower because coxmeg will perform a eigenvalue decomposition under this setting. In the current version, instead of using the previously proposed GPPL in (He and Kulminski 2020), coxmeg uses a modified PPL by turning all zero eigenvalues of the relatedness matrix to a small value (1e-6). This modification makes coxmeg suitable for twin cohorts.

He, Liang, and Alexander M. Kulminski. 2020. “Fast Algorithms for Conducting Large-Scale GWAS of Age-at-Onset Traits Using Cox Mixed-Effects Models.” Genetics, March, genetics.302940.2019. https://doi.org/10.1534/genetics.119.302940.