Skip to content

Instantly share code, notes, and snippets.

@stonegold546
Last active May 20, 2022 20:34
Show Gist options
  • Save stonegold546/c26f4cee1cc29eaad528fab86be14b9c to your computer and use it in GitHub Desktop.
Save stonegold546/c26f4cee1cc29eaad528fab86be14b9c to your computer and use it in GitHub Desktop.
trivial effect plot
library(data.table)
library(ggplot2)
library(scales)
set.seed(12345)
post_df <- data.table(
est = rnorm(13 * 12 / 2, 0, .05),
se = rlnorm(13 * 12 / 2, log(.09), log(.20 / .09) / qnorm(.9999))
)
post_df[, lo := qnorm(.025, est, se)]
post_df[, hi := qnorm(.975, est, se)]
post_df[, study_pos := frank(est)]
post_df
# Using .2 as limit for minor effect
d_lim <- .2
ggplot(post_df, aes(study_pos, est, ymin = lo, ymax = hi)) +
geom_rect(aes(xmin = -10, xmax = 85, ymin = -d_lim, ymax = d_lim), fill = "#e3e3e3") +
geom_segment(x = 0, xend = 79, y = 0, yend = 0, linetype = 2, size = .125) +
geom_segment(x = -5.75, xend = -5.75, y = -d_lim, yend = d_lim, linetype = 1, size = .125 / 4,
arrow = arrow(length = unit(0.03, "npc"))) +
geom_segment(x = -5.75, xend = -5.75, y = d_lim, yend = -d_lim, linetype = 1, size = .125 / 4,
arrow = arrow(length = unit(0.03, "npc"))) +
annotate("text", x = -7, y = 0, hjust = 0.5, size = 3,
label = "Range for small effects", angle = 90) +
geom_point(size = 1, alpha = .5) + geom_linerange(alpha = .5, size = .25) +
# Fake y-axis
geom_vline(xintercept = -.75, size = .25) +
# Fake y-axis labels
annotate("text", label = .2, x = -1.625, y = .2, hjust = 1, size = 3.25, alpha = .65) +
annotate("text", label = "0.0", x = -1.625, y = 0, hjust = 1, size = 3.25, alpha = .65) +
annotate("text", label = -.2, x = -1.625, y = -.2, hjust = 1, size = 3.25, alpha = .65) +
annotate("text", label = -.4, x = -1.625, y = -.4, hjust = 1, size = 3.25, alpha = .65) +
# Fake y-axis ticks
geom_segment(x = -.75, xend = -1.25, y = .2, yend = .2, size = .25) +
geom_segment(x = -.75, xend = -1.25, y = 0, yend = 0, size = .25) +
geom_segment(x = -.75, xend = -1.25, y = -.2, yend = -.2, size = .25) +
geom_segment(x = -.75, xend = -1.25, y = -.4, yend = -.4, size = .25) +
coord_cartesian(xlim = c(-5, 75.5)) +
theme_bw() +
labs(y = "difference in differences",
subtitle = "All pairwise difference estimates are small") +
theme(axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
plot.subtitle = element_text(hjust = .17),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
ggsave("asz_01_l.png", width = 6.5, height = 4)
ggplot(post_df, aes(study_pos, est, ymin = lo, ymax = hi)) +
geom_rect(aes(xmin = -1, xmax = 85, ymin = -d_lim, ymax = d_lim), fill = "#e3e3e3") +
geom_segment(x = 0, xend = 79, y = 0, yend = 0, linetype = 2, size = .125) +
geom_segment(x = 81, xend = 81, y = -d_lim, yend = d_lim, linetype = 1, size = .125 / 4,
arrow = arrow(length = unit(0.03, "npc"))) +
geom_segment(x = 81, xend = 81, y = d_lim, yend = -d_lim, linetype = 1, size = .125 / 4,
arrow = arrow(length = unit(0.03, "npc"))) +
annotate("text", x = 82, y = 0, hjust = 0.5, size = 3,
label = "Range for small effects", angle = 270) +
geom_point(size = 1, alpha = .5) + geom_linerange(alpha = .5, size = .25) +
coord_cartesian(xlim = c(3, 80)) +
theme_bw() +
labs(y = "difference in differences",
subtitle = "All pairwise difference estimates are small") +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.line.y = element_line(size = .25),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
ggsave("asz_01_r.png", width = 6.5, height = 4)
# Calculate prob(eff > d_lim OR eff < -d_lim)
post_df[, p_gt_d_lim := pnorm(d_lim, est, se, lower.tail = FALSE) +
pnorm(-d_lim, est, se, lower.tail = TRUE)]
post_df
# Histogram of probability differences excced abs(d_lim)
ggplot(post_df, aes(p_gt_d_lim)) + geom_histogram(col = 1) + theme_classic() +
scale_x_continuous(
labels = percent_format(),
name = paste0("Probability effect exceeds ", d_lim))
ggsave("asz_02.png", width = 6.5, height = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment