Code
os_est <- function(z, y, x, out.family = gaussian, truncps = c(0, 1), e1 = 1, e0 = 0) {
propensity_score <- glm(z ~ x, family = binomial)$fitted.values
propensity_score <- pmax(truncps[1], pmin(truncps[2], propensity_score))
outcome_regression_1 <- glm(y ~ x, weights = z, family = out.family)$fitted.values
outcome_regression_0 <- glm(y ~ x, weights = 1 - z, family = out.family)$fitted.values
# Estimador 1
tau_hat_1 <- mean(z * y) + mean((1 - z) * outcome_regression_1 / e1) -
mean(z * outcome_regression_0 * e0) - mean((1 - z) * y)
# Estimador 2 e 3
w1 <- propensity_score + (1 - propensity_score) / e1
w0 <- propensity_score * e0 + 1 - propensity_score
tau_hat_2 <- mean(w1 * z * y / propensity_score) - mean(w0 * (1 - z) * y / (1 - propensity_score))
tau_hat_3 <- mean(w1 * z * y / propensity_score)/mean(z / propensity_score) - mean(w0 * (1 - z) * y / (1 - propensity_score)) / mean((1 - z) / (1 - propensity_score))
# Estimador 4
tau_hat_4 <- tau_hat_2 - mean( (z - propensity_score) * (outcome_regression_1 / (propensity_score * e1) + outcome_regression_0 * e0 / (1 - propensity_score)))
return(c(tau_hat_1, tau_hat_2, tau_hat_3, tau_hat_4))
}