Evaluate a causal estimator against a known DGP
causalsim_eval.RdRepeatedly draws datasets from a causalsim_dgp() object, applies a
user-supplied estimator to each draw, and returns a tidy summary of
estimator performance against the known ground truth.
Usage
causalsim_eval(
dgp,
estimator,
reps = 200L,
metrics = c("bias", "rmse", "coverage", "power"),
seed = NULL
)Arguments
- dgp
A
causalsim_dgpobject.- estimator
A function that accepts a data frame produced by
causalsim_draw()and returns a named numeric vector or single-row data frame. Must include anestimatefield. For"coverage"and"power"metrics, must also includeci_lowerandci_upper. See Details.- reps
Positive integer. Number of simulation replications. Default
200L.- metrics
Character vector. Subset of
"bias","rmse","coverage","power". Default: all four.- seed
Integer or
NULL. Passed toset.seed()before the first replication. DefaultNULL.
Value
An S3 object of class causalsim_eval with components:
metricsTidy data frame:
metric,value,se.drawsData frame of per-replication estimates, one row per rep.
true_ateThe DGP's true ATE.
repsNumber of replications run.
Details
Estimator convention
The estimator receives the full data frame returned by causalsim_draw(),
including ground-truth columns .tau and .p. It should return a named
numeric vector or one-row data frame with at minimum:
Metrics
"bias"Monte Carlo estimate of bias:
mean(estimate) - true_ate."rmse"Root mean squared error:
sqrt(mean((estimate - true_ate)^2))."coverage"Proportion of replications where the CI brackets
true_ate. Requiresci_lowerandci_upper."power"Proportion of replications where the CI excludes zero — i.e., the rejection rate of H0: ATE = 0. When
true_ate != 0, this is power; whentrue_ate == 0, it is the Type I error rate. Requiresci_lowerandci_upper.
Monte Carlo standard errors (MCSE) are reported alongside each metric.
For bias: sd(estimates) / sqrt(reps). For RMSE: delta-method
approximation. For coverage and power: Bernoulli SE.
Examples
dgp <- causalsim_dgp(n = 300, n_confounders = 1, effect = 2,
propensity = 0.5)
# OLS estimator (unbiased under RCT)
ols_estimator <- function(data) {
fit <- lm(Y ~ A + W, data = data)
est <- coef(fit)[["A"]]
se <- sqrt(vcov(fit)["A", "A"])
c(estimate = est, ci_lower = est - 1.96 * se, ci_upper = est + 1.96 * se)
}
result <- causalsim_eval(dgp, ols_estimator, reps = 100L, seed = 1L)
result
#> <causalsim_eval> reps: 100 true ATE: 2
#>
#> metric value se
#> bias -0.0061 0.01300
#> rmse 0.1295 0.00723
#> coverage 0.9300 0.02551
#> power 1.0000 0.00000