| Type: | Package |
| Title: | BiMaU Miscellaneous |
| Version: | 0.2.0 |
| Maintainer: | Anna Felip-Badia <annafelipibadia@gmail.com> |
| Description: | Contains a function to plot publication-ready survival curves using the Kaplan-Meier method (1958) <doi:10.2307/2281868>, a function to format p-values, and a function to automatically select statistical tests for comparing continuous variables between groups, which are useful for repetitive analyses. BiMaU stands for the Biostatistics and Mathematics Research Unit at the Sant Joan de Déu - Pediatric Cancer Center Barcelona https://github.com/BiMaU-PCCB. |
| License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
| Depends: | R (≥ 4.1.0) |
| Encoding: | UTF-8 |
| Imports: | exams, survival (≥ 3.8-6), latex2exp, plotfunctions, graphics, utils |
| Suggests: | gtsummary |
| Config/roxygen2/version: | 8.0.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-09-07 07:21:21 UTC; afelipb |
| Author: | Anna Felip-Badia |
| Repository: | CRAN |
| Date/Publication: | 2026-09-07 08:10:09 UTC |
BiMaU Miscellaneous
Description
Contains a function to plot publication-ready survival curves using the Kaplan-Meier method (1958), a function to format p-values, and a function to automatically select statistical tests for comparing continuous variables between groups, which are useful for repetitive analyses. BiMaU stands for the Biostatistics and Mathematics Research Unit at the Sant Joan de Déu - Pediatric Cancer Center Barcelona.
Functions
show.p, survival.plot, find.test
Author(s)
Maintainer: Anna Felip-Badia annafelipibadia@gmail.com (ORCID)
Authors:
Anna Felip-Badia annafelipibadia@gmail.com (ORCID)
Aleix Martín-Moral (ORCID)
Sara Perez-Jaume (ORCID)
References
Kaplan, E. L., & Meier, P. (1958). Nonparametric Estimation from Incomplete Observations. Journal of the American Statistical Association, 53(282), 457–481.
Automatically select statistical tests for quantitative variables
Description
This function evaluates quantitative variables across stratification groups and automatically determines the appropriate parametric or non-parametric hypothesis test.
See Details for an exhaustive explanation of the criteria followed.
Designed to work seamlessly with tbl_summary, this function generates the input lists for arguments test and test.args of the function add_p, specifying the recommended test function and any additional arguments different from the defaults.
Usage
find.test(data, variables, strata, alpha = 0.05)
Arguments
data |
name of the dataset, of class |
variables |
column name(s) of the quantitative variable(s) to analyse. Can be specified as a character string (with quotes) or as an unquoted symbol. |
strata |
strata indicator column name. Can be specified as a character string (with quotes) or as an unquoted symbol. Must be a factor with at least 2 levels. |
alpha |
significance level threshold used for normality ( |
Details
If any level of strata has less than five non-missing observations, no statistical test is performed for that variable, and a warning is issued.
Otherwise, the appropriate test is selected based on the following criteria:
Two Groups (strata with 2 levels):
-
t-test (
t.test): selected if sample sizes in both groups are\ge 30and Shapiro-Wilk tests indicate normality in both groups. Variance equality is tested using an F-test. -
Mann-Whitney test (
wilcox.test): selected if sample sizes are small, this is< 30in some group, or if normality is rejected in either group.
More than Two Groups (strata with > 2 levels):
-
ANOVA (
oneway.test): selected if ANOVA model residuals adhere to a normal distribution according to the Shapiro-Wilk test. Homoscedasticity is tested via Bartlett's test. -
Kruskal-Wallis test (
kruskal.test): selected if ANOVA residuals deviate from normality.
Value
A list of length 2 intended to be passed directly to arguments test and test.args of the function add_p when using tbl_summary:
testa list of formulas matching each variable name to its selected test string (e.g.,
age ~ "t.test").test.argsa list of formulas specifying additional test parameters (e.g.,
age ~ list(var.equal = TRUE)). Variables requiring no additional parameters are omitted from this second list.
Examples
df <- as.data.frame(gtsummary::trial)
df$trt <- factor(df$trt)
df$grade <- factor(df$grade)
# Two levels variable (trt: Drug A, Drug B)
tests2 <- find.test(df, variables = c(age, marker), strata = trt)
tests2
# Use outputs directly with tbl_summary()
gtsummary::tbl_summary(df, include = c(age, marker), by = trt) |>
gtsummary::add_p(test = tests2$test, test.args = tests2$test.args)
# Three levels variable (grade: I, II, III)
tests3 <- find.test(df, variables = c(age, marker), strata = grade)
tests3
# Use outputs directly with tbl_summary()
gtsummary::tbl_summary(df, include = c(age, marker), by = grade) |>
gtsummary::add_p(test = tests3$test, test.args = tests3$test.args)
# Using function show.p() from BiMaUmisc package
gtsummary::tbl_summary(df, include = c(age, marker), by = grade) |>
gtsummary::add_p(test = tests3$test, test.args = tests3$test.args,
pvalue_fun = function(x) show.p(x))
Format p-values for display
Description
Convert p-values to a compact textual representation. The function prints decimals up to the first non-zero decimal and the following one, applying standard rounding.
Usage
show.p(p, add.p = FALSE)
Arguments
p |
a value or vector of the p-value(s) to be shown, where |
add.p |
logical indicating whether the text p= (or p<, if it applies) should be added before the p-value. |
Details
The number of decimals shown depends on the magnitude of the p-value so that two meaningful digits are displayed after the leading zeros.
Specifically:
For
p \ge 0.1, two decimal places are shown.For
0.01 \le p < 0.1, three decimal places are shown.For
0.001 \le p < 0.01, four decimal places are shown.For
0.0001 \le p < 0.001, five decimal places are shown.For
p < 0.0001, the string"<0.0001"is returned.
This formatting ensures that the first non-zero decimal of the p-value and the following digit are displayed,
while avoiding an excessive number of leading zeros.
If add.p = TRUE, the prefix "*p*=" (or "*p*<" when applicable) is added to the formatted value.
This will ensure the p appears in cursive when calling the function in line on R markdown documents.
Value
A character vector with the formatted p-values.
Examples
show.p(0.00785)
show.p(c(0.03042, 0.1579, 0.0000025))
show.p(0.00785, add.p = TRUE)
show.p(c(0.03042, 0.1579, 0.0000025), add.p = TRUE)
Plot survival curves
Description
This function produces an elegant survival curve plot.
It can automatically append the number-at-risk table and display the log-tank test p-value (obtained from a call to survdiff).
Highly customizable, it offers numerous arguments to easily modify the plot's appearance.
Usage
survival.plot(data, time, status, strata = 1,
legend = TRUE, legend.pos = "bottomleft", legend.labs = NULL,
legend.title = NULL, legend.cex = 1,
events = FALSE, events.pos = NULL, events.text = NULL,
mark.time = TRUE, lwd = 3,
risk = TRUE, risk.text = TRUE, risk.labs = NULL, risk.cex = 1,
p.value = TRUE, p.value.pos = NULL,
units = "days", xlab = NULL, times = NULL,
yscale = 1, ylab = NULL,
main = "Survival curve", col = NULL, ...)
Arguments
data |
name of the dataset, of class |
time |
follow-up time column name. Can be specified as a character string (with quotes) or as an unquoted symbol. |
status |
status indicator column name. Can be specified as a character string (with quotes) or as an unquoted symbol. |
strata |
strata indicator column name. Can be specified as a character string (with quotes) or as an unquoted symbol. Up to four categories. Must be a factor. |
legend |
logical indicating whether a legend should be added to the plot. If |
legend.pos |
this argument can be a single keyword from the list in |
legend.labs |
a character vector of length |
legend.title |
title for the legend. If |
legend.cex |
legend's character expansion factor. A numerical value giving the amount by which the legend text should be magnified relative to the default.
If |
events |
logical indicating whether the number of events should be added to the text in the legend.
If |
events.pos |
2-dimensional numerical vector indicating the x-y coordinates where the number of events should be added to the plot.
If |
events.text |
a character indicating the text to display before the number of events in the plot.
If |
mark.time |
logical indicating whether the curves should be marked at each censoring time. |
lwd |
a number indicating the line width(s). Alternatively, when |
risk |
logical indicating whether the number-at-risk table should be appended to the plot. |
risk.text |
logical indicating whether the legend for the number-at-risk table should display text labels. When |
risk.labs |
|
risk.cex |
number-at-risk table's character expansion factor. A numerical value giving the amount by which the number-at-risk table text should be magnified relative to the default.
If |
p.value |
logical indicating whether the p-value obtained from a call to |
p.value.pos |
2-dimensional numerical vector indicating the x-y coordinates where the number of events should be added to the plot.
If |
units |
character indicating the time units. Options are: "days", "weeks", "months" or "years".
This argument will be used to create the default label and tick-marks for the x-axis. This argument is not case-sensitive and is passed to |
xlab |
character indicating a label for the x-axis. |
times |
numerical vector with the time points at which tick-marks are to be drawn in the x-axis.
|
yscale |
a numeric value used to multiply the labels on the y-axis. A value of 100, for instance, would be used to give a percent scale.
Notice only the labels are changed, not the actual plot coordinates. |
ylab |
character indicating a label for the y-axis. |
main |
overall title for the plot. |
col |
colour palette to be used. |
... |
other graphical parameters (to be passed to |
Value
A customized survival curve plot displaying the requested graphical and statistical modifications.
Note
A call to par is used in this function. Notice that the arguments
font.axis, font.lab, cex.lab, las and xpd are always set to 2, 2, 1.2, 1, and TRUE, respectively. Moreover,
the argument mar is always modified and depends on the number of levels in strata as well as whether or not the number-at-risk table is appended.
For optimal resolution and layout alignment when using the function in an R file, adjust the plot window.
For example, ideal dimensions are approximately 7 x 5” for 24” screens and 5.9 x 4.2” for 13” screens.
For optimal resolution and layout alignment when using the function in an R markdown file or similar, use fig.dim = c(7, 5) in the corresponding chunk.
Examples
lung <- survival::cancer
lung$time_y <- lung$time/365.25
lung$sex <- factor(lung$sex, levels = 1:2, labels = c("Male", "Female"))
# no strata
survival.plot(lung, time_y, status, units = "y")
# customizing
survival.plot(lung, time_y, status, units = "y",
xlab = "Time since diagnosis (in years)",
yscale = 100,
main = "Overall survival", mark.col = "darkgray")
# 2 strata
survival.plot(lung, time_y, status, sex, units = "y")
# customizing
survival.plot(lung, time_y, status, sex,
legend.pos = "topright", legend.title = "Sex",
events = TRUE, risk.text = FALSE, p.value.pos = c(2.4, 0.4),
units = "y", xlab = "Time since diagnosis (in years)",
main = "Overall survival", col = c("darkviolet", "darkgreen"))