sim_adrf | R Documentation |
sim_adrf()
is a wrapper for sim_apply()
that computes average dose-response functions (ADRFs) and average marginal effect functions (AMEFs). An ADRF describes the relationship between values a focal variable can take and the expected value of the outcome were all units to be given each value of the variable. An AMEF describes the relationship between values a focal variable can take and the derivative of ADRF at each value.
sim_adrf(
sim,
var,
subset = NULL,
by = NULL,
contrast = "adrf",
at = NULL,
n = 21,
outcome = NULL,
type = NULL,
eps = 1e-05,
verbose = TRUE,
cl = NULL
)
## S3 method for class 'clarify_adrf'
print(x, digits = NULL, max.ests = 6, ...)
sim |
a |
var |
the name of a variable for which the ADRF or AMEF is to be computed. This variable must be present in the model supplied to |
subset |
optional; a vector used to subset the data used to compute the ADRF or AMEF. This will be evaluated within the original dataset used to fit the model using |
by |
a one-sided formula or character vector containing the names of variables for which to stratify the estimates. Each quantity will be computed within each level of the complete cross of the variables specified in |
contrast |
a string naming the type of quantity to be produced: |
at |
the levels of the variable named in |
n |
when |
outcome |
a string containing the name of the outcome or outcome level for multivariate (multiple outcomes) or multi-category outcomes. Ignored for univariate (single outcome) and binary outcomes. |
type |
a string containing the type of predicted values (e.g., the link or the response). Passed to |
eps |
when |
verbose |
|
cl |
a cluster object created by |
x |
a |
digits |
the minimum number of significant digits to be used; passed to |
max.ests |
the maximum number of estimates to display. |
... |
optional arguments passed to |
The ADRF is composed of average marginal means across levels of the focal predictor. For each level of the focal predictor, predicted values of the outcome are computed after setting the value of the predictor to that level, and those values of the outcome are averaged across all units in the sample to arrive at an average marginal mean. Thus, the ADRF represent the relationship between the "dose" (i.e., the level of the focal predictor) and the average "response" (i.e., the outcome variable). It is the continuous analog to the average marginal effect computed for a binary predictor, e.g., using sim_ame()
. Although inference can be at each level of the predictor or between two levels of the predictor, typically a plot of the ADRF is the most useful relevant quantity. These can be requested using plot.clarify_adrf()
.
The AMEF is the derivative of the ADRF; if we call the derivative of the ADRF at each point a "treatment effect" (i.e., the rate at which the outcome changes corresponding to a small change in the predictor, or "treatment"), the AMEF is a function that relates the size of the treatment effect to the level of the treatment. The shape of the AMEF is usually of less importance than the value of the AMEF at each level of the predictor, which corresponds to the size of the treatment effect at the corresponding level. The AMEF is computed by computing the ADRF at each level of the focal predictor specified in at
, shifting the predictor value by a tiny amount (control by eps
), and computing the ratio of the change in the outcome to the shift, then averaging this value across all units. This quantity is related the the average marginal effect of a continuous predictor as computed by sim_ame()
, but rather than average these treatment effects across all observed levels of the treatment, the AMEF is a function evaluated at each possible level of the treatment. The "tiny amount" used is eps
times the standard deviation of var
.
A clarify_adrf
object, which inherits from clarify_est
and is similar to
the output of sim_apply()
, with the additional attributes "var"
containing
the variable named in var
, "by"
containing the names of the variables specified in by
(if any), "at"
containing values at which the ADRF or AMEF is evaluated, and "contrast"
containing the argument supplied to contrast
. For an ADRF, the average marginal means will be named
E[Y({v})]
, where {v}
is replaced with the values in at
. For an AMEF, the average marginal effects will be
named dY/d({x})|{a}
where {x}
is replaced with var
and {a}
is replaced by the values in at
.
plot.clarify_adrf()
for plotting the ADRF or AMEF; sim_ame()
for computing average marginal effects; sim_apply()
, which provides a general interface to computing any
quantities for simulation-based inference; summary.clarify_est()
for computing
p-values and confidence intervals for the estimated quantities.
marginaleffects::avg_slopes()
and marginaleffects::avg_predictions()
for delta method-based implementations of computing average marginal effects and average marginal means.
data("lalonde", package = "MatchIt")
# Fit the model
fit <- glm(I(re78 > 0) ~ treat + age + race +
married + re74,
data = lalonde, family = binomial)
# Simulate coefficients
set.seed(123)
s <- sim(fit, n = 100)
# ADRF for `age`
est <- sim_adrf(s, var = "age",
at = seq(15, 55, length.out = 6),
verbose = FALSE)
est
plot(est)
# AMEF for `age`
est <- sim_adrf(s, var = "age", contrast = "amef",
at = seq(15, 55, length.out = 6),
verbose = FALSE)
est
summary(est)
plot(est)
# ADRF for `age` within levels of `married`
est <- sim_adrf(s, var = "age",
at = seq(15, 55, length.out = 6),
by = ~married,
verbose = FALSE)
est
plot(est)
## Difference between ADRFs
est_diff <- est[7:12] - est[1:6]
plot(est_diff) + ggplot2::labs(y = "Diff")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.