Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(tidyverse)
- tb <- tibble(A_effect = sort(rep(seq(-.5,.5,.002),501)),
- B_effect = rep(seq(-.5,.5,.002),501)) %>%
- mutate(noA_noB = .5,
- A_noB = .5 + A_effect,
- noA_B = .5 + B_effect,
- A_B = .5 + A_effect + B_effect) %>%
- mutate(B_among_A = (A_B)/(A_B + A_noB),
- B_among_nonA = (noA_B)/(noA_B+noA_noB)) %>%
- mutate(effect = ifelse(A_B < 0 | A_B > 1, NA, B_among_A - B_among_nonA))
- ggplot(tb, aes(x = A_effect, y = B_effect)) +
- geom_raster(aes(fill = effect), interpolate=TRUE) +
- scale_fill_gradient2(low="red", mid="blue", high="red",
- midpoint=0, limits=range(tb$effect),
- name = "Collider Bias") +
- theme_classic() +
- labs(x = "Effect of binary A on P(C = 1)",
- y = "Effect of binary B on P(C = 1)",
- title = "Estimated linear effect of A on B when selecting on collider",
- caption = "A, B, C are binary w/ mean .5. True A and B relationship is 0.\nGray is when combined A and B effect gives P(C) outside 0 and 1.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement