S7 class for influence function estimands with forward mode automatic differentiation for variance estimation.
You can install the development version of ife from GitHub with:
# install.packages("pak")
::pak("nt-williams/ife") pak
Consider estimating the population mean outcome under treatment \(E[E[Y \mid A=1,W]]\) and control \(E[E[Y \mid A=0,W]]\) using augmented inverse probability weighting (AIPW).
library(ife)
# Generate simulated data
<- 500
n <- runif(n) # confounder
w <- rbinom(n, 1, 0.5) # treatment (randomized)
a <- rbinom(n, 1, plogis(-0.75 + a + w)) # outcome
y
# Create data-frames for counterfactual predictions
<- data.frame(w, a, y)
foo <- foo0 <- foo
foo1 $a <- 1 # everyone treated
foo1$a <- 0 # everyone untreated
foo0
# Fit outcome model and generate predictions
<- 0.5 # known propensity score
pi <- glm(y ~ a + w, data = foo, family = binomial())
m <- predict(m, type = "response") # predicted outcomes
Qa <- predict(m, newdata = foo1, type = "response") # under treatment
Q1 <- predict(m, newdata = foo0, type = "response") # under control
Q0
# Calculate un-centered influence functions
<- a / pi * (y - Qa) + Q1
if1 <- (1 - a) / (1 - pi) * (y - Qa) + Q0 if0
Create ife objects for these estimates using
influence_func_estimate()
or ife()
:
<- influence_func_estimate(mean(if1), if1)
ife1 <- ife(mean(if0), if0) ife0
ife then allows you to estimate contrasts between estimates, with variance estimated using automatic differentiation. The additive effect (risk difference) can be calculated as:
- ife0
ife1 #> Estimate: 0.254
#> Std. error: 0.042
#> 95% Conf. int.: 0.172, 0.336
The multiplicative effect (risk ratio) can be estimated as:
/ ife0
ife1 #> Estimate: 1.583
#> Std. error: 0.129
#> 95% Conf. int.: 1.33, 1.837
For the risk ratio, which is strictly positive, you can estimate the effect on the log scale and exponentiate the confidence intervals to ensure the lower bound is always positive:
exp(log(ife1 / ife0)@conf_int)
#> [1] 1.35 1.86