
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 estimates and associated inferential statistics for
the difference in population means between two underlying distributions,
based on observed samples df[[.var]] and .ref_group[[.var]] using
s_diff_means().
These statistics are applicable only to numeric data.
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(). If difference-in-means statistics are requested,df[[.var]]must be numeric.- .stats
(
characterorNULL)
Names of statistics to be computed. For numerical data, the names of supported statistics are listed viatern::get_stats()for statistics for a single variable andjunco_get_stats("diff_means")for difference-in-means statistics. 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 difference-in-means statistics are requested,control$conf_levelspecifies the confidence level of the interval.- ...
Additional named arguments passed to
s_diff_means(), exceptdf1,df2,.var, andconf.level, which are already passed.
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_means_n1", "diff_means_n2", "diff_means_est_ci")
# With reference group.
s_summary_diff(df, "CHG", .stats, rg)
#> $n
#> n
#> 5
#>
#> $mean_sd
#> mean sd
#> 2.200000 4.438468
#>
#> $diff_means_n1
#> diff_means_n1
#> 5
#> attr(,"label")
#> [1] "Difference in Means Sample Size (Group 1)"
#>
#> $diff_means_n2
#> diff_means_n2
#> 5
#> attr(,"label")
#> [1] "Difference in Means Sample Size (Group 2)"
#>
#> $diff_means_est_ci
#> diff_means_est diff_means_ci_lwr diff_means_ci_upr
#> 0.400000 -5.632096 6.432096
#> attr(,"conf.level")
#> [1] 0.95
#> 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_means_n1
#> NULL
#>
#> $diff_means_n2
#> NULL
#>
#> $diff_means_est_ci
#> NULL
#>