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, {
ADSL <- random.cdisc.data::cadsl
ADLB <- random.cdisc.data::cadlb
})
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://pharmaverse.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, {
ADSL <- random.cdisc.data::cadsl
ADLB <- random.cdisc.data::cadlb
})
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.9005 teal.transform_0.5.0.9005
[3] teal_0.15.2.9036 teal.slice_0.5.1.9002
[5] teal.data_0.6.0.9002 teal.code_0.5.0.9005
[7] shiny_1.8.1.1 nestcolor_0.1.2.9010
[9] dplyr_1.1.4 ggplot2_3.5.1
[11] tern_0.9.4.9007 rtables_0.6.7.9002
[13] magrittr_2.0.3 formatters_0.5.6.9004
loaded via a namespace (and not attached):
[1] tidyselect_1.2.1 farver_2.1.1
[3] fastmap_1.1.1 random.cdisc.data_0.3.15.9003
[5] TH.data_1.1-2 webshot2_0.1.1
[7] shinyjs_2.1.0 promises_1.3.0
[9] digest_0.6.35 estimability_1.5
[11] mime_0.12 tern.gee_0.1.3.9006
[13] lifecycle_1.0.4 survival_3.6-4
[15] processx_3.8.4 compiler_4.4.0
[17] sass_0.4.9 rlang_1.1.3
[19] tools_4.4.0 utf8_1.2.4
[21] yaml_2.3.8 knitr_1.46
[23] labeling_0.4.3 htmlwidgets_1.6.4
[25] websocket_1.4.1 multcomp_1.4-25
[27] withr_3.0.0 purrr_1.0.2
[29] shinyWidgets_0.8.6 geepack_1.3.10
[31] grid_4.4.0 fansi_1.0.6
[33] teal.logger_0.2.0.9002 xtable_1.8-4
[35] colorspace_2.1-0 emmeans_1.10.1
[37] scales_1.3.0 MASS_7.3-60.2
[39] cli_3.6.2 mvtnorm_1.2-4
[41] rmarkdown_2.26 ragg_1.3.0
[43] generics_0.1.3 cachem_1.0.8
[45] chromote_0.2.0 stringr_1.5.1
[47] splines_4.4.0 formatR_1.14
[49] vctrs_0.6.5 webshot_0.5.5
[51] Matrix_1.7-0 sandwich_3.1-0
[53] jsonlite_1.8.8 callr_3.7.6
[55] systemfonts_1.0.6 teal.widgets_0.4.2.9011
[57] testthat_3.2.1.1 fontawesome_0.5.2
[59] jquerylib_0.1.4 tidyr_1.3.1
[61] glue_1.7.0 codetools_0.2-20
[63] ps_1.7.6 stringi_1.8.3
[65] gtable_0.3.5 later_1.3.2
[67] shinycssloaders_1.0.0 munsell_0.5.1
[69] tibble_3.2.1 logger_0.3.0
[71] pillar_1.9.0 htmltools_0.5.8.1
[73] brio_1.1.5 R6_2.5.1
[75] textshaping_0.3.7 Rdpack_2.6
[77] evaluate_0.23 lattice_0.22-6
[79] rbibutils_2.2.16 backports_1.4.1
[81] memoise_2.0.1 broom_1.0.5
[83] teal.reporter_0.3.1.9005 bslib_0.7.0
[85] httpuv_1.6.15 Rcpp_1.0.12
[87] shinyvalidate_0.1.3 coda_0.19-4.1
[89] nlme_3.1-164 checkmate_2.3.1
[91] xfun_0.43 zoo_1.8-12
[93] 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)
)