Skip to contents

The Qini curve shows, for each share of units treated in order of their estimated effects (highest first), the estimated gain per unit on the whole list. The area between it and a random ranking's line is the Qini coefficient (qini_coef()).

Usage

qini_curve(data, ...)

# S3 method for class 'data.frame'
qini_curve(
  data,
  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 are an error.

case_weights

Not supported yet; must be NULL.

Value

A tibble of class qini_df, with one row per point, from the start of the curve to its end (for grouped data, the grouping columns come first):

  • .threshold: the lowest estimate in the top group (Inf at the start, where no unit is treated).

  • .n_treated, .n_control: the number of treated and control units in the top group.

  • .share: the share of all units in the top group.

  • .gain: the gain per unit on the whole list from treating the top group: the difference between the treated and control units' mean outcomes in the top group, times .share.

Details

Units with the same estimate enter the curve together, so there is one point per distinct estimate. A point where the top group lacks treated or control units has no observed difference, so it is left out. See qini_coef() for how the curve is defined and what it assumes.

A random ranking's line is .share times the curve's last .gain, the overall difference in means.

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