Created
February 2, 2021 17:38
-
-
Save sTeamTraen/ab70c2627e7fb4b8bbc4adede65345eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple demonstration of mean absolute correlation with random data. | |
# By Nick Brown, February 2020. License: CC-0. | |
library(ggplot2) | |
set.seed(1) | |
iter <- 1000 | |
# Create data frame to contain correlation results, excluding CI (which is a list) | |
ct.names <- names(cor.test(1:3, 3:1)) # little hack: correlation is perfect, so no CI | |
cors <- data.frame(matrix(ncol=length(ct.names), nrow=iter)) | |
names(cors) <- ct.names | |
cat("Sample size\tN significant\tMean r\t\tMean absolute r\n") | |
for (n in c(10, 30, 100, 300, 1000, 3000, 10000)) { | |
max.abs.r <- 0 | |
total.r <- 0 | |
total.abs.r <- 0 | |
for (i in 1:iter) { | |
s1 <- rnorm(n, 0, 1) | |
s2 <- rnorm(n, 0, 1) | |
ct <- cor.test(s1, s2) | |
cors[i,] <- ct[1:ncol(cors)] | |
r <- ct$estimate | |
abs.r <- abs(r) | |
total.r <- total.r + r | |
total.abs.r <- total.abs.r + abs(r) | |
if (abs(r) > max.abs.r) { | |
max.abs.r <- abs(r) | |
if (n <= 30) { | |
max.plot.r <- r | |
max.data <- data.frame(s1, s2) | |
} | |
} | |
} | |
n.sig <- nrow(cors[(cors$p.value < 0.05),]) | |
mean.r <- total.r / iter | |
mean.abs.r <- total.abs.r / iter | |
cat(sprintf("%d", n), "\t\t", n.sig, "\t\t", sprintf("%.3f", mean.r), "\t\t", sprintf("%.3f", mean.abs.r), "\n", sep="") | |
} | |
ggplot(max.data, aes(x=s1, y=s2)) + | |
geom_point(colour="red") + | |
geom_smooth(method="lm", formula=y~x, se=FALSE) + | |
annotate(geom="label", label=sprintf("r=%.3f", max.plot.r), x=1.5, y=1.5, fill="white") + | |
labs(x="Sample 1", y="Sample 2") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment