Last updated: 2023-06-28

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 c3a005b. 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:   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/numerical_integration.Rmd) and HTML (docs/numerical_integration.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 c3a005b yunqiyang0215 2023-06-28 wflow_publish("analysis/numerical_integration.Rmd")
html bc28e32 yunqiyang0215 2023-06-28 Build site.
Rmd 6dd1091 yunqiyang0215 2023-06-28 wflow_publish("analysis/numerical_integration.Rmd")

Description: using quadrature method to compute Bayes factor in survival susie model.

The standard BF for comparing the hypothesis of having one predictor vs. the null model: \[ \frac{P(D|H_1)}{P(D|H_0)}=\frac{\int\int p(y,\delta|x,b, h_0)p(b)dbdh_0}{\int p(y,\delta|x,b=0,h_0)dh_0} \]

Based on the paper, “A Bayesian Justification of Cox’s Partial Likelihood” by Sinha, D. and others, we can safely use the partial likelihood to compute Bayes factor. That is,

\[ \begin{split} \frac{P(D|H_1)}{P(D|H_0)}&=\frac{\int Lp(b)p(b)db}{Lp(b=0)}\\ Lp(b)&=\prod_{i=1}^n\{\frac{\exp(x_ib)}{\sum_{j\in R(y_i)}\exp(x_jb)} \}^{\delta_i} \end{split} \]

# 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)
}
library(survival)
library(cubature)
source("./code/surv_susie_helper.R")

Simulate data

set.seed(1)
# simulate 2 variables 
n = 100
X = cbind(rbinom(n, size = 2, prob = 0.3), rbinom(n, size = 1, prob = 0.3))
# the first element of b is for intercept
b = c(1, 1, 0)
censor_lvl = 0.7
dat <- sim_surv_exp(b, X, censor_lvl)
survT <- Surv(dat$y[,1], dat$y[,2])

Use numerical integration

# @param b: the effect size
# @param surT: a Surv() object, containing time and status
# @param x: covariate
get_partial_lik <- function(b, survT, x){
  partial.loglik = logLik(coxph(survT ~ x, init = c(b), control=list('iter.max'= 0, timefix=FALSE)))
  partial.lik = exp(partial.loglik) # handling ties...
  return(as.numeric(partial.lik))
}

integrand <- function(b, survT, x, prior_sd){
  val = get_partial_lik(b, survT, x)*dnorm(b, mean = 0, sd = prior_sd)
  return(val)
}
prior_sd = 1
res = cubintegrate(f = integrand, survT = survT, x = dat$X[,1], prior_sd = 1, lower = -Inf, upper = Inf, method = "hcubature")
res
$integral
[1] 3.867775e-27

$error
[1] 6.23873e-27

$neval
[1] 15

$returnCode
[1] 0
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
lbf.numerical
[1] 6.984264
abf
[1] 7.086837

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.1  tibble_3.1.5     pkgconfig_2.0.3  rlang_1.1.1     
[17] Matrix_1.5-3     cli_3.1.0        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.3.8      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