Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Created January 12, 2024 12:39
Show Gist options
  • Save CnrLwlss/5f1c30ecfee7f0872a7630e80038a6ea to your computer and use it in GitHub Desktop.
Save CnrLwlss/5f1c30ecfee7f0872a7630e80038a6ea to your computer and use it in GitHub Desktop.
Sometimes zero correlation does not mean "randomly distributed", rather just "no effect".
seed(99)
N = 100
x = runif(N)
noise = rnorm(N,mean=0,sd=0.1)
y_corr = 1*x + noise
y_nocorr = noise
rsq_corr = cor(x,y_corr)
rsq_nocorr = cor(x,y_nocorr)
plot(x,y_corr,xlim=c(0,1),ylim=c(-0.3,1.0),pch=16,xlab="x",ylab="y",cex.lab=1.55)
points(x,y_nocorr,pch=16,col="red")
legend("topleft",c(paste("Corr:",format(round(rsq_corr,3),nsmall=3)),paste("Corr:",format(round(rsq_nocorr,3),nsmall=3))),pch=16,col=c("black","red"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment