Skip to content

Commit 3f69db1

Browse files
authored
Update IJMPR example (#1489)
1 parent 48dc917 commit 3f69db1

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

experiments/ijmpr/analysis.R

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,41 +111,48 @@ print(rate.cate)
111111
# We can use the `maq` package for this exercise. This package is more general
112112
# and accepts CATE estimates from multiple treatment arms along with costs that
113113
# denominate what we spend by assigning a unit a treatment. In this application
114-
# we can simply treat the number of units we are considering deploying as the cost.
114+
# we can simply set cost to 1 and compute a Qini curve with fraction treated on
115+
# the x-axis.
115116

116-
# Form a doubly robust estimate of a CATE-based Qini curve (using eval.forest).
117-
num.units = nrow(X)
118-
qini = maq(tau.hat.test,
119-
num.units,
120-
get_scores(eval.forest) * num.units,
121-
R = 200)
117+
# Form a doubly robust estimate of the Qini curve, using R=200 bootstrap replications for std.errors.
118+
cost = 1
119+
qini = maq(tau.hat.test, cost, get_scores(eval.forest), R = 200)
122120

123121
# Form a baseline Qini curve that assigns treatment uniformly.
124-
qini.baseline = maq(tau.hat.test,
125-
num.units,
126-
get_scores(eval.forest) * num.units,
127-
R = 200,
122+
qini.baseline = maq(tau.hat.test, cost, get_scores(eval.forest), R = 200,
128123
target.with.covariates = FALSE)
129124

130-
# Plot the Qini curve along with 95% confidence lines.
131-
plot(qini, ylab = "PTSD cases prevented", xlab = "Units held back from deployment", xlim = c(0, num.units))
132-
plot(qini.baseline, add = TRUE, ci.args = NULL)
125+
# Plot the Qini curve with 95% confidence intervals.
126+
# This shows the expected gain from withholding units from deployments in order of predicted benefit.
127+
128+
# Suppose the application allows intervening on up to 2000 units.
129+
# We use `maq_scale()` to rescale the Qini curve to this real-world deployment size.
130+
max.deployment = 2000
131+
132+
# Plot the scaled Qini curve.
133+
scale_maq(qini, max.deployment) |>
134+
plot(ylab = "PTSD cases prevented",
135+
xlab = "Units held back from deployment")
136+
137+
# Add a baseline curve on top.
138+
scale_maq(qini.baseline, max.deployment) |>
139+
plot(add = TRUE, ci.args = NULL)
133140

134141
# Get estimates from the curve, at for example 500 deployed units.
135-
average_gain(qini, 500)
142+
average_gain(scale_maq(qini, max.deployment), 500)
136143

137144
# Compare the benefit of targeting the 500 units predicted to benefit the most with the baseline.
138-
difference_gain(qini, qini.baseline, 500)
139-
145+
difference_gain(scale_maq(qini, max.deployment),
146+
scale_maq(qini.baseline, max.deployment), 500)
140147

141148
# [The paper shows Qini curves embellished with ggplot. We could have retrieved
142149
# the data underlying the curves and customized our plots further.
143-
# For more details we refer to https://github.com/grf-labs/maq]
150+
# For examples see https://grf-labs.github.io/maq/reference/plot.maq.html]
144151

145152

146153
# *** Describing the fit CATE function ****
147154

148-
# Our `cate.forest` has given us some estimated function \tau(x).
155+
# Our `cate.forest` has given us some estimated function \hat tau().
149156
# Let's have a closer look at how this function stratifies our sample in terms of "covariate" profiles.
150157
# One way to do so is to look at histograms of our covariates by for example low / high CATE predictions.
151158

0 commit comments

Comments
 (0)