Last updated: 2023-06-16
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 4b699e2. 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/coxph_fit.Rmd
) and HTML (docs/coxph_fit.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 | 4b699e2 | yunqiyang0215 | 2023-06-16 | wflow_publish("analysis/coxph_fit.Rmd") |
html | f7b9dc7 | yunqiyang0215 | 2023-06-16 | Build site. |
Rmd | 2248e0e | yunqiyang0215 | 2023-06-16 | wflow_publish("analysis/coxph_fit.Rmd") |
Conclusion:
Extremely high censoring rate probably won’t affect effect size estimate as long as there are moderate number of events.
If we draw a contigency table (\(2\times 2\)). \(\delta = 0,1\) indicates whether the outcome is observed, and \(x=0,1\) indicates genotypes. As long as there are a couple observations in each cell, the effect size estimate is fine. Otherwise, coefficient tends to be infinite.
library(survival)
# 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)
}
set.seed(1)
# simulate 2 variables
n = 100
X = cbind(rbinom(n, size = 1, prob = 0.3), rbinom(n, size = 1, prob = 0.1))
# 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)
# rownames are unique y[,2] values, delta. delta = 1 indicates event happened.
# colnames are X values, genotype.
table(dat$y[,2], dat$X[,1])
0 1
0 53 26
1 15 6
surT <- Surv(dat$y[,1], dat$y[,2])
fit1 <- coxph(surT ~ dat$X[,1])
summary(fit1)
Call:
coxph(formula = surT ~ dat$X[, 1])
n= 100, number of events= 21
coef exp(coef) se(coef) z Pr(>|z|)
dat$X[, 1] 2.035 7.656 0.587 3.468 0.000525 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
exp(coef) exp(-coef) lower .95 upper .95
dat$X[, 1] 7.656 0.1306 2.423 24.19
Concordance= 0.686 (se = 0.058 )
Likelihood ratio test= 10.26 on 1 df, p=0.001
Wald test = 12.02 on 1 df, p=5e-04
Score (logrank) test = 15.51 on 1 df, p=8e-05
Select sub-samples where individuals who have event have x = 1. And individuals with x = 0 are all censored.
# let's just select sub-samples where individuals who have event have x = 1.
# And individuals with x = 0 are all censored.
indx = which(dat$y[,2] == dat$X[,1])
y = dat$y[indx, ]
x = dat$X[indx, 1]
table(y[,2], x)
x
0 1
0 53 0
1 0 6
surT <- Surv(y[,1], y[,2])
fit2 <- coxph(surT ~ x)
Warning in coxph.fit(X, Y, istrat, offset, init, control, weights = weights, :
Loglik converged before variable 1 ; coefficient may be infinite.
summary(fit2)
Call:
coxph(formula = surT ~ x)
n= 59, number of events= 6
coef exp(coef) se(coef) z Pr(>|z|)
x 2.410e+01 2.936e+10 2.077e+04 0.001 0.999
exp(coef) exp(-coef) lower .95 upper .95
x 2.936e+10 3.406e-11 0 Inf
Concordance= 0.964 (se = 0.015 )
Likelihood ratio test= 29.39 on 1 df, p=6e-08
Wald test = 0 on 1 df, p=1
Score (logrank) test = 60.57 on 1 df, p=7e-15
In fit3, we add 2 samples to data in fit2. Doesn’t help in this case.
In fit4, we add 4 samples to data in fit2. It helped a lot.
indx2 = which(dat$y[,2] != dat$X[,1])[c(1,4)]
y2 = rbind(y, dat$y[indx2, ])
x2 = c(x, dat$X[,1][indx2])
table(y2[,2], x2)
x2
0 1
0 53 1
1 1 6
surT <- Surv(y2[,1], y2[,2])
fit3 <- coxph(surT ~ x2)
Warning in coxph.fit(X, Y, istrat, offset, init, control, weights = weights, :
Loglik converged before variable 1 ; coefficient may be infinite.
summary(fit3)
Call:
coxph(formula = surT ~ x2)
n= 61, number of events= 7
coef exp(coef) se(coef) z Pr(>|z|)
x2 2.335e+01 1.379e+10 1.443e+04 0.002 0.999
exp(coef) exp(-coef) lower .95 upper .95
x2 1.379e+10 7.25e-11 0 Inf
Concordance= 0.919 (se = 0.036 )
Likelihood ratio test= 28.75 on 1 df, p=8e-08
Wald test = 0 on 1 df, p=1
Score (logrank) test = 56.01 on 1 df, p=7e-14
indx2 = which(dat$y[,2] != dat$X[,1])[c(1:4)]
y2 = rbind(y, dat$y[indx2, ])
x2 = c(x, dat$X[,1][indx2])
table(y2[,2], x2)
x2
0 1
0 53 3
1 1 6
surT <- Surv(y2[,1], y2[,2])
fit4 <- coxph(surT ~ x2)
summary(fit4)
Call:
coxph(formula = surT ~ x2)
n= 63, number of events= 7
coef exp(coef) se(coef) z Pr(>|z|)
x2 3.996 54.381 1.099 3.635 0.000278 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
exp(coef) exp(-coef) lower .95 upper .95
x2 54.38 0.01839 6.304 469.1
Concordance= 0.904 (se = 0.04 )
Likelihood ratio test= 21.26 on 1 df, p=4e-06
Wald test = 13.21 on 1 df, p=3e-04
Score (logrank) test = 38.8 on 1 df, p=5e-10
set.seed(1)
# simulate 2 variables
n = 1000
X = cbind(rbinom(n, size = 1, prob = 0.3), rbinom(n, size = 1, prob = 0.1))
# the first element of b is for intercept
b = c(1, 1, 0)
censor_lvl = 0.99
dat <- sim_surv_exp(b, X, censor_lvl)
In this case,
table(dat$y[,2], dat$X[,1])
0 1
0 690 301
1 6 3
surT <- Surv(dat$y[,1], dat$y[,2])
fit1 <- coxph(surT ~ dat$X[,1])
summary(fit1)
Call:
coxph(formula = surT ~ dat$X[, 1])
n= 1000, number of events= 9
coef exp(coef) se(coef) z Pr(>|z|)
dat$X[, 1] 1.4241 4.1541 0.7576 1.88 0.0601 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
exp(coef) exp(-coef) lower .95 upper .95
dat$X[, 1] 4.154 0.2407 0.9411 18.34
Concordance= 0.699 (se = 0.09 )
Likelihood ratio test= 3.03 on 1 df, p=0.08
Wald test = 3.53 on 1 df, p=0.06
Score (logrank) test = 4.09 on 1 df, p=0.04
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 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.0.6
[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