CIG01
Confidence Interval Plot
The function stat_mean_ci
from the tern
package can be used with default values to draw the 95% confidence interval around the mean.
Code
plot <- ggplot(
data = adlb,
mapping = aes(
x = ARMCD, y = AVAL, color = SEX,
lty = SEX, shape = SEX
)
) +
stat_summary(
fun.data = tern::stat_mean_ci,
geom = "errorbar",
width = 0.1,
position = position_dodge(width = 0.5)
) +
stat_summary(
fun = mean,
geom = "point",
position = position_dodge(width = 0.5)
) +
labs(
title = "Confidence Interval Plot by Treatment Group",
caption = "Mean and 95% CIs for mean are displayed.",
x = "Treatment Group",
y = paste0(adlb$PARAMCD[1], " (", adlb$AVALU[1], ")")
)
plot
Code
plot <- ggplot(
data = adlb,
mapping = aes(
x = ARMCD, y = AVAL, color = STRATA2,
lty = STRATA2, shape = STRATA2
)
) +
stat_summary(
fun.data = tern::stat_mean_ci,
geom = "errorbar",
width = 0.1,
position = position_dodge(width = 0.5)
) +
stat_summary(
fun = mean,
geom = "point",
position = position_dodge(width = 0.5)
) +
labs(
title = "Confidence Interval Plot by Treatment Group",
caption = "Mean and 95% CIs for mean are displayed.",
x = "Treatment Group",
y = paste0(adlb$PARAMCD[1], " (", adlb$AVALU[1], ")")
)
plot
The function stat_median_ci
from the tern
package works similarly to stat_mean_ci
.
Code
plot <- ggplot(
data = adlb,
mapping = aes(
x = ARMCD, y = AVAL, color = STRATA1,
lty = STRATA1, shape = STRATA1
)
) +
stat_summary(
fun.data = stat_median_ci,
geom = "errorbar",
width = 0.1,
position = position_dodge(width = 0.5)
) +
stat_summary(
fun = median,
geom = "point",
position = position_dodge(width = 0.5)
) +
labs(
title = "Confidence Interval Plot by Treatment Group",
caption = "Median and 95% CIs for median are displayed.",
x = "Treatment Group",
y = paste0(adlb$PARAMCD[1], " (", adlb$AVALU[1], ")")
)
plot
To modify the confidence level for the estimation of the confidence interval, the call to stat_mean_ci
(or stat_median_ci
) can be slightly modified.
Code
plot <- ggplot(
data = adlb,
mapping = aes(
x = ARMCD, y = AVAL, color = SEX,
lty = SEX, shape = SEX
)
) +
stat_summary(
fun.data = function(x) tern::stat_mean_ci(x, conf_level = 0.9),
geom = "errorbar",
width = 0.1,
position = position_dodge(width = 0.5)
) +
stat_summary(
fun = mean,
geom = "point",
position = position_dodge(width = 0.5)
) +
labs(
title = "Confidence Interval Plot by Treatment Group",
caption = "Mean and 90% CIs for mean are displayed.",
x = "Treatment Group",
y = paste0(adlb$PARAMCD[1], " (", adlb$AVALU[1], ")")
)
plot
The corresponding table is simply obtained using the rtables
framework:
Code
ARM A ARM B ARM C
------------------------------------------------
Mean (SD) 17.7 (9.9) 18.7 (9.8) 19.5 (9.1)
Median 17.5 18.2 19.0
Code
library(teal.modules.clinical)
## Data reproducible code
data <- teal_data()
data <- within(data, {
library(scda)
library(scda.2022)
ADSL <- synthetic_cdisc_dataset("latest", "adsl")
ADLB <- synthetic_cdisc_dataset("latest", "adlb")
})
datanames <- c("ADSL", "ADLB")
datanames(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]
## Reusable Configuration For Modules
ADLB <- data[["ADLB"]]
## Setup App
app <- init(
data = data,
modules = modules(
tm_g_ci(
label = "Confidence Interval Plot",
x_var = data_extract_spec(
dataname = "ADSL",
select = select_spec(
choices = c("ARMCD", "BMRKR2"),
selected = c("ARMCD"),
multiple = FALSE,
fixed = FALSE
)
),
y_var = data_extract_spec(
dataname = "ADLB",
filter = list(
filter_spec(
vars = "PARAMCD",
choices = levels(ADLB$PARAMCD),
selected = levels(ADLB$PARAMCD)[1],
multiple = FALSE,
label = "Select lab:"
),
filter_spec(
vars = "AVISIT",
choices = levels(ADLB$AVISIT),
selected = levels(ADLB$AVISIT)[1],
multiple = FALSE,
label = "Select visit:"
)
),
select = select_spec(
label = "Analyzed Value",
choices = c("AVAL", "CHG"),
selected = "AVAL",
multiple = FALSE,
fixed = FALSE
)
),
color = data_extract_spec(
dataname = "ADSL",
select = select_spec(
label = "Color by variable",
choices = c("SEX", "STRATA1", "STRATA2"),
selected = c("STRATA1"),
multiple = FALSE,
fixed = FALSE
)
)
)
),
header = "Example of Confidence Interval Plot",
footer = tags$p(
class = "text-muted", "Source: `teal.modules.clinical::tm_g_ci`"
)
)
shinyApp(app$ui, app$server)
shinylive
allow you to modify to run shiny
application entirely in the web browser. Modify the code below and click re-run the app to see the results. The performance is slighly worse and some of the features (e.g. downloading) might not work at all.
#| standalone: true
#| viewerHeight: 800
#| components: [viewer, editor]
#| layout: vertical
# -- WEBR HELPERS --
options(webr_pkg_repos = c("r-universe" = "https://insightsengineering.r-universe.dev", getOption("webr_pkg_repos")))
if (packageVersion("webr") < "0.3.0") {
.e <- as.environment("webr_shims")
.e[["library"]] <- function(pkg, ...) {
package <- as.character(substitute(pkg))
if (length(find.package(package, quiet = TRUE)) == 0) {
webr::install(package)
}
base::library(package, character.only = TRUE, ...)
}
}
# -- APP CODE --
library(teal.modules.clinical)
## Data reproducible code
data <- teal_data()
data <- within(data, {
library(scda)
library(scda.2022)
ADSL <- synthetic_cdisc_dataset("latest", "adsl")
ADLB <- synthetic_cdisc_dataset("latest", "adlb")
})
datanames <- c("ADSL", "ADLB")
datanames(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]
## Reusable Configuration For Modules
ADLB <- data[["ADLB"]]
## Setup App
app <- init(
data = data,
modules = modules(
tm_g_ci(
label = "Confidence Interval Plot",
x_var = data_extract_spec(
dataname = "ADSL",
select = select_spec(
choices = c("ARMCD", "BMRKR2"),
selected = c("ARMCD"),
multiple = FALSE,
fixed = FALSE
)
),
y_var = data_extract_spec(
dataname = "ADLB",
filter = list(
filter_spec(
vars = "PARAMCD",
choices = levels(ADLB$PARAMCD),
selected = levels(ADLB$PARAMCD)[1],
multiple = FALSE,
label = "Select lab:"
),
filter_spec(
vars = "AVISIT",
choices = levels(ADLB$AVISIT),
selected = levels(ADLB$AVISIT)[1],
multiple = FALSE,
label = "Select visit:"
)
),
select = select_spec(
label = "Analyzed Value",
choices = c("AVAL", "CHG"),
selected = "AVAL",
multiple = FALSE,
fixed = FALSE
)
),
color = data_extract_spec(
dataname = "ADSL",
select = select_spec(
label = "Color by variable",
choices = c("SEX", "STRATA1", "STRATA2"),
selected = c("STRATA1"),
multiple = FALSE,
fixed = FALSE
)
)
)
),
header = "Example of Confidence Interval Plot",
footer = tags$p(
class = "text-muted", "Source: `teal.modules.clinical::tm_g_ci`"
)
)
shinyApp(app$ui, app$server)
Timestamp
Session Info
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0
locale:
[1] C
time zone: Etc/UTC
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] teal.modules.clinical_0.9.1 teal.transform_0.5.0
[3] teal_0.15.2 teal.slice_0.5.1
[5] teal.data_0.6.0 teal.code_0.5.0
[7] shiny_1.8.1.1 nestcolor_0.1.2
[9] scda.2022_0.1.5 scda_0.1.6
[11] dplyr_1.1.4 ggplot2_3.5.1
[13] tern_0.9.4 rtables_0.6.7
[15] magrittr_2.0.3 formatters_0.5.6
loaded via a namespace (and not attached):
[1] tidyselect_1.2.1 farver_2.1.1 fastmap_1.1.1
[4] TH.data_1.1-2 webshot2_0.1.1 promises_1.3.0
[7] shinyjs_2.1.0 digest_0.6.35 estimability_1.5
[10] mime_0.12 tern.gee_0.1.3 lifecycle_1.0.4
[13] processx_3.8.4 survival_3.6-4 compiler_4.4.0
[16] sass_0.4.9 rlang_1.1.3 tools_4.4.0
[19] utf8_1.2.4 yaml_2.3.8 knitr_1.46
[22] labeling_0.4.3 htmlwidgets_1.6.4 websocket_1.4.1
[25] multcomp_1.4-25 withr_3.0.0 purrr_1.0.2
[28] shinyWidgets_0.8.6 geepack_1.3.10 grid_4.4.0
[31] fansi_1.0.6 teal.logger_0.2.0 xtable_1.8-4
[34] colorspace_2.1-0 emmeans_1.10.1 scales_1.3.0
[37] MASS_7.3-60.2 cli_3.6.2 mvtnorm_1.2-4
[40] rmarkdown_2.26 ragg_1.3.1 generics_0.1.3
[43] chromote_0.2.0 cachem_1.0.8 splines_4.4.0
[46] formatR_1.14 vctrs_0.6.5 webshot_0.5.5
[49] Matrix_1.7-0 sandwich_3.1-0 jsonlite_1.8.8
[52] callr_3.7.6 systemfonts_1.0.6 teal.widgets_0.4.2
[55] testthat_3.2.1.1 fontawesome_0.5.2 jquerylib_0.1.4
[58] tidyr_1.3.1 glue_1.7.0 ps_1.7.6
[61] codetools_0.2-20 stringi_1.8.4 gtable_0.3.5
[64] later_1.3.2 shinycssloaders_1.0.0 munsell_0.5.1
[67] tibble_3.2.1 logger_0.3.0 pillar_1.9.0
[70] htmltools_0.5.8.1 brio_1.1.5 R6_2.5.1
[73] textshaping_0.3.7 Rdpack_2.6 evaluate_0.23
[76] lattice_0.22-6 rbibutils_2.2.16 backports_1.4.1
[79] memoise_2.0.1 broom_1.0.5 teal.reporter_0.3.1
[82] httpuv_1.6.15 bslib_0.7.0 Rcpp_1.0.12
[85] shinyvalidate_0.1.3 coda_0.19-4.1 nlme_3.1-164
[88] checkmate_2.3.1 xfun_0.43 zoo_1.8-12
[91] pkgconfig_2.0.3
.lock
file
Download the .lock
file and use renv::restore()
on it to recreate environment used to generate this website.
Code
parent_file_path <- knitr::current_input(dir = TRUE)
book_root_dir <- parent_file_path
while (!file.exists(file.path(book_root_dir, "_quarto.yml"))) book_root_dir <- dirname(book_root_dir)
lock_path <- file.path(
"../../assets/www/lock",
paste0(
gsub(
file.path(book_root_dir, ""),
"",
gsub(
"\\.rmarkdown$",
"",
knitr::current_input(dir = TRUE)
)
),
".lock"
)
)
withr::with_options(
list(renv.verbose = FALSE),
renv::snapshot(parent_file_path, lockfile = lock_path, prompt = FALSE, force = TRUE)
)