Usage
s_compare(x, .ref_group, .in_ref_col, ...)
# S3 method for numeric
s_compare(x, .ref_group, .in_ref_col, ...)
# S3 method for factor
s_compare(x, .ref_group, .in_ref_col, denom = "n", na.rm = TRUE, ...)
# S3 method for character
s_compare(
x,
.ref_group,
.in_ref_col,
denom = "n",
na.rm = TRUE,
.var,
verbose = TRUE,
...
)
# S3 method for logical
s_compare(x, .ref_group, .in_ref_col, na.rm = TRUE, denom = "n", ...)
a_compare(x, .ref_group, .in_ref_col, ..., .var)
# S3 method for numeric
a_compare(x, .ref_group, .in_ref_col, ...)
# S3 method for factor
a_compare(x, .ref_group, .in_ref_col, denom = "n", na.rm = TRUE, ...)
# S3 method for character
a_compare(
x,
.ref_group,
.in_ref_col,
denom = "n",
na.rm = TRUE,
.var,
verbose = TRUE,
...
)
# S3 method for logical
a_compare(x, .ref_group, .in_ref_col, na.rm = TRUE, denom = "n", ...)
compare_vars(
lyt,
vars,
var_labels = vars,
nested = TRUE,
...,
na_level = NA_character_,
show_labels = "default",
table_names = vars,
.stats = c("n", "mean_sd", "count_fraction", "pval"),
.formats = NULL,
.labels = NULL,
.indent_mods = NULL
)Arguments
- x
(
numeric)
vector of numbers we want to analyze.- .ref_group
(
data.frameorvector)
the data corresponding to the reference group.- .in_ref_col
(
logical)TRUEwhen working with the reference level,FALSEotherwise.- ...
arguments passed to
s_compare().- denom
(
string)
choice of denominator for factor proportions, can only ben(number of values in this row and column intersection).- na.rm
(
flag)
whetherNAvalues should be removed fromxprior to analysis.- .var
(
string)
single variable name that is passed byrtableswhen requested by a statistics function.- verbose
(
logical)
Whether warnings and messages should be printed. Mainly used to print out information about factor casting. Defaults toTRUE.- lyt
(
layout)
input layout where analyses will be added to.- vars
(
character)
variable names for the primary analysis variable to be iterated over.- var_labels
(
character)
character for label.- nested
(
flag)
whether this layout instruction be applied within the existing layout structure if possible (TRUE, the default) or as a new top-level element (FALSE). Ignored if it would nest a split underneath analyses, which is not allowed.- na_level
(
string)
string used to replace allNAor empty values in the output.- show_labels
(
string)
label visibility: one of "default", "visible" and "hidden".- table_names
(
character)
this can be customized in case that the samevarsare analyzed multiple times, to avoid warnings fromrtables.- .stats
(
character)
statistics to select for the table.- .formats
(named
characterorlist)
formats for the statistics.- .labels
(named
character)
labels for the statistics (without indent).- .indent_mods
(named
vectorofinteger)
indent modifiers for the labels. Each element of the vector should be a name-value pair with name corresponding to a statistic specified in.statsand value the indentation for that statistic's row label.
Value
s_compare()returns output ofs_summary()and comparisons versus the reference group in the form of p-values.
a_compare()returns the corresponding list with formattedrtables::CellValue().
compare_vars()returns a layout object suitable for passing to further layouting functions, or tortables::build_table(). Adding this function to anrtablelayout will add formatted rows containing the statistics froms_compare()to the table layout.
Functions
s_compare(): S3 generic function to produce a comparison summary.s_compare(numeric): Method fornumericclass. This uses the standard t-test to calculate the p-value.s_compare(factor): Method forfactorclass. This uses the chi-squared test to calculate the p-value.s_compare(character): Method forcharacterclass. This makes an automatic conversion tofactor(with a warning) and then forwards to the method for factors.s_compare(logical): Method forlogicalclass. A chi-squared test is used. If missing values are not removed, then they are counted asFALSE.a_compare(): Formatted analysis function which is used asafunincompare_vars().a_compare(numeric): Formatted analysis function method fornumericclass.a_compare(factor): Formatted analysis function method forfactorclass.a_compare(character): Formatted analysis function method forcharacterclass.a_compare(logical): Formatted analysis function method forlogicalclass.compare_vars(): Layout-creating function which can take statistics function arguments and additional format arguments. This function is a wrapper forrtables::analyze().
Note
For factor variables,
denomfor factor proportions can only bensince the purpose is to compare proportions between columns, therefore a row-based proportion would not make sense. Proportion based onN_colwould be difficult since we use counts for the chi-squared test statistic, therefore missing values should be accounted for as explicit factor levels.If factor variables contain
NA, theseNAvalues are excluded by default. To includeNAvalues setna.rm = FALSEand missing values will be displayed as anNAlevel. Alternatively, an explicit factor level can be defined forNAvalues during pre-processing viadf_explicit_na()- the defaultna_level("<Missing>") will also be excluded whenna.rmis set toTRUE.For character variables, automatic conversion to factor does not guarantee that the table will be generated correctly. In particular for sparse tables this very likely can fail. Therefore it is always better to manually convert character variables to factors during pre-processing.
For
compare_vars(), the column split must define a reference group viaref_groupso that the comparison is well defined.
See also
Relevant constructor function create_afun_compare(), and s_summary() which is used internally
to compute a summary within s_compare().
Examples
# `s_compare.numeric`
## Usual case where both this and the reference group vector have more than 1 value.
s_compare(rnorm(10, 5, 1), .ref_group = rnorm(5, -5, 1), .in_ref_col = FALSE)
#> $n
#> n
#> 10
#>
#> $sum
#> sum
#> 49.33208
#>
#> $mean
#> mean
#> 4.933208
#>
#> $sd
#> sd
#> 0.6592892
#>
#> $se
#> se
#> 0.2084856
#>
#> $mean_sd
#> mean sd
#> 4.9332084 0.6592892
#>
#> $mean_se
#> mean se
#> 4.9332084 0.2084856
#>
#> $mean_ci
#> mean_ci_lwr mean_ci_upr
#> 4.461581 5.404836
#> attr(,"label")
#> [1] "Mean 95% CI"
#>
#> $mean_sei
#> mean_sei_lwr mean_sei_upr
#> 4.724723 5.141694
#> attr(,"label")
#> [1] "Mean -/+ 1xSE"
#>
#> $mean_sdi
#> mean_sdi_lwr mean_sdi_upr
#> 4.273919 5.592498
#> attr(,"label")
#> [1] "Mean -/+ 1xSD"
#>
#> $mean_pval
#> p_value
#> 2.051419e-09
#> attr(,"label")
#> [1] "Mean p-value (H0: mean = 0)"
#>
#> $median
#> median
#> 4.905561
#>
#> $mad
#> mad
#> 4.440892e-16
#>
#> $median_ci
#> median_ci_lwr median_ci_upr
#> 4.278735 5.716378
#> attr(,"conf_level")
#> [1] 0.9785156
#> attr(,"label")
#> [1] "Median 95% CI"
#>
#> $quantiles
#> quantile_0.25 quantile_0.75
#> 4.797474 5.143685
#> attr(,"label")
#> [1] "25% and 75%-ile"
#>
#> $iqr
#> iqr
#> 0.3462109
#>
#> $range
#> min max
#> 3.662966 6.011961
#>
#> $min
#> min
#> 3.662966
#>
#> $max
#> max
#> 6.011961
#>
#> $median_range
#> median min max
#> 4.905561 3.662966 6.011961
#> attr(,"label")
#> [1] "Median (Min - Max)"
#>
#> $cv
#> cv
#> 13.36431
#>
#> $geom_mean
#> geom_mean
#> 4.891893
#>
#> $geom_mean_ci
#> mean_ci_lwr mean_ci_upr
#> 4.430296 5.401584
#> attr(,"label")
#> [1] "Geometric Mean 95% CI"
#>
#> $geom_cv
#> geom_cv
#> 13.92178
#>
#> $pval
#> [1] 3.396106e-05
#>
## If one group has not more than 1 value, then p-value is not calculated.
s_compare(rnorm(10, 5, 1), .ref_group = 1, .in_ref_col = FALSE)
#> $n
#> n
#> 10
#>
#> $sum
#> sum
#> 46.68639
#>
#> $mean
#> mean
#> 4.668639
#>
#> $sd
#> sd
#> 0.7003379
#>
#> $se
#> se
#> 0.2214663
#>
#> $mean_sd
#> mean sd
#> 4.6686395 0.7003379
#>
#> $mean_se
#> mean se
#> 4.6686395 0.2214663
#>
#> $mean_ci
#> mean_ci_lwr mean_ci_upr
#> 4.167648 5.169631
#> attr(,"label")
#> [1] "Mean 95% CI"
#>
#> $mean_sei
#> mean_sei_lwr mean_sei_upr
#> 4.447173 4.890106
#> attr(,"label")
#> [1] "Mean -/+ 1xSE"
#>
#> $mean_sdi
#> mean_sdi_lwr mean_sdi_upr
#> 3.968302 5.368977
#> attr(,"label")
#> [1] "Mean -/+ 1xSD"
#>
#> $mean_pval
#> p_value
#> 5.705574e-09
#> attr(,"label")
#> [1] "Mean p-value (H0: mean = 0)"
#>
#> $median
#> median
#> 4.570029
#>
#> $mad
#> mad
#> 4.440892e-16
#>
#> $median_ci
#> median_ci_lwr median_ci_upr
#> 3.970773 5.342752
#> attr(,"conf_level")
#> [1] 0.9785156
#> attr(,"label")
#> [1] "Median 95% CI"
#>
#> $quantiles
#> quantile_0.25 quantile_0.75
#> 4.103653 5.314028
#> attr(,"label")
#> [1] "25% and 75%-ile"
#>
#> $iqr
#> iqr
#> 1.210374
#>
#> $range
#> min max
#> 3.788968 5.924275
#>
#> $min
#> min
#> 3.788968
#>
#> $max
#> max
#> 5.924275
#>
#> $median_range
#> median min max
#> 4.570029 3.788968 5.924275
#> attr(,"label")
#> [1] "Median (Min - Max)"
#>
#> $cv
#> cv
#> 15.0009
#>
#> $geom_mean
#> geom_mean
#> 4.622594
#>
#> $geom_mean_ci
#> mean_ci_lwr mean_ci_upr
#> 4.159142 5.137689
#> attr(,"label")
#> [1] "Geometric Mean 95% CI"
#>
#> $geom_cv
#> geom_cv
#> 14.84936
#>
#> $pval
#> character(0)
#>
## Empty numeric does not fail, it returns NA-filled items and no p-value.
s_compare(numeric(), .ref_group = numeric(), .in_ref_col = FALSE)
#> $n
#> n
#> 0
#>
#> $sum
#> sum
#> NA
#>
#> $mean
#> mean
#> NA
#>
#> $sd
#> sd
#> NA
#>
#> $se
#> se
#> NA
#>
#> $mean_sd
#> mean sd
#> NA NA
#>
#> $mean_se
#> mean se
#> NA NA
#>
#> $mean_ci
#> mean_ci_lwr mean_ci_upr
#> NA NA
#> attr(,"label")
#> [1] "Mean 95% CI"
#>
#> $mean_sei
#> mean_sei_lwr mean_sei_upr
#> NA NA
#> attr(,"label")
#> [1] "Mean -/+ 1xSE"
#>
#> $mean_sdi
#> mean_sdi_lwr mean_sdi_upr
#> NA NA
#> attr(,"label")
#> [1] "Mean -/+ 1xSD"
#>
#> $mean_pval
#> p_value
#> NA
#> attr(,"label")
#> [1] "Mean p-value (H0: mean = 0)"
#>
#> $median
#> median
#> NA
#>
#> $mad
#> mad
#> NA
#>
#> $median_ci
#> median_ci_lwr median_ci_upr
#> NA NA
#> attr(,"conf_level")
#> [1] NA
#> attr(,"label")
#> [1] "Median 95% CI"
#>
#> $quantiles
#> quantile_0.25 quantile_0.75
#> NA NA
#> attr(,"label")
#> [1] "25% and 75%-ile"
#>
#> $iqr
#> iqr
#> NA
#>
#> $range
#> min max
#> NA NA
#>
#> $min
#> min
#> NA
#>
#> $max
#> max
#> NA
#>
#> $median_range
#> median min max
#> NA NA NA
#> attr(,"label")
#> [1] "Median (Min - Max)"
#>
#> $cv
#> cv
#> NA
#>
#> $geom_mean
#> geom_mean
#> NaN
#>
#> $geom_mean_ci
#> mean_ci_lwr mean_ci_upr
#> NA NA
#> attr(,"label")
#> [1] "Geometric Mean 95% CI"
#>
#> $geom_cv
#> geom_cv
#> NA
#>
#> $pval
#> character(0)
#>
# `s_compare.factor`
## Basic usage:
x <- factor(c("a", "a", "b", "c", "a"))
y <- factor(c("a", "b", "c"))
s_compare(x = x, .ref_group = y, .in_ref_col = FALSE)
#> $n
#> [1] 5
#>
#> $count
#> $count$a
#> [1] 3
#>
#> $count$b
#> [1] 1
#>
#> $count$c
#> [1] 1
#>
#>
#> $count_fraction
#> $count_fraction$a
#> [1] 3.0 0.6
#>
#> $count_fraction$b
#> [1] 1.0 0.2
#>
#> $count_fraction$c
#> [1] 1.0 0.2
#>
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.7659283
#>
## Management of NA values.
x <- explicit_na(factor(c("a", "a", "b", "c", "a", NA, NA)))
y <- explicit_na(factor(c("a", "b", "c", NA)))
s_compare(x = x, .ref_group = y, .in_ref_col = FALSE, na.rm = TRUE)
#> $n
#> [1] 5
#>
#> $count
#> $count$a
#> [1] 3
#>
#> $count$b
#> [1] 1
#>
#> $count$c
#> [1] 1
#>
#>
#> $count_fraction
#> $count_fraction$a
#> [1] 3.0 0.6
#>
#> $count_fraction$b
#> [1] 1.0 0.2
#>
#> $count_fraction$c
#> [1] 1.0 0.2
#>
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.7659283
#>
s_compare(x = x, .ref_group = y, .in_ref_col = FALSE, na.rm = FALSE)
#> $n
#> [1] 7
#>
#> $count
#> $count$a
#> [1] 3
#>
#> $count$b
#> [1] 1
#>
#> $count$c
#> [1] 1
#>
#> $count$`<Missing>`
#> [1] 2
#>
#>
#> $count_fraction
#> $count_fraction$a
#> [1] 3.0000000 0.4285714
#>
#> $count_fraction$b
#> [1] 1.0000000 0.1428571
#>
#> $count_fraction$c
#> [1] 1.0000000 0.1428571
#>
#> $count_fraction$`<Missing>`
#> [1] 2.0000000 0.2857143
#>
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.9063036
#>
# `s_compare.character`
## Basic usage:
x <- c("a", "a", "b", "c", "a")
y <- c("a", "b", "c")
s_compare(x, .ref_group = y, .in_ref_col = FALSE, .var = "x", verbose = FALSE)
#> $n
#> [1] 5
#>
#> $count
#> $count$a
#> [1] 3
#>
#> $count$b
#> [1] 1
#>
#> $count$c
#> [1] 1
#>
#>
#> $count_fraction
#> $count_fraction$a
#> [1] 3.0 0.6
#>
#> $count_fraction$b
#> [1] 1.0 0.2
#>
#> $count_fraction$c
#> [1] 1.0 0.2
#>
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.7659283
#>
## Note that missing values handling can make a large difference:
x <- c("a", "a", "b", "c", "a", NA)
y <- c("a", "b", "c", rep(NA, 20))
s_compare(x,
.ref_group = y, .in_ref_col = FALSE,
.var = "x", verbose = FALSE
)
#> $n
#> [1] 5
#>
#> $count
#> $count$a
#> [1] 3
#>
#> $count$b
#> [1] 1
#>
#> $count$c
#> [1] 1
#>
#>
#> $count_fraction
#> $count_fraction$a
#> [1] 3.0 0.6
#>
#> $count_fraction$b
#> [1] 1.0 0.2
#>
#> $count_fraction$c
#> [1] 1.0 0.2
#>
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.7659283
#>
s_compare(x,
.ref_group = y, .in_ref_col = FALSE, .var = "x",
na.rm = FALSE, verbose = FALSE
)
#> $n
#> [1] 6
#>
#> $count
#> $count$a
#> [1] 3
#>
#> $count$b
#> [1] 1
#>
#> $count$c
#> [1] 1
#>
#> $count$`<Missing>`
#> [1] 1
#>
#>
#> $count_fraction
#> $count_fraction$a
#> [1] 3.0 0.5
#>
#> $count_fraction$b
#> [1] 1.0000000 0.1666667
#>
#> $count_fraction$c
#> [1] 1.0000000 0.1666667
#>
#> $count_fraction$`<Missing>`
#> [1] 1.0000000 0.1666667
#>
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.005768471
#>
# `s_compare.logical`
## Basic usage:
x <- c(TRUE, FALSE, TRUE, TRUE)
y <- c(FALSE, FALSE, TRUE)
s_compare(x, .ref_group = y, .in_ref_col = FALSE)
#> $n
#> [1] 4
#>
#> $count
#> [1] 3
#>
#> $count_fraction
#> [1] 3.00 0.75
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.2702894
#>
## Management of NA values.
x <- c(NA, TRUE, FALSE)
y <- c(NA, NA, NA, NA, FALSE)
s_compare(x, .ref_group = y, .in_ref_col = FALSE, na.rm = TRUE)
#> $n
#> [1] 2
#>
#> $count
#> [1] 1
#>
#> $count_fraction
#> [1] 1.0 0.5
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.3864762
#>
s_compare(x, .ref_group = y, .in_ref_col = FALSE, na.rm = FALSE)
#> $n
#> [1] 3
#>
#> $count
#> [1] 1
#>
#> $count_fraction
#> [1] 1.0000000 0.3333333
#>
#> $n_blq
#> [1] 0
#>
#> $pval
#> [1] 0.1675463
#>
# `a_compare.numeric`
a_compare(
rnorm(10, 5, 1),
.ref_group = rnorm(20, -5, 1),
.in_ref_col = FALSE,
.var = "bla"
)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#> row_name formatted_cell indent_mod
#> 1 n 10 0
#> 2 sum 47.6 0
#> 3 mean 4.8 0
#> 4 sd 0.7 0
#> 5 se 0.2 0
#> 6 mean_sd 4.8 (0.7) 0
#> 7 mean_se 4.8 (0.2) 0
#> 8 mean_ci (4.23, 5.30) 0
#> 9 mean_sei (4.53, 5.00) 0
#> 10 mean_sdi (4.01, 5.51) 0
#> 11 mean_pval 0.00 0
#> 12 median 4.9 0
#> 13 mad 0.0 0
#> 14 median_ci (4.01, 5.36) 0
#> 15 quantiles 4.2 - 5.3 0
#> 16 iqr 1.1 0
#> 17 range 3.2 - 5.7 0
#> 18 min 3.2 0
#> 19 max 5.7 0
#> 20 median_range 4.9 (3.2 - 5.7) 0
#> 21 cv 15.7 0
#> 22 geom_mean 4.7 0
#> 23 geom_mean_ci 4.15658278859885, 5.32303985313443 0
#> 24 geom_cv 17.4 0
#> 25 pval <0.0001 0
#> row_label
#> 1 n
#> 2 Sum
#> 3 Mean
#> 4 SD
#> 5 SE
#> 6 Mean (SD)
#> 7 Mean (SE)
#> 8 Mean 95% CI
#> 9 Mean -/+ 1xSE
#> 10 Mean -/+ 1xSD
#> 11 Mean p-value (H0: mean = 0)
#> 12 Median
#> 13 Median Absolute Deviation
#> 14 Median 95% CI
#> 15 25% and 75%-ile
#> 16 IQR
#> 17 Min - Max
#> 18 Minimum
#> 19 Maximum
#> 20 Median (Min - Max)
#> 21 CV (%)
#> 22 Geometric Mean
#> 23 Geometric Mean 95% CI
#> 24 CV % Geometric Mean
#> 25 p-value (t-test)
# `a_compare.factor`
# We need to ungroup `count` and `count_fraction` first so that the `rtables` formatting
# functions can be applied correctly.
afun <- make_afun(
getS3method("a_compare", "factor"),
.ungroup_stats = c("count", "count_fraction")
)
x <- factor(c("a", "a", "b", "c", "a"))
y <- factor(c("a", "a", "b", "c"))
afun(x, .ref_group = y, .in_ref_col = FALSE)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#> row_name formatted_cell indent_mod row_label
#> 1 n 5 0 n
#> 2 a 3 0 a
#> 3 b 1 0 b
#> 4 c 1 0 c
#> 5 a 3 (60%) 0 a
#> 6 b 1 (20%) 0 b
#> 7 c 1 (20%) 0 c
#> 8 n_blq 0 0 n_blq
#> 9 pval 0.9560 0 p-value (chi-squared test)
# `a_compare.character`
afun <- make_afun(
getS3method("a_compare", "character"),
.ungroup_stats = c("count", "count_fraction")
)
x <- c("A", "B", "A", "C")
y <- c("B", "A", "C")
afun(x, .ref_group = y, .in_ref_col = FALSE, .var = "x", verbose = FALSE)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#> row_name formatted_cell indent_mod row_label
#> 1 n 4 0 n
#> 2 A 2 0 A
#> 3 B 1 0 B
#> 4 C 1 0 C
#> 5 A 2 (50%) 0 A
#> 6 B 1 (25%) 0 B
#> 7 C 1 (25%) 0 C
#> 8 n_blq 0 0 n_blq
#> 9 pval 0.9074 0 p-value (chi-squared test)
# `a_compare.logical`
afun <- make_afun(
getS3method("a_compare", "logical")
)
x <- c(TRUE, FALSE, FALSE, TRUE, TRUE)
y <- c(TRUE, FALSE)
afun(x, .ref_group = y, .in_ref_col = FALSE)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#> row_name formatted_cell indent_mod row_label
#> 1 n 5 0 n
#> 2 count 3 0 count
#> 3 count_fraction 3 (60%) 0 count_fraction
#> 4 n_blq 0 0 n_blq
#> 5 pval 0.8091 0 p-value (chi-squared test)
# `compare_vars()` in `rtables` pipelines
## Default output within a `rtables` pipeline.
lyt <- basic_table() %>%
split_cols_by("ARMCD", ref_group = "ARM B") %>%
compare_vars(c("AGE", "SEX"))
build_table(lyt, tern_ex_adsl)
#> ARM B ARM A ARM C
#> ———————————————————————————————————————————————————————————————————
#> AGE
#> n 73 69 58
#> Mean (SD) 35.8 (7.1) 34.1 (6.8) 36.1 (7.4)
#> p-value (t-test) 0.1446 0.8212
#> SEX
#> n 73 69 58
#> F 40 (54.8%) 38 (55.1%) 32 (55.2%)
#> M 33 (45.2%) 31 (44.9%) 26 (44.8%)
#> p-value (chi-squared test) 1.0000 1.0000
## Select and format statistics output.
lyt <- basic_table() %>%
split_cols_by("ARMCD", ref_group = "ARM C") %>%
compare_vars(
vars = "AGE",
.stats = c("mean_sd", "pval"),
.formats = c(mean_sd = "xx.x, xx.x"),
.labels = c(mean_sd = "Mean, SD")
)
build_table(lyt, df = tern_ex_adsl)
#> ARM C ARM A ARM B
#> ————————————————————————————————————————————————————
#> Mean, SD 36.1, 7.4 34.1, 6.8 35.8, 7.1
#> p-value (t-test) 0.1176 0.8212