Last updated: 2024-06-06

Checks: 7 0

Knit directory: survival-susie/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20230201) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version f1d5b99. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/SPAcox.Rmd) and HTML (docs/SPAcox.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd f1d5b99 yunqiyang0215 2024-06-06 wflow_publish("analysis/SPAcox.Rmd")
html 1aa74aa yunqiyang0215 2023-09-11 Build site.
Rmd 0e6ac3b yunqiyang0215 2023-09-11 wflow_publish("analysis/SPAcox.Rmd")
html 18f9865 yunqiyang0215 2023-09-11 Build site.
Rmd f62866a yunqiyang0215 2023-09-11 wflow_publish("analysis/SPAcox.Rmd")
html 019f19a yunqiyang0215 2023-09-11 Build site.
Rmd 9eca4f1 yunqiyang0215 2023-09-11 wflow_publish("analysis/SPAcox.Rmd")
html a0c6a37 yunqiyang0215 2023-09-11 Build site.
Rmd 94b4b44 yunqiyang0215 2023-09-11 wflow_publish("analysis/SPAcox.Rmd")

1. Understand SPAcox

Result format: an R matrix with the following columns

  1. MAF: Minor allele frequencies

  2. missing.rate: Missing rates

  3. p.value.spa: p value (recommanded) from a saddlepoint approximation.

  4. p.value.norm: p value from a normal distribution approximation.

  5. Stat: score statistics

  6. Var: estimated variances of the score statistics

  7. z: z values corresponding to the score statistics

library(SPACox)
Loading required package: seqminer
Loading required package: data.table
library(survival)
# Simulation phenotype and genotype
set.seed(1)
N = 1e3
nSNP = 3
MAF = 0.1
Phen.mtx = data.frame(ID = paste0("IID-",1:N),
                      event=rbinom(N,1,0.5),
                      time=runif(N),
                      Cov1=rnorm(N),
                      Cov2=rbinom(N,1,0.5))

# First two SNPs with cor = 1
x1 = rnorm(N)
x2 = x1 + rnorm(N, sd = 1)
Geno.mtx = matrix(c(x1, x1, x2),N,nSNP)
cor(Geno.mtx)
          [,1]      [,2]      [,3]
[1,] 1.0000000 1.0000000 0.7441044
[2,] 1.0000000 1.0000000 0.7441044
[3,] 0.7441044 0.7441044 1.0000000
# NOTE: The row and column names of genotype matrix are required.
rownames(Geno.mtx) = paste0("IID-",1:N)
colnames(Geno.mtx) = paste0("SNP-",1:nSNP)
# Attach the survival package so that we can use its function Surv()
t1 = proc.time()
obj.null = SPACox_Null_Model(Surv(time,event)~Cov1+Cov2, data=Phen.mtx,
                             pIDs=Phen.mtx$ID, gIDs=rownames(Geno.mtx))
[1] "Start calculating empirical CGF for martingale residuals..."
[1] "Complete 1000/10000."
[1] "Complete 2000/10000."
[1] "Complete 3000/10000."
[1] "Complete 4000/10000."
[1] "Complete 5000/10000."
[1] "Complete 6000/10000."
[1] "Complete 7000/10000."
[1] "Complete 8000/10000."
[1] "Complete 9000/10000."
[1] "Complete 10000/10000."
SPACox.res = SPACox(obj.null, Geno.mtx)
[1] "Sample size is 1000."
[1] "Number of variants is 3."
[1] "Start Analyzing..."
[1] "2024-06-06 16:41:49 CDT"
[1] "Analysis Complete."
[1] "2024-06-06 16:41:49 CDT"
t2 = proc.time()

t2 - t1
   user  system elapsed 
  1.145   0.501   1.707 
# The below is an example code to use survival package
t1 = proc.time()
coxph(Surv(time,event)~Cov1+Cov2+Geno.mtx, data=Phen.mtx)
Call:
coxph(formula = Surv(time, event) ~ Cov1 + Cov2 + Geno.mtx, data = Phen.mtx)

                  coef exp(coef) se(coef)      z     p
Cov1          -0.05679   0.94479  0.04371 -1.299 0.194
Cov2          -0.10803   0.89760  0.09267 -1.166 0.244
Geno.mtxSNP-1  0.09007   1.09425  0.06721  1.340 0.180
Geno.mtxSNP-2       NA        NA  0.00000     NA    NA
Geno.mtxSNP-3 -0.03418   0.96640  0.04745 -0.720 0.471

Likelihood ratio test=4.18  on 4 df, p=0.3816
n= 1000, number of events= 480 
t2 = proc.time()

t2 - t1
   user  system elapsed 
  0.012   0.001   0.014 
# we recommand using column of 'p.value.spa' to associate genotype with time-to-event phenotypes
head(SPACox.res)
             MAF missing.rate p.value.spa p.value.norm     Stat       Var
SNP-1 0.02784507            0   0.2216493    0.2216493 27.86489  519.8315
SNP-2 0.02784507            0   0.2216493    0.2216493 27.86489  519.8315
SNP-3 0.01215918            0   0.6529406    0.6529406 14.59243 1053.0433
              z
SNP-1 1.2221541
SNP-2 1.2221541
SNP-3 0.4496809
coxph(Surv(time,event)~Cov1+Cov2+Geno.mtx[,1], data=Phen.mtx)
Call:
coxph(formula = Surv(time, event) ~ Cov1 + Cov2 + Geno.mtx[, 
    1], data = Phen.mtx)

                  coef exp(coef) se(coef)      z     p
Cov1          -0.05569   0.94583  0.04365 -1.276 0.202
Cov2          -0.10232   0.90274  0.09231 -1.108 0.268
Geno.mtx[, 1]  0.05345   1.05490  0.04387  1.218 0.223

Likelihood ratio test=3.67  on 3 df, p=0.2999
n= 1000, number of events= 480 
coxph(Surv(time,event)~Cov1+Cov2+Geno.mtx[,3], data=Phen.mtx)
Call:
coxph(formula = Surv(time, event) ~ Cov1 + Cov2 + Geno.mtx[, 
    3], data = Phen.mtx)

                  coef exp(coef) se(coef)      z     p
Cov1          -0.04969   0.95153  0.04337 -1.146 0.252
Cov2          -0.09478   0.90957  0.09211 -1.029 0.303
Geno.mtx[, 3]  0.01400   1.01410  0.03098  0.452 0.651

Likelihood ratio test=2.38  on 3 df, p=0.497
n= 1000, number of events= 480 

2.Speed comparison between SPAcox and coxph()

# Simulation phenotype and genotype
set.seed(1)
N = 10000
nSNP = 1000
MAF = 0.1
Phen.mtx = data.frame(ID = paste0("IID-",1:N),
                      event=rbinom(N,1,0.5),
                      time=runif(N),
                      Cov1=rnorm(N),
                      Cov2=rbinom(N,1,0.5))
Geno.mtx = matrix(rbinom(N*nSNP,2,MAF),N,nSNP)

# NOTE: The row and column names of genotype matrix are required.
rownames(Geno.mtx) = paste0("IID-",1:N)
colnames(Geno.mtx) = paste0("SNP-",1:nSNP)


t1 = proc.time()
obj.null = SPACox_Null_Model(Surv(time,event)~Cov1+Cov2, data=Phen.mtx,
                             pIDs=Phen.mtx$ID, gIDs=rownames(Geno.mtx))
[1] "Start calculating empirical CGF for martingale residuals..."
[1] "Complete 1000/10000."
[1] "Complete 2000/10000."
[1] "Complete 3000/10000."
[1] "Complete 4000/10000."
[1] "Complete 5000/10000."
[1] "Complete 6000/10000."
[1] "Complete 7000/10000."
[1] "Complete 8000/10000."
[1] "Complete 9000/10000."
[1] "Complete 10000/10000."
SPACox.res = SPACox(obj.null, Geno.mtx)
[1] "Sample size is 10000."
[1] "Number of variants is 1000."
[1] "Start Analyzing..."
[1] "2024-06-06 16:41:54 CDT"
[1] "Analysis Complete."
[1] "2024-06-06 16:41:56 CDT"
t2 = proc.time()
time.spa = t2 - t1


# The below is an example code to use survival package
t1 = proc.time()
coxph(Surv(time,event)~Cov1+Cov2+Geno.mtx[,1], data=Phen.mtx)
Call:
coxph(formula = Surv(time, event) ~ Cov1 + Cov2 + Geno.mtx[, 
    1], data = Phen.mtx)

                   coef exp(coef)  se(coef)      z     p
Cov1          -0.000685  0.999315  0.014318 -0.048 0.962
Cov2          -0.029170  0.971251  0.028430 -1.026 0.305
Geno.mtx[, 1]  0.008415  1.008450  0.032692  0.257 0.797

Likelihood ratio test=1.12  on 3 df, p=0.7722
n= 10000, number of events= 4953 
t2 = proc.time()
time.coxph = (t2 - t1)*nSNP
time.spa
   user  system elapsed 
  4.195   1.891   6.175 
time.coxph
   user  system elapsed 
     47      12      59 

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin20.6.0 (64-bit)
Running under: macOS Monterey 12.0.1

Matrix products: default
BLAS:   /usr/local/Cellar/openblas/0.3.18/lib/libopenblasp-r0.3.18.dylib
LAPACK: /usr/local/Cellar/r/4.1.1_1/lib/R/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] survival_3.2-11   SPACox_0.1.2      data.table_1.14.9 seqminer_9.1     
[5] workflowr_1.6.2  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     pillar_1.9.0     compiler_4.1.1   bslib_0.4.1     
 [5] later_1.3.0      jquerylib_0.1.4  git2r_0.28.0     tools_4.1.1     
 [9] digest_0.6.28    lattice_0.20-44  jsonlite_1.7.2   evaluate_0.14   
[13] lifecycle_1.0.3  tibble_3.1.5     pkgconfig_2.0.3  rlang_1.1.1     
[17] Matrix_1.5-3     cli_3.6.1        rstudioapi_0.13  yaml_2.2.1      
[21] xfun_0.27        fastmap_1.1.0    stringr_1.4.0    knitr_1.36      
[25] fs_1.5.0         vctrs_0.6.3      sass_0.4.4       grid_4.1.1      
[29] rprojroot_2.0.2  glue_1.4.2       R6_2.5.1         fansi_0.5.0     
[33] rmarkdown_2.11   magrittr_2.0.1   whisker_0.4      splines_4.1.1   
[37] promises_1.2.0.1 ellipsis_0.3.2   htmltools_0.5.5  httpuv_1.6.3    
[41] utf8_1.2.2       stringi_1.7.5    cachem_1.0.6