# \donttest{
trial <- Trial$new(
covariates = function(n) data.frame(a = rbinom(n, 1, 0.5), x = rnorm(n)),
outcome = setargs(outcome_count,
mean = ~ 1 + a*x,
par = c(1, -0.1, 0.5, 0.2),
overdispersion = 2)
)
dd <- trial$simulate(1e4)
# equivalent specifications to estimate log(E[Y(1)] / E[Y(0)])
estimators <- list(
est_adj(family = poisson, treatment.effect = "logrr"),
est_glm(family = poisson),
est_adj(response = y ~ a, family = poisson, treatment.effect = "logrr"),
est_adj(response = targeted::learner_glm(y ~ a, family = poisson),
treatment.effect = "logrr"
)
)
lapply(estimators, \(est) est(dd))
# now with covariates, estimating E[Y(1)] - E[Y(0)]
estimators <- list(
est_adj(covariates = "x", family = poisson),
est_adj(response = y ~ a * x, family = poisson),
est_adj(response = targeted::learner_glm(y ~ a * x, family = poisson))
)
lapply(estimators, \(est) est(dd))
# custom treatment.effect function
estimator <- est_adj(response = y ~ a * x, family = poisson,
treatment.effect = \(x) x[2] - x[1] # x[1] contains the estimate of E[Y(0)]
)
estimator(dd)
dd_factor <- dd
# when using factors, the control/comparator treatment needs to be the first
# level to estimate the contrasts defined by the `treatment.level` argument
estimator <- est_adj(response = y ~ a * x, family = poisson)
dd_factor$a <- factor(dd_factor$a, levels = c(0, 1))
estimator(dd_factor) # E[Y(1)] - E[Y(0)]
dd_factor$a <- factor(dd_factor$a, levels = c(1, 0))
estimator(dd_factor) # E[Y(1)] - E[Y(0)]
# }
Run the code above in your browser using DataLab