Skip to contents

Splits units into bins by their estimated effect and compares each bin's mean estimate with its observed effect, the difference between the mean outcomes of its treated and control units. If the estimates are well calibrated, the two are close in every bin. The Qini coefficient (qini_coef()) only checks the order of the estimates; this table also checks their size, which matters when a decision depends on how large an effect is (e.g., whether it is worth the cost of treating).

Usage

cate_cal_table(data, ...)

# S3 method for class 'data.frame'
cate_cal_table(
  data,
  truth,
  estimate,
  treatment,
  treated_level = NULL,
  event_level = "first",
  bins = 10,
  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.

bins

The number of bins, a whole number (default 10). Bin 1 holds the smallest estimates.

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 with one row per bin (for grouped data, the grouping columns come first):

  • .bin: the bin number, from 1 (smallest estimates) up.

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

  • .mean_estimate: the bin's mean estimated effect.

  • .observed: the bin's observed effect, the treated units' mean outcome minus the control units'.

  • .std_error: the standard error of .observed.

Details

Units are split into bins bins of equal size by their estimate, as dplyr::ntile() does. Units with the same estimate are then kept in the same bin (the lowest bin any of them landed in), so with ties some bins are larger than others, and there can be fewer bins than asked for: constant estimates give a single bin.

The standard error of a bin's observed effect is that of a difference in two means, sqrt(var(treated) / n_treated + var(control) / n_control). The observed effect is NA when a bin has no treated or no control units, and the standard error is NA when either group has fewer than two.

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. With small bins the observed effects are noisy, so compare the gaps with the standard errors.

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