
Descriptive Statistics for Univariate Data with Optional Reference Comparison
Source:R/s_summary_diff.r
s_summary_diff.RdComputes descriptive statistics for a single variable df[[.var]] using
tern::s_summary(), which dispatches type-specific methods depending on the
S3 class of the input (e.g., character, factor, logical, numeric).
Optionally, it computes a difference in means with confidence interval
between df[[.var]] and .ref_group[[.var]] using s_diff_mean_ci().
This statistic is applicable only to numeric variables.
Usage
s_summary_diff(
df,
.var,
.stats = NULL,
.ref_group = NULL,
.in_ref_col = FALSE,
control = tern::control_analyze_vars(),
...
)Arguments
- df
(
data.frame)
data set containing all analysis variables.- .var
(
character(1))
Name of the column indfcontaining the values for which statistics are computed. The variable type is handled by the corresponding methods oftern::s_summary(). Thediff_mean_cistatistic is only valid whendf[[.var]]is numeric.- .stats
(
characterorNULL)
Names of statistics to be computed. For numerical data, supported statistics are listed viatern::get_stats(method_groups = "analyze_vars_numeric", custom_stats_in = "diff_mean_ci"). IfNULL, all available statistics for numerical data are computed.- .ref_group
(
data.frameorvector)
the data corresponding to the reference group.- .in_ref_col
(
logical)TRUEwhen working with the reference level,FALSEotherwise.- control
(
list)
List of control options passed totern::s_summary(). If diff_mean_ci statistic is requested,control$conf_levelspecifies the confidence level used for the interval.- ...
Additional arguments passed to
tern::s_summary()and tos_diff_mean_ci()when diff_mean_ci is computed.
Examples
df <- data.frame(
USUBJID = c("X01", "X02", "X03", "X04", "X05"),
TRT01A = rep("ARM_A", 5),
PARAMCD = rep("SYSBP", 5),
AVISIT = rep("Visit 1", 5),
CHG = c(4, 1, -1, 9, -2)
)
df
#> USUBJID TRT01A PARAMCD AVISIT CHG
#> 1 X01 ARM_A SYSBP Visit 1 4
#> 2 X02 ARM_A SYSBP Visit 1 1
#> 3 X03 ARM_A SYSBP Visit 1 -1
#> 4 X04 ARM_A SYSBP Visit 1 9
#> 5 X05 ARM_A SYSBP Visit 1 -2
rg <- data.frame(
USUBJID = c("X06", "X07", "X08", "X09", "X10"),
TRT01A = rep("Placebo", 5),
PARAMCD = rep("SYSBP", 5),
AVISIT = rep("Visit 1", 5),
CHG = c(-2, 6, -2, 5, 2)
)
rg
#> USUBJID TRT01A PARAMCD AVISIT CHG
#> 1 X06 Placebo SYSBP Visit 1 -2
#> 2 X07 Placebo SYSBP Visit 1 6
#> 3 X08 Placebo SYSBP Visit 1 -2
#> 4 X09 Placebo SYSBP Visit 1 5
#> 5 X10 Placebo SYSBP Visit 1 2
.stats <- c("n", "mean_sd", "diff_mean_ci")
# With reference group.
s_summary_diff(df, "CHG", .stats, rg)
#> $n
#> n
#> 5
#>
#> $mean_sd
#> mean sd
#> 2.200000 4.438468
#>
#> $diff_mean_ci
#> diff_mean ci_lwr ci_upr
#> 0.400000 -5.632096 6.432096
#> attr(,"label")
#> [1] "Difference in Means + 95% CI"
#>
# Using df as reference.
s_summary_diff(df, "CHG", .stats, df, .in_ref_col = TRUE)
#> $n
#> n
#> 5
#>
#> $mean_sd
#> mean sd
#> 2.200000 4.438468
#>
#> $diff_mean_ci
#> NULL
#>