Last updated: 2019-11-25
Checks: 6 1
Knit directory: simpamm/
This reproducible R Markdown analysis was created with workflowr (version 1.5.0). 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(20180517)
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.
To ensure reproducibility of the results, delete the cache directory confidence-intervals_cache
and re-run the analysis. To have workflowr automatically delete the cache directory prior to building the file, set delete_cache = TRUE
when running wflow_build()
or wflow_publish()
.
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 version displayed above was the version of the Git repository at the time these results were generated.
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: .Rhistory
Ignored: .Rproj.user/
Ignored: analysis/confidence-intervals_cache/
Ignored: analysis/pem_vs_pam_cache/
Ignored: analysis/time-varying-cumulative-effect_cache/
Untracked files:
Untracked: analysis/fit-vs-truth-ped.gif
Untracked: analysis/time-varying-cumulative-effect.Rmd
Untracked: output/sim-conf-int-registry/
Untracked: output/sim-lag-lead-registry/
Untracked: output/sim-pem-vs-pam-registry/
Untracked: output/tve-cumulative-registry/
Untracked: sandbox/
Untracked: simpamm.Rproj
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 R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote
), click on the hyperlinks in the table below to view them.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | a900481 | Andreas Bender | 2019-11-25 | wflow_publish(“analysis/confidence-intervals.Rmd”) |
html | 45c6520 | Andreas Bender | 2019-11-25 | Build site. |
Rmd | 4ea7082 | Andreas Bender | 2019-11-25 | Add sim study on confidence intervals |
library(dplyr)
library(ggplot2)
theme_set(theme_bw())
library(patchwork)
library(survival)
library(mgcv)
library(pammtools)
library(batchtools)
knitr::opts_chunk$set(autodep = TRUE)
Here we consider three different ways to calculate confidence intervals for hazard rates estimated by PAMMs and derivatives thereof, i.e., the cumulative hazard and survival probability. The three methods compared here are
We simulate data with log-hazard rate \(\log(\lambda(t|x)) = -3.5 + f(8,2) \cdot 6 - 0.5\cdot x_1 + \sqrt{x_2}\), where \(f(8,2)\) is the gamma density function with respective parameters.
The below wrapper creates a data set with \(n = 500\) survival times based on above hazard. The simulation of survival times is performed using function pammtools::sim_pexp
.
sim_wrapper <- function(data, job, n = 500, time_grid = seq(0, 10, by = 0.05)) {
# create data set with covariates
df <- tibble::tibble(x1 = runif(n, -3, 3), x2 = runif(n, 0, 6))
# baseline hazard
f0 <- function(t) {dgamma(t, 8, 2) * 6}
ndf <- sim_pexp(
formula = ~ -3.5 + f0(t) -0.5*x1 + sqrt(x2),
data = df, cut = time_grid)
ndf
}
The below wrapper - transforms the simulated survival data into the PED format - fits the PAMM - returns a data frame that contains the coverage for each method
Expand to see function source code
ci_wrapper <- function(
data,
job,
instance,
bs = "ps",
k = 10,
ci_type = "default") {
# instance <- sim_wrapper()
ped <- as_ped(
data = instance,
formula = Surv(time, status) ~ x1 + x2,
id = "id")
form <- paste0("ped_status ~ s(tend, bs='", bs, "', k=", k, ") + s(x1) + s(x2)")
mod <- gam(
formula = as.formula(form),
data = ped,
family = poisson(),
offset = offset,
method = "REML")
f0 <- function(t) {dgamma(t, 8, 2) * 6}
# create new data set
nd <- make_newdata(ped, tend = unique(tend), x1 = c(0), x2 = c(3)) %>%
mutate(
true_hazard = exp(-3.5 + f0(tend) -0.5 * x1 + sqrt(x2)),
true_cumu = cumsum(intlen * true_hazard),
true_surv = exp(-true_cumu))
# add hazard, cumulative hazard, survival probability with confidence intervals
# using the 3 different methods
nd_default <- nd %>%
add_hazard(mod, se_mult = qnorm(0.975), ci_type = "default") %>%
add_cumu_hazard(mod, se_mult = qnorm(0.975), ci_type = "default") %>%
add_surv_prob(mod, se_mult = qnorm(0.975), ci_type = "default") %>%
mutate(
hazard = (true_hazard >= ci_lower) & (true_hazard <= ci_upper),
cumu = (true_cumu >= cumu_lower) & (true_cumu <= cumu_upper),
surv = (true_surv >= surv_lower) & (true_surv <= surv_upper)) %>%
select(hazard, cumu, surv) %>%
summarize_all(mean) %>%
mutate(method = "direct")
nd_delta <- nd %>%
add_hazard(mod, se_mult = qnorm(0.975), ci_type = "delta", overwrite = TRUE) %>%
add_cumu_hazard(mod, se_mult = qnorm(0.975), ci_type = "delta", overwrite = TRUE) %>%
add_surv_prob(mod, se_mult = qnorm(0.975), ci_type = "delta", overwrite = TRUE) %>%
mutate(
hazard = (true_hazard >= ci_lower) & (true_hazard <= ci_upper),
cumu = (true_cumu >= cumu_lower) & (true_cumu <= cumu_upper),
surv = (true_surv >= surv_lower) & (true_surv <= surv_upper)) %>%
select(hazard, cumu, surv) %>%
summarize_all(mean) %>%
mutate(method = "delta")
nd_sim <- nd %>%
add_hazard(mod, se_mult = qnorm(0.975), ci_type = "sim", nsim = 500, overwrite = TRUE) %>%
add_cumu_hazard(mod, se_mult = qnorm(0.975), ci_type = "sim", nsim = 500, overwrite = TRUE) %>%
add_surv_prob(mod, se_mult = qnorm(0.975), ci_type = "sim", nsim = 500, overwrite = TRUE) %>%
mutate(
hazard = (true_hazard >= ci_lower) & (true_hazard <= ci_upper),
cumu = (true_cumu >= cumu_lower) & (true_cumu <= cumu_upper),
surv = (true_surv >= surv_lower) & (true_surv <= surv_upper)) %>%
select(hazard, cumu, surv) %>%
summarize_all(mean) %>%
mutate(method = "simulation")
rbind(nd_default, nd_delta, nd_sim)
}
We simulate 100 data sets and obtain respective estimates using package batchtools
:
Expand to see
batchtools
code
if (!checkmate::test_directory_exists("output/sim-conf-int-registry")) {
reg <- makeExperimentRegistry(
"output/sim-conf-int-registry",
packages = c("mgcv", "dplyr", "tidyr", "pammtools"),
seed = 20052018)
reg <- loadRegistry("output/sim-conf-int-registry", writeable = TRUE)
# reg$cluster.functions = makeClusterFunctionsInteractive()
addProblem(name = "ci", fun = sim_wrapper)
addAlgorithm(name = "ci", fun = ci_wrapper)
algo_df <- data.frame(bs = c("tp", "ps"), stringsAsFactors = FALSE)
addExperiments(algo.design = list(ci = algo_df), repls = 300)
submitJobs(findNotDone())
# waitForJobs()
}
Below the RMSE and coverage are calculated for the different methods to estimate the confidence intervals
reg <- loadRegistry("output/sim-conf-int-registry", writeable = TRUE)
ids_res <- findExperiments(prob.name = "ci", algo.name = "ci")
pars <- unwrap(getJobPars()) %>% as_tibble()
res <- reduceResultsDataTable(ids=findDone(ids_res)) %>%
as_tibble() %>%
tidyr::unnest(cols = c(result)) %>%
left_join(pars)
# RMSE and coverage hazard
res %>%
group_by(bs, method) %>%
summarize(
"coverage hazard" = mean(hazard),
"coverage cumulative hazard" = mean(cumu),
"coverage survival probability" = mean(surv)) %>%
ungroup() %>%
mutate_if(is.numeric, ~round(., 3)) %>%
rename("basis" = "bs") %>%
knitr::kable()
basis | method | coverage hazard | coverage cumulative hazard | coverage survival probability |
---|---|---|---|---|
ps | delta | 0.911 | 0.904 | 0.907 |
ps | direct | 0.914 | 0.965 | 0.965 |
ps | simulation | 0.891 | 0.904 | 0.907 |
tp | delta | 0.936 | 0.925 | 0.933 |
tp | direct | 0.938 | 0.978 | 0.978 |
tp | simulation | 0.918 | 0.933 | 0.933 |
sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tidyr_1.0.0 batchtools_0.9.11 data.table_1.12.6
[4] pammtools_0.1.14 mgcv_1.8-31 nlme_3.1-142
[7] survival_3.1-7 patchwork_0.0.1.9000 ggplot2_3.2.1
[10] dplyr_0.8.3
loaded via a namespace (and not attached):
[1] progress_1.2.2 tidyselect_0.2.5 xfun_0.11 purrr_0.3.3
[5] splines_3.6.1 lattice_0.20-38 colorspace_1.4-1 vctrs_0.2.0
[9] expm_0.999-4 htmltools_0.4.0 yaml_2.2.0 rlang_0.4.2
[13] later_1.0.0 pillar_1.4.2 glue_1.3.1 withr_2.1.2
[17] rappdirs_0.3.1 lifecycle_0.1.0 stringr_1.4.0 munsell_0.5.0
[21] gtable_0.3.0 workflowr_1.5.0 mvtnorm_1.0-11 evaluate_0.14
[25] knitr_1.26 httpuv_1.5.2 highr_0.8 Rcpp_1.0.3
[29] promises_1.1.0 scales_1.1.0 backports_1.1.5 checkmate_1.9.4
[33] fs_1.3.1 brew_1.0-6 hms_0.5.2 digest_0.6.23
[37] stringi_1.4.3 msm_1.6.7 grid_3.6.1 rprojroot_1.3-2
[41] tools_3.6.1 magrittr_1.5 base64url_1.4 lazyeval_0.2.2
[45] tibble_2.1.3 Formula_1.2-3 crayon_1.3.4 whisker_0.4
[49] pkgconfig_2.0.3 zeallot_0.1.0 Matrix_1.2-17 prettyunits_1.0.2
[53] assertthat_0.2.1 rmarkdown_1.17 R6_2.4.1 git2r_0.26.1
[57] compiler_3.6.1