plot.maq: Plot the estimated Qini curve.

View source: R/plot.R

plot.maqR Documentation

Plot the estimated Qini curve.

Description

Plot the estimated curve Q(B), B \in (0, B_{max}]. If the underlying estimated policy \pi_B entails treating zero units (that is, all the estimated treatment effects are negative) then this function returns an empty value.

Usage

## S3 method for class 'maq'
plot(
  x,
  ...,
  add = FALSE,
  horizontal.line = TRUE,
  ci.args = list(),
  grid.step = NULL
)

Arguments

x

A maq object.

...

Additional arguments passed to plot.

add

Whether to add to an already existing plot. Default is FALSE.

horizontal.line

Whether to draw a horizontal line where the Qini curve plateaus. Only applies if the maq object is fit with a maximum budget that is sufficient to treat all units that are expected to benefit. Default is TRUE.

ci.args

A list of optional arguments to lines() for drawing 95 % confidence bars. Set to NULL to ignore CIs.

grid.step

The spend grid increment size to plot the curve on. Default is max(floor(length(path.length) / 1000), 1) where path.length is the size of the grid underlying the estimated Qini curve.

Value

A data.frame with the data making up the plot (point estimates and lower/upper 95% CIs)

Examples


if (require("ggplot2", quietly = TRUE)) {
# Generate toy data and customize plots.
n <- 500
K <- 1
reward <- matrix(1 + rnorm(n * K), n, K)
scores <- reward + matrix(rnorm(n * K), n, K)
cost <- 1

# Fit Qini curves.
qini.avg <- maq(reward, cost, scores, R = 200, target.with.covariates = FALSE)
qini <- maq(reward, cost, scores, R = 200)

# In some settings we may want to plot using one of R's many plot libraries.
# The plot method invisibly returns the plot data we can use for this purpose.
df.qini.baseline <- plot(qini.avg)
df.qini <- plot(qini, add = TRUE, col = 2)

# Make an alternate plot style, using, for example, ggplot.
ggplot(df.qini, aes(x = spend, y = gain)) +
  geom_ribbon(aes(ymin = gain - 1.96 * std.err,
                  ymax = gain + 1.96 * std.err),
              fill = "lightgray") +
  geom_line(linewidth = 2) +
  ylab("Policy value") +
  xlab("Fraction treated") +
  geom_line(data = df.qini.baseline, aes(x = spend, y = gain), lty = 2)
}



maq documentation built on April 15, 2025, 1:35 a.m.

Related to plot.maq in maq...