Skip to content

Instantly share code, notes, and snippets.

@dantalus
Created August 25, 2015 12:21
Show Gist options
  • Save dantalus/e5285efd109d32e5586c to your computer and use it in GitHub Desktop.
Save dantalus/e5285efd109d32e5586c to your computer and use it in GitHub Desktop.
correlationL <- list()
i <- 1
for (r in seq(.75, .95, .05)){ # Correlations
for (n in seq(10, 1000, 10)){ # Sample sizes
z <- atanh(r) # Fisher's transformation
sez <- 1 / sqrt(n - 3) # SE of transformed variable
lower <- z - (1.96 * sez)
upper <- z + (1.96 * sez)
lowert <- (exp(2 * lower) - 1) / (exp(2 * lower) + 1) # Back-transform
uppert <- (exp(2 * upper) - 1) / (exp(2 * upper) + 1) # Back-transform
correlationL[i] <- as.data.frame(c(r, n, z, sez, lower, upper,
lowert, uppert))
i <- i + 1
}
}
correlationDf <- as.data.frame(do.call(rbind, correlationL))
colnames(correlationDf) <- c("Correlation", "n", "z", "sez", "lowerz",
"upperz", "lowerR", "upperR")
# Plot the 95% CI for correlations at different sample sizes, all in one plot
ggplot(correlationDf,
aes(x = n, y = Correlation, ymin = lowerR, ymax = upperR,
group = factor(Correlation))) +
geom_ribbon(aes(fill = factor(Correlation)), color = "black", alpha = .5) +
geom_line(aes(alpha = n), color = "white", show_guide = F) +
geom_vline(x = 550, color = "red") +
coord_cartesian(ylim = c(.6, 1)) +
ylab("Correlation") +
xlab("Sample size") +
theme_bw() +
theme(text = element_text (color = "black", family = "serif"),
panel.border = element_blank(),
panel.grid.major = element_line(colour = "grey70", size = 0.2)) +
scale_fill_brewer(name = "Correlation", palette = "Set1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment