Last updated: 2023-09-15

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 5281280. 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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/.DS_Store

Unstaged changes:
    Modified:   analysis/run_ser_simple_dat.Rmd
    Modified:   analysis/ser_survival.Rmd
    Modified:   analysis/time_comparison.Rmd
    Modified:   data/dsc3/susie.lbf.rds

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/bf_diff.Rmd) and HTML (docs/bf_diff.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 5281280 yunqiyang0215 2023-09-15 wflow_publish("analysis/bf_diff.Rmd")
html 74ef6f7 yunqiyang0215 2023-09-15 Build site.
Rmd 27950c6 yunqiyang0215 2023-09-15 wflow_publish("analysis/bf_diff.Rmd")
html f92d5ae yunqiyang0215 2023-08-01 Build site.
html 0527948 yunqiyang0215 2023-07-31 Build site.
Rmd dbbc9ce yunqiyang0215 2023-07-31 wflow_publish("analysis/bf_diff.Rmd")
html 11f0c28 yunqiyang0215 2023-07-31 Build site.
Rmd f661067 yunqiyang0215 2023-07-31 wflow_publish("analysis/bf_diff.Rmd")

Description:

Assess difference in Bayes factor computed by numerical integration vs. ABF. The example of using numerical integration to compute BF is available at: https://yunqiyang0215.github.io/survival-susie/numerical_integration.html

We assess three scenarios under relatively large sample size, \(n= 10,000\).

  1. Extremely high censoring rate, censoring rate = 0.99

  2. Low allele frequency, MAF = 0.001

  3. Both.

Result shows that low allele frequency makes the difference between BF and ABF larger.

# Function to simulate survival time under exponential model. That is,
# assuming survival time is exponentially distributed.
# lambda(t) = lambda*exp(b0 + Xb). S(t) = exp(-lambda*t),
# F(t) = 1- S(t) \sim Unif(0,1). Therefore, t = log(1-runif(0,1))/-exp(b0+Xb).
# For censored objects, we simulate the censoring time by rescale the actual survival time.
# @param b: vector of length (p+1) for true effect size, include intercept.
# @param X: variable matrix of size n by p.
# @param censor_lvl: a constant from [0,1], indicating the censoring level in the data.
# @return  dat: a dataframe that contains `y`, `x` and `status`.
# `status`: censoring status: 0 = censored, 1 = event observed. See Surv() in library(survival)
sim_surv_exp <- function(b, X, censor_lvl){
  n = nrow(X)
  p = ncol(X)
  dat = list()

  status <- ifelse(runif(n) > censor_lvl, 1, 0)
  lambda <- exp(cbind(rep(1,n), X) %*% b)
  surT <- log(1 - runif(n)) /(-lambda)
  # rescale censored subject to get observed time
  surT[status == 0] = surT[status == 0] * runif(sum(status == 0))

  y = cbind(surT, status)
  colnames(y) = c("time", "status")
  colnames(X) <- unlist(lapply(1:p, function(i) paste0("x", i)))
  dat[["X"]] = X
  dat[["y"]] = y
  return(dat)
}
# @param b: the effect size
# @param surT: a Surv() object, containing time and status
# @param x: covariate
get_partial_lik <- function(b, survT, x){
  ll.partial = logLik(coxph(survT ~ x, init = c(b), control=list('iter.max'= 0, timefix=FALSE)))
  max.ll.partial = logLik(coxph(survT ~ x))
  lik.partial.normed = exp(ll.partial - max.ll.partial) # partial likelihood / max(partial likelihood)
  return(as.numeric(lik.partial.normed))
}

integrand <- function(b, survT, x, prior_sd){
  val = get_partial_lik(b, survT, x) * dnorm(b, mean = 0, sd = prior_sd)
  return(val)
}
library(survival)
library(cubature)
source("./code/surv_susie_helper.R")

Scenario 1:

seeds = c(1:5)
result = matrix(NA, ncol = 3, nrow = length(seeds))
colnames(result) = c("diff", "integral", "error")
for (seed in seeds){
  set.seed(seed)
  # simulate 1 variable
  n = 1e4
  X = cbind(rbinom(n, size = 2, prob = 0.2))
  # the first element of b is for intercept
  b = c(1, 1)
  censor_lvl = 0.99
  dat <- sim_surv_exp(b, X, censor_lvl)
  survT <- Surv(dat$y[,1], dat$y[,2])

  
  prior_sd = 1
  res = cubintegrate(f = integrand, survT = survT, x = dat$X[,1], prior_sd = 1, lower = -100, upper = 100, method = "hcubature")
  max.ll.partial = logLik(coxph(survT ~ dat$X[,1]))
  prob.h1 = res$integral*exp(max.ll.partial)
  prob.h0 = get_partial_lik(b = 0, survT, dat$X[,1])
  lbf.numerical = log(res$integral) - log(prob.h0)
  abf <-surv_uni_fun(x = dat$X[,1], y = survT, o = rep(0, n), prior_variance = prior_sd^2)$lbf
  result[seed, 1] = lbf.numerical - abf
  result[seed, 2] = res$integral
  result[seed, 3] = res$error
}

Scenario 2:

seeds = c(1:5)
result = matrix(NA, ncol = 3, nrow = length(seeds))
colnames(result) = c("diff", "integral", "error")
for (seed in seeds){
  set.seed(seed)
  # simulate 1 variable
  n = 1e4
  X = cbind(rbinom(n, size = 2, prob = 0.001))
  # the first element of b is for intercept
  b = c(1, 1)
  censor_lvl = 0.6
  dat <- sim_surv_exp(b, X, censor_lvl)
  survT <- Surv(dat$y[,1], dat$y[,2])

  
  prior_sd = 1
  res = cubintegrate(f = integrand, survT = survT, x = dat$X[,1], prior_sd = 1, lower = -100, upper = 100, method = "hcubature")
  max.ll.partial = logLik(coxph(survT ~ dat$X[,1]))
  prob.h1 = res$integral*exp(max.ll.partial)
  prob.h0 = get_partial_lik(b = 0, survT, dat$X[,1])
  lbf.numerical = log(res$integral) - log(prob.h0)
  abf <-surv_uni_fun(x = dat$X[,1], y = survT, o = rep(0, n), prior_variance = prior_sd^2)$lbf
  result[seed, 1] = lbf.numerical - abf
  result[seed, 2] = res$integral
  result[seed, 3] = res$error
}
result
           diff  integral        error
[1,] 0.10664739 0.3084903 1.524437e-06
[2,] 0.05312745 0.1639938 6.036238e-08
[3,] 0.08580469 0.1176151 6.913447e-08
[4,] 0.12090607 0.2083243 1.052610e-06
[5,] 0.05529886 0.2403162 3.301337e-09

Scenario 3:

seeds = c(1:3)
result = matrix(NA, ncol = 3, nrow = 3)
colnames(result) = c("diff", "integral", "error")
for (seed in seeds){
  set.seed(seed)
  # simulate 1 variable
  n = 1e4
  X = cbind(rbinom(n, size = 2, prob = 0.001))
  # the first element of b is for intercept
  b = c(1, 1)
  censor_lvl = 0.99
  dat <- sim_surv_exp(b, X, censor_lvl)
  survT <- Surv(dat$y[,1], dat$y[,2])

  
  prior_sd = 1
  res = cubintegrate(f = integrand, survT = survT, x = dat$X[,1], prior_sd = 1, lower = -100, upper = 100, method = "hcubature")
  max.ll.partial = logLik(coxph(survT ~ dat$X[,1]))
  prob.h1 = res$integral*exp(max.ll.partial)
  prob.h0 = get_partial_lik(b = 0, survT, dat$X[,1])
  lbf.numerical = log(res$integral) - log(prob.h0)
  abf <-surv_uni_fun(x = dat$X[,1], y = survT, o = rep(0, n), prior_variance = prior_sd^2)$lbf
  result[seed, 1] = lbf.numerical - abf
  result[seed, 2] = res$integral
  result[seed, 3] = res$error
}
result
            diff  integral        error
[1,] -0.03289383 0.9676211 3.969705e-08
[2,] -0.07537664 0.9273741 4.480898e-08
[3,] -0.08586509 0.9176952 4.704538e-08

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] cubature_2.1.0  survival_3.2-11 workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     pillar_1.6.4     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     crayon_1.4.1