| expectation | R Documentation |
Estimates the mean parameter
\Psi = E{f(X; \theta)} by the empirical average of f(p, data)
over the rows of data, where the parameter \theta
is estimated by object. The influence function of the estimate
accounts for both the empirical averaging and the uncertainty of
\widehat\theta. Standardization and g-computation are special
applications of this function.
expectation(f, object, data, id, subset, labels = NULL, ...)
f |
function with named argument |
object |
model object ( |
data |
|
id |
(optional) ids of the values returned by |
subset |
(optional) subpopulation on which the average is conditioned
(conditional marginal mean estimate). Either a logical vector (or numeric
0/1 vector) with one value per value of |
labels |
(optional) character vector of parameter names |
... |
additional arguments passed to |
The influence function of the estimate is
\mathrm{IC}_\Psi(Z; P) = f(X;\theta) - \Psi +
[E\nabla_\theta f(X;\theta)]\,\phi(Z; P)
where \phi is the influence function of \widehat\theta.
The two terms are identified by different ids: the first term by id (the
rows of data), and the second term by the ids of the model, i.e.,
index(object) for estimate objects, or the row names of the influence
function (for model objects such as glm the row names of the model
frame). The model may therefore be estimated on another (e.g., smaller or
partly overlapping) dataset than data. The two terms are aligned on the
union of ids: each term is zero for ids outside its own support and
rescaled by the inverse proportion of observed ids (as in
merge.estimate). If there are no common ids, the model estimate and
data are treated as independent. Use estimate(object, id=...) to
assign ids (or clusters) to the model.
estimate object
estimate.default, vignette("influencefunction", package = "lava")
m <- lvm(y ~ x + z)
distribution(m, y ~ x) <- dist_bernoulli("logit")
d <- sim(m, 1000, seed = 1)
fit <- glm(y ~ z + x, data = d, family = binomial())
## Standardization (g-computation)
f <- function(p, data)
list(p0 = expit(p["(Intercept)"] + p["z"] * data[, "z"]),
p1 = expit(p["(Intercept)"] + p["x"] + p["z"] * data[, "z"]))
e <- expectation(f, fit)
e
estimate(e, diff)
## Model estimated on a subset, standardized over the full data
d$id <- paste0("i", seq_len(nrow(d)))
d$w <- rbinom(nrow(d), 1, 0.5)
d1 <- subset(d, w == 1)
g1 <- glm(y ~ x + z, data = d1, family = binomial)
e1 <- estimate(g1, id = d1$id)
expectation(f, e1, data = d, id = "id")
## Conditional marginal mean effects (subset) and clusters
d$cl <- rep(seq(nrow(d) / 4), each = 4)
expectation(function(p, data) expit(p[1] + p["z"] * data[, "z"]),
estimate(fit, id = d$cl),
data = d, subset = ~ z > 0, id = "cl")
## Simple mean of known function
expectation(~ 1 / (1+z^2), data=d, subset = ~x, labels="p")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.