Skip to contents

Uplift at k is the observed effect among the units with the highest estimated effects: the difference between the mean outcomes of the treated and the control units in the top share k (by default, the top 30%). It answers "if we treated only the top 30%, how much would treatment raise their outcome, on average?" Like qini_coef(), it needs only the observed outcomes, so it works on real data.

Usage

uplift_at_k(data, ...)

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

uplift_at_k_vec(
  truth,
  estimate,
  treatment,
  k = 0.3,
  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 uplift_at_k_vec(), a vector.

estimate

The estimated effect column, unquoted, such as .pred_cate. For uplift_at_k_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 uplift_at_k_vec(), a vector.

k

The share of units at the top of the ranking, a number above 0 and at most 1 (default 0.3, the top 30%).

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 uplift_at_k(), a tibble with columns .metric, .estimator, and .estimate, and one row (one per group for grouped data). For uplift_at_k_vec(), a single number.

Details

The top group is the ceiling(k * n) units with the highest estimates, taken from all units at once (treated and control together), plus any units tied with the last one in, so it can be a little larger. If it has no treated or no control units, the result is NA, with a warning.

Uplift at k is an effect per unit in the top group. The Qini curve's height at share k (qini_curve()) is about the same effect times k: the gain spread over every unit on the list.

It makes the same assumptions as qini_coef() (a completely randomized experiment in which every unit had the same chance of being treated, scored on units the learner wasn't fit on) and is just as noisy on small test sets: the top 30% of 500 units is only 150 units. It can be combined with qini_coef() in yardstick::metric_set(); k is then passed on to it too.

See also

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