Created
October 7, 2017 12:04
-
-
Save johnmyleswhite/b25cd84ab8db7542cddeebdb813bcc55 to your computer and use it in GitHub Desktop.
Ratios in causal inference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("ggplot2") | |
library("dplyr") | |
# Population size | |
n <- 2500 | |
# Sessions per user if assigned to test | |
sessions_test <- as.integer(exp(rnorm(n, 0.5, 1))) | |
# Sessions per user if assigned to control | |
sessions_control <- as.integer(exp(rnorm(n, 0.0, 1))) | |
# Has sessions variables are transformations of these. | |
has_hessions_test <- as.integer(sessions_test > 0) | |
has_hessions_control <- as.integer(sessions_control > 0) | |
# Given assignment, estimate the estimand in a naive way | |
analyze <- function( | |
sessions_test, | |
sessions_control, | |
assignments | |
) { | |
ratio_test <- ( | |
( | |
sum(sessions_test * assignments) | |
) | |
/ | |
( | |
sum(sessions_test * assignments > 0) | |
) | |
) | |
ratio_control <- ( | |
( | |
sum(sessions_control * (1 - assignments)) | |
) | |
/ | |
( | |
sum(sessions_control * (1 - assignments) > 0) | |
) | |
) | |
estimator <- ratio_test / ratio_control | |
return(estimator) | |
} | |
monte_carlo <- function( | |
sessions_test, | |
sessions_control, | |
n_sims=10000L | |
) { | |
estimators <- rep(NA, n_sims) | |
for (s in 1:n_sims) { | |
assignments <- rbinom(n, 1, 0.5) | |
estimators[s] <- analyze( | |
sessions_test, | |
sessions_control, | |
assignments | |
) | |
} | |
return(estimators) | |
} | |
estimand <- ( | |
( | |
sum(sessions_test) / sum(has_hessions_test) | |
) | |
/ | |
( | |
sum(sessions_control) / sum(has_hessions_control) | |
) | |
) | |
estimators <- monte_carlo(sessions_test, sessions_control) | |
hist(estimators - estimand) | |
mean(estimators) | |
estimand |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment