# Estimating causal parameters using geex

## IPW estimator of counterfactual mean

An example $$\psi$$ function written in R. This function computes the score functions for a GLM, plus two counterfactual means estimated by inverse probability weighting.

eefun <- function(data, model, alpha){
X <- model.matrix(model, data = data)
A <- model.response(model.frame(model, data = data))
Y <- data$Y function(theta){ p <- length(theta) p1 <- length(coef(model)) lp <- X %*% theta[1:p1] rho <- plogis(lp) hh <- ((rho/alpha)^A * ((1-rho)/(1-alpha))^(1 - A)) IPW <- 1/(exp(sum(log(hh)))) score_eqns <- apply(X, 2, function(x) sum((A - rho) * x)) ce0 <- mean(Y * (A == 0)) * IPW / (1 - alpha) ce1 <- mean(Y * (A == 1)) * IPW / (alpha) c(score_eqns, ce0 - theta[p - 1], ce1 - theta[p]) } } Compare to what inferference gets. library(geex) library(inferference) test <- interference( formula = Y | A ~ X1 | group, data = vaccinesim, model_method = 'glm', allocations = c(.35, .4)) ## [1] "Calculating matrix of IP weights..." ## [1] "Calculating array of IP weight derivatives..." ## [1] "Calculating matrix of scores..." ## [1] "Computing effect estimates..." ## [1] "Interference complete" mglm <- glm(A ~ X1, data = vaccinesim, family = binomial) ce_estimates <- m_estimate( estFUN = eefun, data = vaccinesim, units = 'group', root_control = setup_root_control(start = c(coef(mglm), .4, .13)), outer_args = list(alpha = .35, model = mglm) ) roots(ce_estimates) ## (Intercept) X1 ## -0.36869683 -0.02037916 0.42186669 0.15507946 # Compare parameter estimates direct_effect(test, allocation = .35)$estimate
## [1] 0.2667871
roots(ce_estimates)[3] - roots(ce_estimates)[4]
##
## 0.2667872
# conpare SE estimates
L <- c(0, 0, 1, -1)
Sigma <- vcov(ce_estimates)
sqrt(t(L) %*% Sigma %*% L)  # from GEEX
##            [,1]
## [1,] 0.03716025
direct_effect(test, allocation = .35)\$std.error # from inferference
## [1] 0.02602315

I would expect them to be somewhat different, since inferference uses a slightly different variance estimator defined in the web appendix of Perez-Heydrich et al (2014).