1 .emulate_coding <- function(ground_truth, Q = 1) {
 
   3         return(sample(c(0, 1), size = 1, replace = TRUE))
 
   9 distort_gt <- function(x, Q = NULL) {
 
  10     return(purrr::map_dbl(x, .emulate_coding, Q = Q))
 
  13 N <- c(1000, 3600, 14400)
 
  17 Bxy <- c(0.1, 0.2, 0.5)
 
  21 conditions <- expand.grid(N, m, B0, Bxy, Q)
 
  23 colnames(conditions) <- c("N", "m", "B0", "Bxy", "Q")
 
  25 logistic <- function(x) {1/(1+exp(-1*x))}
 
  27 .step <- function(i, Bxy, B0, Q, N, m) {
 
  28     x <- rbinom(N, 1, 0.5)
 
  29     y <-  Bxy * x + rnorm(N, 0, .5) + B0
 
  31     dx <- as.numeric(distort_gt(x, Q = Q))
 
  33     randomidx <- sample(seq(N), m)
 
  35     coder1x <- distort_gt(x[randomidx], Q = Q)
 
  36     coder2x <- distort_gt(x[randomidx], Q = Q)
 
  37     coding_data <- matrix(c(as.numeric(coder1x), as.numeric(coder2x)), nrow = 2, byrow = TRUE)
 
  38     alpha <- irr::kripp.alpha(coding_data, method = "nominal")
 
  39     estimated_q <- alpha$value^(1/2)
 
  40     estimated_q2 <- alpha$value
 
  42     res <- data.frame(x = as.factor(x), y = y, dx = as.factor(dx))
 
  43     small_y <- y[randomidx]
 
  44     small_x <- x[randomidx]
 
  45     naive_mod <- glm(y~dx, data = res, x = TRUE, y = TRUE)
 
  46     real_mod <- glm(y~x, data = res, x = TRUE, y = TRUE)
 
  47     m1 <- glm(small_y~coder1x)
 
  48     m2 <- glm(small_y~coder2x)
 
  49     m3 <- glm(small_y~small_x)
 
  50     correct_only_idx <- coder1x == coder2x
 
  51     m4 <- glm(small_y[correct_only_idx] ~ small_x[correct_only_idx])
 
  52     lab_only_gt <- coef(m3)[2]
 
  53     lab_only_avg <- mean(coef(m1)[2], coef(m2)[2])
 
  54     lab_only_correct_only <- coef(m4)[2]    
 
  55     return(tibble::tibble(N, m, Q, Bxy, B0, estimated_q, naive_Bxy = as.numeric(coef(naive_mod)[2]), real_Bxy = as.numeric(coef(real_mod)[2]), lab_only_gt= lab_only_gt, lab_only_avg = lab_only_avg, lab_only_correct_only = lab_only_correct_only))
 
  60 ## for (i in seq(nrow(conditions))) {
 
  62 ##     res[[i]] <- purrr::map_dfr(1:100, ~.step(., conditions$Bxy[i], conditions$B0[i], conditions$Q[i], conditions$N[i], conditions$m[i]))
 
  68 .run <- function(i, conditions) {
 
  69     purrr::map_dfr(1:100, ~.step(., conditions$Bxy[i], conditions$B0[i], conditions$Q[i], conditions$N[i], conditions$m[i]))
 
  72 res <- future_map(seq(nrow(conditions)), .run, conditions = conditions, .progress = TRUE)
 
  74 ##saveRDS(res, "rubin_res.RDS")
 
  76 conditions <- tibble::as_tibble(conditions)
 
  81 conditions %>% mutate(loco_median = purrr::map_dbl(res, ~median(.$lab_only_correct_only)), loco_p025 = purrr::map_dbl(res, ~quantile(.$lab_only_correct_only, probs = 0.025)), loco_p975 = purrr::map_dbl(res, ~quantile(.$lab_only_correct_only, probs = 0.975))) %>% mutate(loa_median = purrr::map_dbl(res, ~median(.$lab_only_avg)), loa_p025 = purrr::map_dbl(res, ~quantile(.$lab_only_avg, probs = 0.025)), loa_p975 = purrr::map_dbl(res, ~quantile(.$lab_only_avg, probs = 0.975))) %>% filter(B0 == 0.1 & Bxy == 0.5) %>% select(N, m, Q, starts_with("loco"), starts_with("loa")) %>% pivot_longer(cols = loco_median:loa_p975, names_to = c("type", "tile"),names_pattern = "(.*)_(.*)", values_to = "value") %>% pivot_wider(names_from = "tile") %>% ggplot(aes(x = Q, y = median, ymin = p025, ymax = p975, fill = type, col = type))  + geom_line() + geom_ribbon(alpha = 0.2) + facet_grid(N~m) + geom_hline(yintercept = .5, linetype = 2, col = "grey")