Computes the standardized difference, group 1 minus group 0, for each
named variable, choosing the formula by the variable's type. A port of the
CCF %stddiff SAS macro (Artis 2019, from Yang 2012), and intended to
reproduce its est.stddiff output.
Usage
ps_stddiff(
data,
treatment_col = "tavr",
gaussian = NULL,
nong_ord = NULL,
binary = NULL,
categorical = NULL,
weight_col = NULL,
treated_level = NULL
)Arguments
- data
A data frame.
- treatment_col
Name of the group column, coded 0/1 or logical, with both groups present. Default
"tavr".- gaussian, nong_ord, binary, categorical
Character vectors of column names, by type. See Details. At least one variable is required, and a variable may appear under one type only. A
binarycolumn must be coded 0/1 or logical; acategoricalcolumn may have any coding.- weight_col
Optional name of a column of positive weights, for example the inverse-probability weights from
ps_weight()or the matching weightmt_wtfromps_logistic(). Weights must be finite.- treated_level
The value of
treatment_colthat is treated.NULL(default) requires a 0/1 or logical column and takes 1 orTRUE. Give a value, for example"transcatheter", to use a column holding any two values.
Value
An object of class c("ps_stddiff", "ps_data") with:
$dataThe input data frame, unchanged.
$metaNamed list:
treatment_col,weight_col,variables(a list by type),method,n_total,n_dropped.$tablesNamed list with
stddiff, a data frame with one row per variable in the order given:variable,label(the column's"label"attribute, orNA),type("gaussian","nong_ord","binary","categorical"or"onelevel") andstddiff, which isNAwhen the pooled variance is zero.
Details
Variable types. The type decides the formula, so it is given, not inferred: an ordinal NYHA class stored as an integer would otherwise be treated as Gaussian.
gaussian: difference in means oversqrt((var1 + var0) / 2). The denominator averages the two variances; it does not pool them by sample size, so it differs from a Cohen's d when the groups are unequal.nong_ord: the same formula on ranks of the pooled sample, ties averaged, asPROC RANKdoes by default. Ranks are unweighted; only their means and variances useweight_col.binaryandcategoricalare routed by the number of levels observed, as the macro routes itsBINARY=andCATG=lists: one level gives 0, two use the binary formula on the proportion at the last level in sort order (1for a 0/1 variable, the last level for a factor), and more than two use the Mahalanobis form of Yang and Dalton (2012). For that form the last level in sort order is the one dropped, andShas diagonal(t_i (1 - t_i) + c_i (1 - c_i)) / 2and off-diagonal-(t_i t_j + c_i c_j) / 2. A categorical difference is never negative. WhenSis singular, as when the two groups share no levels, the difference isNAwith a warning naming the variable.
Weights. Within each group, the weights are first divided by their
mean in that group, so that they average 1 there. Means are then
sum(w x) / sum(w) and variances sum(w (x - m)^2) / (n - 1), which is
what PROC MEANS reports with a WEIGHT statement and VARDEF=DF. The
result is therefore the same when every weight in a group is multiplied by
one constant, so stabilised and unstabilised weights from ps_weight()
give the same differences. It equals the %stddiff macro's output when the
weights already average 1 in each group, and otherwise does not: the
macro's Gaussian and rank differences change with the scale of the weights.
Proportions, for binary and categorical, are sum(w x) / sum(w) within
a group and never depended on that scale. Weights must be positive.
Missing values. Rows with a missing group are dropped. Otherwise a
missing value is dropped for that variable only, and a variable with no
non-missing values gets NA.
Units. The result is a proportion of a standard deviation, not a percent. The SAS output labels the column "(%)" but does not multiply by 100.
References
Yang D, Dalton JE. A unified approach to measuring the effect size between two groups using SAS. SAS Global Forum 2012, paper 335-2012.
Examples
dta <- sample_ps_data(n = 200, seed = 42)
obj <- ps_stddiff(dta, treatment_col = "tavr",
gaussian = c("age", "ef"), binary = c("female", "diabetes"))
obj$tables$stddiff
#> variable label type stddiff
#> 1 age <NA> gaussian -0.79935800
#> 2 ef <NA> gaussian 0.61385077
#> 3 female <NA> binary 0.39993164
#> 4 diabetes <NA> binary -0.05555041