]> code.communitydata.science - ml_measurement_error_public.git/blob - irr/loco_loa.R
Add another robustness check for varying levels of accuracy.
[ml_measurement_error_public.git] / irr / loco_loa.R
1 .emulate_coding <- function(ground_truth, Q = 1) {
2     if (runif(1) > Q) {
3         return(sample(c(0, 1), size = 1, replace = TRUE))
4     } else {
5         return(ground_truth)
6     }
7 }
8
9 distort_gt <- function(x, Q = NULL) {
10     return(purrr::map_dbl(x, .emulate_coding, Q = Q))
11 }
12
13 N <- c(1000, 3600, 14400)
14 m <- c(75, 150, 300)
15
16 B0 <- c(0, 0.1, 0.3)
17 Bxy <- c(0.1, 0.2, 0.5)
18
19 Q <- c(.6, .8, .9)
20
21 conditions <- expand.grid(N, m, B0, Bxy, Q)
22
23 colnames(conditions) <- c("N", "m", "B0", "Bxy", "Q")
24
25 logistic <- function(x) {1/(1+exp(-1*x))}
26
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
30
31     dx <- as.numeric(distort_gt(x, Q = Q))
32
33     randomidx <- sample(seq(N), m)
34     
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
41     
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))
56 }
57
58 ## res <- list()
59
60 ## for (i in seq(nrow(conditions))) {
61 ##     message(i)
62 ##     res[[i]] <- purrr::map_dfr(1:100, ~.step(., conditions$Bxy[i], conditions$B0[i], conditions$Q[i], conditions$N[i], conditions$m[i]))
63 ## }
64
65 require(furrr)
66 plan(multisession)
67
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]))
70 }
71
72 res <- future_map(seq(nrow(conditions)), .run, conditions = conditions, .progress = TRUE)
73
74 ##saveRDS(res, "rubin_res.RDS")
75
76 conditions <- tibble::as_tibble(conditions)
77 conditions$res <- res
78
79 require(tidyverse)
80
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")

Community Data Science Collective || Want to submit a patch?