Last updated: 2023-03-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 c48008f. 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:    analysis/.DS_Store
    Ignored:    analysis/.RData
    Ignored:    analysis/.Rhistory

Untracked files:
    Untracked:  analysis/ibss_null_model.Rmd

Unstaged changes:
    Modified:   analysis/bhglm_censoring.Rmd
    Modified:   analysis/check_coxph_fit.Rmd
    Deleted:    analysis/null_model_demo.Rmd
    Modified:   analysis/null_model_zscore.Rmd
    Deleted:    analysis/one_predictor_investigation.Rmd
    Deleted:    analysis/ser_survival.Rmd
    Modified:   analysis/sim_survival_with_censoring.Rmd

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/vi_exponential.Rmd) and HTML (docs/vi_exponential.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 c48008f yunqiyang0215 2023-03-28 wflow_publish("analysis/vi_exponential.Rmd")
html bd953f3 yunqiyang0215 2023-03-27 Build site.
Rmd 1f4f29f yunqiyang0215 2023-03-27 wflow_publish("analysis/vi_exponential.Rmd")
html 42bd7c9 yunqiyang0215 2023-03-27 Build site.
Rmd d440e56 yunqiyang0215 2023-03-27 wflow_publish("analysis/vi_exponential.Rmd")
html 7e681f8 yunqiyang0215 2023-03-27 Build site.
Rmd 00a41d0 yunqiyang0215 2023-03-27 wflow_publish("analysis/vi_exponential.Rmd")
html bcdf196 yunqiyang0215 2023-03-27 Build site.
Rmd daeebdb yunqiyang0215 2023-03-27 wflow_publish("analysis/vi_exponential.Rmd")
html 599ae7f yunqiyang0215 2023-03-25 Build site.
Rmd a3b74ea yunqiyang0215 2023-03-25 wflow_publish("analysis/vi_exponential.Rmd")
html 01efa0e yunqiyang0215 2023-03-24 Build site.
Rmd dbad2be yunqiyang0215 2023-03-24 wflow_publish("analysis/vi_exponential.Rmd")

1. Model:

\[ \begin{split} b&\sim N(0, \sigma^2)\\ h(t)&=h_0(t)\exp\{bx\}\\ \end{split} \] And we assume \(h_0(t) =h_0\), constant baseline hazard and independent censoring. \(y_i=\min(t_i,c_i)\), and \(\delta_i\) indicates censoring status. \(\delta_i=1\): the event time is observed and \(\delta_i=0\) indicates censoring.

library(survival)
source("./code/VI_exponential.R")

2. data simulation

\[ \begin{split} S(t)&=\exp\{-h(t)*t\}\\ F(t)&=1-S(t)\sim U[0,1]\\ \end{split} \] Using this relationship, we can simulate \(t_i\) by: \[ \begin{split} t_i=\log(1-u_i)/(-h_0\exp\{bx_i\}) \end{split} \] Check if the data simulation makes sense

set.seed(1)
n = 5000
censoring_lvl = 0
s2 = 1 #prior variance of b
h0 = 2
b = rnorm(1, mean = 0, sd = s2)
print(b)
[1] -0.6264538
seeds  = c(1:100)
bhat = rep(NA, 100)

for (seed in seeds){
  x = rnorm(n, 1)
  y <- log(1 - runif(n)) / (-h0*exp(x*b))
  # Status variable delta. delta = 1, outcome observed. delta = 0, censored. 
  d = rbinom(n, size = 1, prob = 1-censoring_lvl)
  y[!d] <- y[!d] * runif(sum(!d))   
  y.surv <- Surv(y, d)
  cox1 <- coxph(y.surv ~ x)
  bhat[seed] = coef(cox1)[1]
}
hist(bhat)
abline(v = mean(bhat), col = "red")
abline(v = b, col = "blue")

Version Author Date
bcdf196 yunqiyang0215 2023-03-27

3. Run variational approximation

# change parameter init vals
# the first param is m, the second is v2.
lower = c(-10, 1e-6)
upper = c(10, 10)
res1 <- update_q(x, y, d, h0 = 2, m = 1, v2 = 1, s2 = 10, maxiter = 50, tol = 0.1, lower, upper)
tail(res1)
           elbo       h0          m    v2        s2
[45,] -4558.438 2.034419 -0.6260834 1e-06 0.3919814
[46,] -4558.438 2.034419 -0.6260834 1e-06 0.3919814
[47,] -4558.438 2.034419 -0.6260834 1e-06 0.3919814
[48,] -4558.438 2.034419 -0.6260834 1e-06 0.3919814
[49,] -4558.438 2.034419 -0.6260834 1e-06 0.3919814
[50,] -4558.438 2.034419 -0.6260834 1e-06 0.3919814
# change parameter init vals
lower = c(-50, 1e-3)
upper = c(50, 50)
res2 <- update_q(x, y, d, h0 = 2, m = 1, v2 = 1, s2 = 10, maxiter = 50, tol = 0.1, lower, upper)
tail(res2)
           elbo       h0          m    v2        s2
[45,] -4559.926 2.034362 -0.6270516 0.001 0.3941937
[46,] -4559.926 2.034362 -0.6270516 0.001 0.3941937
[47,] -4559.926 2.034362 -0.6270516 0.001 0.3941937
[48,] -4559.926 2.034362 -0.6270516 0.001 0.3941937
[49,] -4559.926 2.034362 -0.6270516 0.001 0.3941937
[50,] -4559.926 2.034362 -0.6270516 0.001 0.3941937
# change parameter init vals
lower = c(-50, 1)
upper = c(50, 50)
res3 <- update_q(x, y, d, h0 = 10, m = 1, v2 = 1, s2 = 50, maxiter = 50, tol = 0.1, lower, upper)
tail(res3)
           elbo       h0         m v2       s2
[45,] -12776.24 1.124916 -1.687747  1 3.848488
[46,] -12776.24 1.124916 -1.687747  1 3.848488
[47,] -12776.24 1.124916 -1.687747  1 3.848488
[48,] -12776.24 1.124916 -1.687747  1 3.848488
[49,] -12776.24 1.124916 -1.687747  1 3.848488
[50,] -12776.24 1.124916 -1.687747  1 3.848488
# change parameter init vals
lower = c(-50, 1e-10)
upper = c(50, 50)
res4 <- update_q(x[1:50], y[1:50], d, h0 = 10, m = 1, v2 = 1, s2 = 50, maxiter = 50, tol = 0.1, lower, upper)
tail(res4)
           elbo       h0         m    v2       s2
[45,] -478714.8 562.3696 -2.115074 1e-10 4.473538
[46,] -478714.8 562.3696 -2.115074 1e-10 4.473538
[47,] -478714.8 562.3696 -2.115074 1e-10 4.473538
[48,] -478714.8 562.3696 -2.115074 1e-10 4.473538
[49,] -478714.8 562.3696 -2.115074 1e-10 4.473538
[50,] -478714.8 562.3696 -2.115074 1e-10 4.473538
grid = seq(1e-7, 5, by = 0.001)
res = rep(NA, length(grid))

for (i in 1:length(grid)){
  res[i] = partial_dv_v2(grid[i], x[1:100], y[1:100], s2 = 1, m = 0, h0 = 1)
}
# all less than 0. 
plot(grid, res, ylab = "value of partial derivative of v2")


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 workflowr_1.6.2

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