if (FALSE) {
library(marginaleffects)
library(magrittr)
set.seed(1024)
mod <- lm(Sepal.Length ~ Sepal.Width * Species, data = iris)
# bootstrap
avg_predictions(mod, by = "Species") %>%
inferences(method = "boot")
avg_predictions(mod, by = "Species") %>%
inferences(method = "rsample")
# Fractional (bayesian) bootstrap
avg_slopes(mod, by = "Species") %>%
inferences(method = "fwb") %>%
get_draws("rvar") %>%
data.frame()
# Simulation-based inference
slopes(mod) %>%
inferences(method = "simulation") %>%
head()
# Two-step estimation procedure: Propensity score + G-Computation
lalonde <- get_dataset("lalonde")
estimator <- function(data) {
# Step 1: Estimate propensity scores
fit1 <- glm(treat ~ age + educ + race, family = binomial, data = data)
ps <- predict(fit1, type = "response")
# Step 2: Fit weighted outcome model
m <- lm(re78 ~ treat * (re75 + age + educ + race),
data = data, weight = ps
)
# Step 3: Compute average treatment effect by G-computation
avg_comparisons(m, variables = "treat", wts = ps, vcov = FALSE)
}
inferences(lalonde, method = "rsample", estimator = estimator)
}
Run the code above in your browser using DataLab