Skip to contents

Calculate quantile-quantile data comparing the distribution of a variable between treatment groups for a single weighting scheme (or unweighted). This function computes the quantiles for both groups and returns a data frame suitable for plotting or further analysis.

Usage

bal_qq(
  .data,
  .var,
  .exposure,
  .weights = NULL,
  quantiles = seq(0.01, 0.99, 0.01),
  .reference_level = NULL,
  na.rm = FALSE
)

Arguments

.data

A data frame containing the variables.

.var

Variable to compute quantiles for (unquoted).

.exposure

Column name of treatment/group variable (unquoted).

.weights

Optional single weight variable (unquoted). If NULL, computes unweighted quantiles.

quantiles

Numeric vector of quantiles to compute. Default is seq(0.01, 0.99, 0.01) for 99 quantiles.

.reference_level

The level of .exposure to treat as the reference, the unexposed group whose quantiles are returned in unexposed_quantiles. Either a level of .exposure or its position among the observed levels. If NULL (default), the first observed level is used.

na.rm

Logical. If FALSE (default), missing values in .var, .exposure, or .weights raise an error. If TRUE, rows with missing values are dropped before computation.

Value

A tibble with columns:

quantile

Numeric. The quantile probability (0-1).

exposed_quantiles

Numeric. The quantile value for the exposed group, the level of .exposure that is not the reference level.

unexposed_quantiles

Numeric. The quantile value for the unexposed group, the reference level of .exposure.

Details

This function computes the data needed for quantile-quantile plots by calculating corresponding quantiles from two distributions. Unweighted quantiles come from stats::quantile(); weighted quantiles come from weighted_quantile(), which uses the same definition, so a constant weight reproduces the observed quantiles.

When the distributions of a variable are similar between treatment groups (indicating good balance), the QQ plot points will lie close to the diagonal line y = x.

See also

Examples

# Unweighted QQ data
bal_qq(nhefs_weights, age, qsmk)
#> # A tibble: 99 × 3
#>    quantile exposed_quantiles unexposed_quantiles
#>       <dbl>             <dbl>               <dbl>
#>  1     0.01                25                25  
#>  2     0.02                25                25  
#>  3     0.03                26                25  
#>  4     0.04                26                25.5
#>  5     0.05                27                26  
#>  6     0.06                27                26  
#>  7     0.07                28                26  
#>  8     0.08                28                27  
#>  9     0.09                29                27  
#> 10     0.1                 29                28  
#> # ℹ 89 more rows

# Weighted QQ data
bal_qq(nhefs_weights, age, qsmk, .weights = w_ate)
#> # A tibble: 99 × 3
#>    quantile exposed_quantiles unexposed_quantiles
#>       <dbl>             <dbl>               <dbl>
#>  1     0.01                25                25  
#>  2     0.02                25                25  
#>  3     0.03                26                25  
#>  4     0.04                26                26  
#>  5     0.05                26                26  
#>  6     0.06                26                26  
#>  7     0.07                27                27  
#>  8     0.08                27                27  
#>  9     0.09                28                27.1
#> 10     0.1                 28                28  
#> # ℹ 89 more rows

# Custom quantiles
bal_qq(nhefs_weights, age, qsmk, .weights = w_ate,
       quantiles = seq(0.1, 0.9, 0.1))
#> # A tibble: 9 × 3
#>   quantile exposed_quantiles unexposed_quantiles
#>      <dbl>             <dbl>               <dbl>
#> 1      0.1              28                    28
#> 2      0.2              32                    31
#> 3      0.3              34.8                  35
#> 4      0.4              39.1                  39
#> 5      0.5              43                    43
#> 6      0.6              47                    47
#> 7      0.7              51                    50
#> 8      0.8              55                    54
#> 9      0.9              60                    60