Skip to contents

Returns new_data with each unit's estimated effect added as the first column, .pred_cate, ready for the uplift metrics such as qini_coef(). Works for every learner's fit.

Usage

# S3 method for class 'cate_fit'
augment(x, new_data, ...)

Arguments

x

A fitted learner, from fit() on any of s_learner(), t_learner(), x_learner(), r_learner(), or dr_learner().

new_data

A data frame with the predictors the learner needs (see its predict() method). It doesn't need the treatment column, but keep it, along with the outcome, to score the estimates.

...

Not currently used.

Value

A tibble: .pred_cate, followed by the columns of new_data.

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