Type: Package
Title: Compute Standardized Mean Differences
Version: 0.8.0
Description: Computes standardized mean differences and confidence intervals for multiple data types based on Yang, D., & Dalton, J. E. (2012) https://support.sas.com/resources/papers/proceedings12/335-2012.pdf.
Imports: MASS (≥ 7.3-50), methods (≥ 3.5.1)
Suggests: testthat, stddiff, tableone, knitr, dplyr, purrr, markdown, rmarkdown
License: MIT + file LICENSE
URL: https://bsaul.github.io/smd/
BugReports: https://github.com/bsaul/smd/issues
Encoding: UTF-8
RoxygenNote: 7.3.2
VignetteBuilder: knitr
Repository: CRAN
NeedsCompilation: no
Packaged: 2025-02-12 16:11:35 UTC; nixbld
Author: Bradley Saul [aut, cre], Alex Breskin [ctb], Catie Wiener [ctb], Matt Phelan [ctb], Daniel Sjoberg [ctb], Nuvan Rathnayaka [ctb], Malcolm Barrett [ctb]
Maintainer: Bradley Saul <bradleysaul@fastmail.com>
Date/Publication: 2025-02-12 17:50:02 UTC

Checks whether a vector has more than two unique values

Description

Checks whether a vector has more than two unique values

Usage

check_for_two_levels(x)

Arguments

x

a vector


Compute the standardized mean difference

Description

This function is for internal package use only. See smd for usage.

Usage

compute_smd_pairwise(smd_parts)

compute_smd(D, S)

Arguments

smd_parts

a list of components for from compute_smd_parts computing standardized mean differences

D

vector of differences for each level of a factor (will be length 1 for numeric values)

S

the covariance matrix

Details

Computes:

d = \sqrt{D' S^{-1} D}

where D is a vector of differences between group 1 and 2 and S is the covariance matrix of these differences. If D is length 1, the result is multplied by sign(D).

In the case of a numeric or integer variable, this is equivalent to:

d = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{(s^2_1 + s^2_2)/2}}

where \bar{x}_g is the sample mean for group g and s^2_g is the sample variance.

For a logical or factor with only two levels, the equation above is \bar{x}_g = \hat{p}_g, i.e. the sample proportion and s^2_g = \hat{p}_g(1 - \hat{p}_g) (NOTE: interally smd uses the var function, which uses n-1 as the denominator. Hence, in small samples, s^2_g will not be precisely \hat{p}_g(1 - \hat{p}_g)).

Value

a single numeric value

References

Yang, D., & Dalton, J. E. (2012, April). A unified approach to measuring the effect size between two groups using SASĀ®. In SAS Global Forum (Vol. 335, pp. 1-6)

See Also

smd


Compute components of SMD

Description

Computes D and S for use within compute_smd.

Usage

compute_smd_parts(.x, .g, .w, .na, .ref, .unwgt, applyFUN = n_mean_var)

Arguments

.x

a vector of values

.g

a vector of groupings to compare

.w

a vector of weights (optional)

.na

TRUE/FALSE. NA handling

.ref

integer position of the reference group

.unwgt

Use unweighted or weighted covariance.

applyFUN

the FUN used to compute the SMD parts. Defaults to n_mean_var


Computes SMD variance

Description

Calculates the variance of a standardized mean difference using the method of Hedges and Olkin (1985):

Usage

compute_smd_var(d, smd_parts)

Arguments

d

an SMD value

smd_parts

a list of components for from compute_smd_parts computing standardized mean differences

Details

\sqrt{\frac{n_1 + n_2}{n_1n_2} + \frac{d^2}{2(n_1 + n_2)}}


Apply a function pairwise along a list

Description

Applies a function f(ref, y) where y is an element of a list and ref is a reference element.

Usage

lapplyFUNpairwise(x, f, ref)

Arguments

x

a list

f

the function to apply

ref

the index of the reference element


Compute n, mean and variance

Description

Available for numeric, integer, and factor objects. character objects are handled by first converting to a factor.

Usage

n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'numeric,missing'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'numeric,numeric'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'numeric,ANY'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'integer,missing'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'integer,numeric'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'integer,ANY'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'logical,missing'
n_mean_var(x, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'logical,numeric'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'logical,ANY'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'factor,missing'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'factor,numeric'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'factor,ANY'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'character,missing'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

## S4 method for signature 'character,numeric'
n_mean_var(x, w = NULL, na.rm = TRUE, unwgt.var = TRUE)

## S4 method for signature 'character,ANY'
n_mean_var(x, w = NULL, na.rm = FALSE, unwgt.var = TRUE)

Arguments

x

a vector of values

w

an optional vector of numeric weights or a vector convertible with numeric with 'as.double()'.

na.rm

passed to sum

unwgt.var

Use unweighted or weighted covariance matrix

Value

a list containing mean and var


Compute Standardized Mean Difference

Description

Computes the standardized mean differnce (SMD) between two groups.

d = \sqrt{D' S^{-1} D}

where D is a vector of differences between group 1 and 2 and S is the covariance matrix of these differences. If D is length 1, the result is multplied by sign(D).

In the case of a numeric or integer variable, this is equivalent to:

d = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{(s^2_1 + s^2_2)/2}}

where \bar{x}_g is the sample mean for group g and s^2_g is the sample variance.

For a logical or factor with only two levels, the equation above is \bar{x}_g = \hat{p}_g, i.e. the sample proportion and s^2_g = \hat{p}_g(1 - \hat{p}_g).

When using the SMD to evaluate the effectiveness of weighting in achieving covariate balance, it is important to isolate the change in SMD before and after weighting to the change in mean difference, so the denominator (covariance matrix) must be held constant (Stuart 2008, doi:10.1002/sim.3207). By default, the unweighted covariance matrix is used to compute SMD in both the unweighted and weighted case. If the weights are not being used to adjust for covariate imbalance (e.g. case weights), the unwgt.var argument can be set to FALSE to use the weighted covariance matrix as the denominator.

Usage

smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'character,ANY,missing'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'character,ANY,numeric'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'logical,ANY,missing'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'logical,ANY,numeric'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'matrix,ANY,missing'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'matrix,ANY,numeric'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'list,ANY,missing'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'list,ANY,numeric'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'data.frame,ANY,missing'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

## S4 method for signature 'data.frame,ANY,numeric'
smd(x, g, w, std.error = FALSE, na.rm = FALSE, gref = 1L, unwgt.var = TRUE)

Arguments

x

a vector or matrix of values

g

a vector of at least 2 groups to compare. This should coercable to a factor.

w

a vector of numeric weights (optional)

std.error

Logical indicator for computing standard errors using compute_smd_var. Defaults to FALSE.

na.rm

Remove NA values from x? Defaults to FALSE.

gref

an integer indicating which level of g to use as the reference group. Defaults to 1.

unwgt.var

Use unweighted or weighted covariance matrix. Defaults to TRUE

Value

a data.frame containing standardized mean differences between levels of g for values of x. The data.frame contains the columns:

Examples

x <- rnorm(100)
g <- rep(1:2, each = 50)
smd(x, g)

Helper to clean up smd output

Description

Helper to clean up smd output

Usage

tidy_smd_singlevar(smd_res)

tidy_smd_multiplevar(smd_res)

Arguments

smd_res

a result of smd

Value

a data.frame


Variance computations

Description

Variance computations

Usage

multinom_var(p)

Arguments

p

a vector of proportions corresponding to the proportion in each group

Value

a covariance matrix