Last updated: 2023-02-09

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 4f1c5c3. 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

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/run_ser_simple_dat.Rmd) and HTML (docs/run_ser_simple_dat.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 4f1c5c3 yunqiyang0215 2023-02-09 wflow_publish("analysis/run_ser_simple_dat.Rmd")
html ba5113f yunqiyang0215 2023-02-09 Build site.
Rmd 4e06f27 yunqiyang0215 2023-02-09 wflow_publish("analysis/run_ser_simple_dat.Rmd")

The Wakefield approximation:

\[ ABF(H_1/H_0)=\sqrt\frac{V}{V+W}\exp\{\frac{z^2}{2}\frac{W}{V+W}\}, \] where \(V\) is the variance of estimated regression coefficient, and \(W\) is variance in the normal prior, \(N(0,W)\).

# Function to calculate approximate BF based on Wakefield approximation
# @param z: zscore of the regression coefficient
# @param s: standard deviation of the estimated coefficient
compute_abf <- function(z, s, prior_variance){
  abf <- sqrt(s^2/(s^2+prior_variance))*exp(z^2/2*(prior_variance/(s^2+prior_variance)))
  return(abf)
}


compute_approx_post_var <- function(z, s, prior_variance){
  post_var <- 1/(1/s^2 + 1/prior_variance)
  return(post_var)
}

# @param post_var: posterior variance
# @param s: standard deviation of the estimated coefficient
# @param bhat: estimated beta effect
compute_approx_post_mean <- function(post_var, s, bhat){
  mu <- post_var/(s^2)*bhat
  return(mu)
}
dat = readRDS("./data/sim_dat_simple.rds")

library(survival)
# Modified Karl's code for intercept part
devtools::load_all("/Users/nicholeyang/Desktop/logisticsusie")
ℹ Loading logisticsusie
surv_uni_fun <- function(x, y, o, prior_variance, estimate_intercept = 0, ...){
  fit <- coxph(y~ x + o)
  bhat <- summary(fit)$coefficients[1, 1]
  sd <- summary(fit)$coefficients[1, 3]
  zscore <- summary(fit)$coefficients[1, 4]
  
  bf <- compute_abf(zscore, sd, prior_variance)
  var <- compute_approx_post_var(zscore, sd, prior_variance)
  mu <- compute_approx_post_mean(var, sd, bhat)
  lbf <- log(bf)
  return(list(mu = mu, var=var, lbf=lbf, intercept=0))
}

fit_coxph <- ser_from_univariate(surv_uni_fun)

Data 1: simulated from null model:

\(\log T_i =\beta_0+\epsilon_i\) and \(\beta_0 = 1\).

X = as.matrix(dat[[1]][, c(2:3)])
## Create  survival object. status == 2 is death
y <- with(dat[[1]], Surv(surT, status == 2))
fit1 <- ibss_from_ser(X, y, L = 10, prior_variance = 1., prior_weights = rep(1/2, 2), tol = 1e-3, maxit = 100, estimate_intercept = TRUE, ser_function = fit_coxph)
Warning in ibss_from_ser(X, y, L = 10, prior_variance = 1, prior_weights =
rep(1/2, : Maximum number of iterations reached
7.056 sec elapsed
fit1$alpha
           [,1]      [,2]
 [1,] 0.7644728 0.2355272
 [2,] 0.6077619 0.3922381
 [3,] 0.5201106 0.4798894
 [4,] 0.7071450 0.2928550
 [5,] 0.4063704 0.5936296
 [6,] 0.4362467 0.5637533
 [7,] 0.5102112 0.4897888
 [8,] 0.8054618 0.1945382
 [9,] 0.8126784 0.1873216
[10,] 0.8152894 0.1847106

Data 2: simulated from \(\log T_i=\beta_0 + \beta_1x_{i1}\). \(x_1\) and \(x_2\) no correlation.

\(\beta_0 = 1, \beta_1 = 3\)

X = as.matrix(dat[[2]][, c(2:3)])
## Create  survival object. status == 2 is death
y <- with(dat[[2]], Surv(surT, status == 2))
fit2 <- ibss_from_ser(X, y, L = 10, prior_variance = 1., prior_weights = rep(1/2, 2), tol = 1e-3, maxit = 100, estimate_intercept = TRUE, ser_function = fit_coxph)
0.188 sec elapsed
fit2$alpha
      [,1]         [,2]
 [1,]    1 1.475791e-10
 [2,]    1 1.475791e-10
 [3,]    1 1.475791e-10
 [4,]    1 1.475791e-10
 [5,]    1 1.475791e-10
 [6,]    1 1.475791e-10
 [7,]    1 1.475791e-10
 [8,]    1 1.475791e-10
 [9,]    1 1.475791e-10
[10,]    1 1.475791e-10

Data 3: simulated from \(\log T_i=\beta_0 + \beta_1x_{i1}\). \(x_1\) and \(x_2\) have high correlation.

\(\beta_0 = 1, \beta_1 = 3\) and \(cor(x_1,x_2)=0.9\).

X = as.matrix(dat[[3]][, c(2:3)])
## Create  survival object. status == 2 is death
y <- with(dat[[3]], Surv(surT, status == 2))
fit3 <- ibss_from_ser(X, y, L = 10, prior_variance = 1., prior_weights = rep(1/2, 2), tol = 1e-3, maxit = 100, estimate_intercept = TRUE, ser_function = fit_coxph)
2.947 sec elapsed
fit3$alpha
           [,1]      [,2]
 [1,] 0.1553077 0.8446923
 [2,] 0.6345619 0.3654381
 [3,] 0.6348456 0.3651544
 [4,] 0.6348509 0.3651491
 [5,] 0.6345786 0.3654214
 [6,] 0.6341502 0.3658498
 [7,] 0.6337550 0.3662450
 [8,] 0.6335615 0.3664385
 [9,] 0.6336429 0.3663571
[10,] 0.6339523 0.3660477

Data 4: simulated from \(\log T_i=\beta_0 + \beta_1x_{i1} + \beta_1x_{i2}\). \(x_1\) and \(x_2\) have high correlation

\(\beta_0 = 1, \beta_1 = 3, \beta_2 = 2\) and \(cor(x_1,x_2)=0.9\).

X = as.matrix(dat[[4]][, c(2:3)])
## Create  survival object. status == 2 is death
y <- with(dat[[4]], Surv(surT, status == 2))
fit4 <- ibss_from_ser(X, y, L = 10, prior_variance = 1., prior_weights = rep(1/2, 2), tol = 1e-3, maxit = 100, estimate_intercept = TRUE, ser_function = fit_coxph)
Warning in ibss_from_ser(X, y, L = 10, prior_variance = 1, prior_weights =
rep(1/2, : Maximum number of iterations reached
6.832 sec elapsed
fit4$alpha
              [,1]       [,2]
 [1,] 0.9526665552 0.04733344
 [2,] 0.0001298666 0.99987013
 [3,] 0.4833317337 0.51666827
 [4,] 0.4895864377 0.51041356
 [5,] 0.4936979043 0.50630210
 [6,] 0.4963614006 0.50363860
 [7,] 0.4981962018 0.50180380
 [8,] 0.4995617786 0.50043822
 [9,] 0.5006569051 0.49934309
[10,] 0.5015948479 0.49840515

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] logisticsusie_0.0.0.9004 testthat_3.1.0           survival_3.2-11         
[4] workflowr_1.6.2         

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.1   xfun_0.27          bslib_0.4.1        remotes_2.4.1     
 [5] purrr_0.3.4        splines_4.1.1      lattice_0.20-44    generics_0.1.2    
 [9] vctrs_0.3.8        usethis_2.1.3      htmltools_0.5.2    yaml_2.2.1        
[13] utf8_1.2.2         rlang_1.0.6        pkgbuild_1.2.0     jquerylib_0.1.4   
[17] later_1.3.0        pillar_1.6.4       glue_1.4.2         withr_2.5.0       
[21] sessioninfo_1.1.1  matrixStats_0.63.0 lifecycle_1.0.1    stringr_1.4.0     
[25] tictoc_1.1         devtools_2.4.2     evaluate_0.14      memoise_2.0.1     
[29] knitr_1.36         callr_3.7.0        fastmap_1.1.0      httpuv_1.6.3      
[33] ps_1.6.0           fansi_0.5.0        Rcpp_1.0.8.3       promises_1.2.0.1  
[37] cachem_1.0.6       desc_1.4.0         pkgload_1.2.3      jsonlite_1.7.2    
[41] fs_1.5.0           digest_0.6.28      stringi_1.7.5      dplyr_1.0.7       
[45] processx_3.5.2     rprojroot_2.0.2    grid_4.1.1         cli_3.1.0         
[49] tools_4.1.1        magrittr_2.0.1     sass_0.4.4         tibble_3.1.5      
[53] crayon_1.4.1       whisker_0.4        pkgconfig_2.0.3    ellipsis_0.3.2    
[57] Matrix_1.5-3       prettyunits_1.1.1  rmarkdown_2.11     rstudioapi_0.13   
[61] R6_2.5.1           git2r_0.28.0       compiler_4.1.1