Skip to contents

The Qini coefficient measures how well estimated effects rank units: do the units with the highest estimated effects really gain the most from treatment? It is the area between the Qini curve (qini_curve()) and the line a random ranking would give. It needs only the observed outcomes, not the true effects, so it works on real data.

Usage

qini_coef(data, ...)

# S3 method for class 'data.frame'
qini_coef(
  data,
  truth,
  estimate,
  treatment,
  treated_level = NULL,
  event_level = "first",
  na_rm = TRUE,
  case_weights = NULL,
  ...
)

qini_coef_vec(
  truth,
  estimate,
  treatment,
  treated_level = NULL,
  event_level = "first",
  na_rm = TRUE,
  case_weights = NULL,
  ...
)

Arguments

data

A data frame with the observed outcome, the estimated effect, and the treatment, such as the output of augment() on a test set. For a grouped data frame, there is one result per group.

...

Not currently used.

truth

The observed outcome column, unquoted: numeric, or a factor with two levels for a binary outcome. For qini_coef_vec(), a vector.

estimate

The estimated effect column, unquoted, such as .pred_cate. For qini_coef_vec(), a numeric vector.

treatment

The treatment column, unquoted (e.g., treatment = emailed), coded as in fit(): numeric 0/1, logical, or a factor or character column with exactly two values. For qini_coef_vec(), a vector.

treated_level

For a factor or character treatment, the value that means treated. Required for those treatments and not allowed for 0/1 or TRUE/FALSE treatments, where 1 or TRUE means treated.

event_level

For a binary outcome, whether the "first" (default) or "second" level of truth is the event, following the yardstick convention. Effects are then differences in the share of units with the event. Ignored for numeric outcomes.

na_rm

If TRUE (default), rows with a missing value in any of the three columns are dropped. If FALSE, missing values make the result NA.

case_weights

Not supported yet; must be NULL.

Value

For qini_coef(), a tibble with columns .metric, .estimator, and .estimate, and one row (one per group for grouped data). For qini_coef_vec(), a single number.

Details

The Qini curve sorts units by their estimated effect, highest first. For each share of units at the top of that list, its height is the gain per unit on the whole list from treating only that top group: the difference between the mean outcome of the treated and the control units in the top group, times the share. The random line runs straight from the start of the curve, (0, 0), to its end, where everyone is treated and the height is the overall difference in means. The Qini coefficient is the area between the two, computed with the trapezoid rule, so it is in the outcome's units per unit (e.g., dollars per customer).

A positive value means the ranking beats a random one, and constant estimates give 0. The best possible value depends on the data, so compare learners on the same data and outcome only. Unlike some Python packages (causalml, scikit-uplift), the coefficient isn't divided by the area of a perfect ranking's curve, and the curve is the uplift curve of Gutierrez and Gérardy (2017), which is not affected by the share of units treated, so values won't match theirs.

The observed outcomes only reveal effects when the treatment was assigned at random: the Qini coefficient assumes a completely randomized experiment in which every unit had the same chance of being treated (any chance, e.g., 50%). Score units the learner wasn't fit on, such as a test set.

The coefficient is noisy: with a few hundred units, its sampling error can be larger than the differences between learners, and even a random ranking can score well. Standard errors aren't available yet; to see how much the coefficient varies, compute it on bootstrap samples of the test set (e.g., with rsample::bootstraps()).

qini_coef() and uplift_at_k() are yardstick metrics, so they can be combined with yardstick::metric_set(), which passes treatment and treated_level on to them. Other metrics in the same set, such as yardstick::rmse(), ignore the treatment.

A group of grouped data that lacks treated or control units gets NA, with a warning.

References

Gutierrez, P., & Gérardy, J.-Y. (2017). Causal inference and uplift modelling: A review of the literature. Proceedings of Machine Learning Research, 67, 1-13. https://proceedings.mlr.press/v67/gutierrez17a.html

Radcliffe, N. J., & Surry, P. D. (2011). Real-world uplift modelling with significance-based uplift trees (White paper TR-2011-1). Stochastic Solutions.

Examples

# A randomized email campaign: the email raises spending more for customers
# who bought recently
set.seed(1)
n <- 2000
recency <- sample(1:12, n, replace = TRUE)
emailed <- rbinom(n, size = 1, prob = 0.5)
spend <- 20 - recency + emailed * (10 - 0.75 * recency) + rnorm(n, sd = 5)
emails <- data.frame(recency, emailed, spend)
train <- emails[1:1000, ]
test <- emails[1001:2000, ]

wf <- workflows::workflow(spend ~ recency, parsnip::linear_reg())
fitted <- t_learner(wf) |>
  fit(data = train, treatment = emailed)

# Score the estimated effects on customers the learner wasn't fit on
scored <- augment(fitted, new_data = test)

qini_coef(scored, truth = spend, estimate = .pred_cate, treatment = emailed)
#> # A tibble: 1 × 3
#>   .metric   .estimator .estimate
#>   <chr>     <chr>          <dbl>
#> 1 qini_coef standard       0.600
uplift_at_k(scored, truth = spend, estimate = .pred_cate, treatment = emailed)
#> # A tibble: 1 × 3
#>   .metric     .estimator .estimate
#>   <chr>       <chr>          <dbl>
#> 1 uplift_at_k standard        7.64

# Both metrics at once
uplift_metrics <- yardstick::metric_set(qini_coef, uplift_at_k)
uplift_metrics(scored, truth = spend, estimate = .pred_cate, treatment = emailed)
#> # A tibble: 2 × 3
#>   .metric     .estimator .estimate
#>   <chr>       <chr>          <dbl>
#> 1 qini_coef   standard       0.600
#> 2 uplift_at_k standard       7.64 

# The curve, and whether the estimates match the observed effects
qini_curve(scored, truth = spend, estimate = .pred_cate, treatment = emailed)
#> # A tibble: 13 × 5
#>    .threshold .n_treated .n_control .share .gain
#>         <dbl>      <int>      <int>  <dbl> <dbl>
#>  1     Inf             0          0  0     0    
#>  2       9.30         34         34  0.068 0.532
#>  3       8.55         82         74  0.156 1.16 
#>  4       7.80        123        111  0.234 1.83 
#>  5       7.05        155        163  0.318 2.43 
#>  6       6.30        196        209  0.405 3.02 
#>  7       5.55        229        249  0.478 3.44 
#>  8       4.80        274        295  0.569 4.06 
#>  9       4.05        316        340  0.656 4.39 
#> 10       3.30        361        392  0.753 4.65 
#> 11       2.56        402        441  0.843 4.73 
#> 12       1.81        440        484  0.924 4.88 
#> 13       1.06        470        530  1     5.21 
cate_cal_table(scored, truth = spend, estimate = .pred_cate, treatment = emailed,
               bins = 5)
#> # A tibble: 5 × 6
#>    .bin .n_treated .n_control .mean_estimate .observed .std_error
#>   <int>      <int>      <int>          <dbl>     <dbl>      <dbl>
#> 1     1        109        138           1.85      1.31      0.642
#> 2     2         87         97           3.66      3.04      0.783
#> 3     3        119        132           5.54      6.37      0.645
#> 4     4         73         89           7.41      7.40      0.898
#> 5     5         82         74           8.88      7.44      0.769