Skip to contents

Calculates the Pearson correlation coefficient between two numeric vectors, with optional case weights. Uses the standard correlation formula for unweighted data and weighted covariance for weighted data.

Usage

bal_corr(x, y, weights = NULL, na.rm = FALSE)

Arguments

x

A numeric vector containing the first variable.

y

A numeric vector containing the second variable. Must have the same length as x.

weights

An optional numeric vector of case weights. If provided, must have the same length as x and y. All weights must be non-negative.

na.rm

A logical value indicating whether to remove missing values before computation. If FALSE (default), missing values result in NA output.

Value

A numeric value representing the correlation coefficient between -1 and 1. Returns NA if either variable has zero variance.

See also

check_balance() for computing multiple balance metrics at once

Other balance functions: bal_ks(), bal_smd(), bal_vr(), check_auc(), check_balance()

Examples

bal_corr(nhefs_weights$age, nhefs_weights$wt71)
#> [1] 0.02219162