Skip to contents

Mirrors the Python morie.calculate_ipw_weights(). Pure-R, no extra dependencies.

Usage

morie_calculate_ipw_weights(
  data,
  treatment,
  ps_col,
  stabilized = FALSE,
  trim_quantiles = NULL
)

Arguments

data

A data.frame containing treatment assignment and propensity scores.

treatment

Column name (string) of the binary treatment.

ps_col

Column name (string) of the propensity scores.

stabilized

If TRUE, return stabilised IPW weights. Default FALSE.

trim_quantiles

Optional length-2 numeric vector \((q_l, q_u)\) in \(`[0, 1]`\); if supplied, weights are clipped to the \(q_l\)-th and \(q_u\)-th quantiles of the unclipped weight distribution (Crump et al. 2009 trimming). Default NULL.

Value

Numeric vector of IPTW weights, length nrow(data).

Details

Standard IPW: \(w_i = T_i / e_i + (1 - T_i)/(1 - e_i)\), with the propensity score \(e_i\) clipped at [0.01, 0.99] for stability. Stabilised IPW replaces \(T\) and \(1 - T\) with the marginal treatment probability \(P(T = 1)\) and \(P(T = 0)\) respectively.

Examples

set.seed(1)
df <- data.frame(
  t = rbinom(100, 1, 0.4),
  ps = pmin(pmax(runif(100, 0.05, 0.95), 0.05), 0.95)
)
w <- morie_calculate_ipw_weights(df, treatment = "t", ps_col = "ps")
summary(w)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   1.060   1.355   1.916   2.636   3.268  13.997