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")
Run the code above in your browser using DataLab